package billingrefs import ( "context" "flag" "fmt" dsdssandboxes "edge-infra.dev/pkg/sds/lib/ci/dsds-sandboxes" sandboxes "edge-infra.dev/pkg/sds/lib/ci/dsds-sandboxes" "github.com/peterbourgon/ff/v3/ffcli" ) type BillingRefFlagConfig struct { ProjectsFilePath string ExcludeHeadings bool } func NewCmd() *ffcli.Command { billingRefFlagSet := flag.NewFlagSet("refs", flag.ExitOnError) billingRefFlagConfig := &BillingRefFlagConfig{} bindProjectFlags(billingRefFlagSet, billingRefFlagConfig) return &ffcli.Command{ Name: "refs", ShortUsage: "billingcli refs", ShortHelp: "List billing refs for each project", Exec: func(_ context.Context, _ []string) error { return printBillingRefs(billingRefFlagConfig) }, FlagSet: billingRefFlagSet, } } func bindProjectFlags(flags *flag.FlagSet, config *BillingRefFlagConfig) { flags.StringVar( &config.ProjectsFilePath, "project-dir", dsdssandboxes.DefaultPlaypitDir, "Location of the CI project YAML within edge-infra", ) flags.BoolVar( &config.ExcludeHeadings, "no-headings", false, "Do not print column headings in CSV output", ) } func printBillingRefs(config *BillingRefFlagConfig) error { confs, err := sandboxes.GetBillingConfs(config.ProjectsFilePath) if err != nil { return err } if !config.ExcludeHeadings { printHeading() } printResults(confs) return nil } func printHeading() { fmt.Println("Project ID, Project Name, Billing ref") } func printResults(billingConfigs map[string]*sandboxes.BillingConf) { for _, conf := range billingConfigs { fmt.Printf("%v, %v, %v\n", conf.Metadata.Name, conf.Spec.Name, conf.Metadata.Labels.BillingRef) } }