...

Source file src/edge-infra.dev/pkg/tools/ci-billing/commands/billingrefs/billingrefs.go

Documentation: edge-infra.dev/pkg/tools/ci-billing/commands/billingrefs

     1  package billingrefs
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  
     8  	dsdssandboxes "edge-infra.dev/pkg/sds/lib/ci/dsds-sandboxes"
     9  	sandboxes "edge-infra.dev/pkg/sds/lib/ci/dsds-sandboxes"
    10  
    11  	"github.com/peterbourgon/ff/v3/ffcli"
    12  )
    13  
    14  type BillingRefFlagConfig struct {
    15  	ProjectsFilePath string
    16  	ExcludeHeadings  bool
    17  }
    18  
    19  func NewCmd() *ffcli.Command {
    20  	billingRefFlagSet := flag.NewFlagSet("refs", flag.ExitOnError)
    21  	billingRefFlagConfig := &BillingRefFlagConfig{}
    22  	bindProjectFlags(billingRefFlagSet, billingRefFlagConfig)
    23  
    24  	return &ffcli.Command{
    25  		Name:       "refs",
    26  		ShortUsage: "billingcli refs",
    27  		ShortHelp:  "List billing refs for each project",
    28  		Exec: func(_ context.Context, _ []string) error {
    29  			return printBillingRefs(billingRefFlagConfig)
    30  		},
    31  		FlagSet: billingRefFlagSet,
    32  	}
    33  }
    34  
    35  func bindProjectFlags(flags *flag.FlagSet, config *BillingRefFlagConfig) {
    36  	flags.StringVar(
    37  		&config.ProjectsFilePath,
    38  		"project-dir",
    39  		dsdssandboxes.DefaultPlaypitDir,
    40  		"Location of the CI project YAML within edge-infra",
    41  	)
    42  	flags.BoolVar(
    43  		&config.ExcludeHeadings,
    44  		"no-headings",
    45  		false,
    46  		"Do not print column headings in CSV output",
    47  	)
    48  }
    49  
    50  func printBillingRefs(config *BillingRefFlagConfig) error {
    51  	confs, err := sandboxes.GetBillingConfs(config.ProjectsFilePath)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	if !config.ExcludeHeadings {
    57  		printHeading()
    58  	}
    59  
    60  	printResults(confs)
    61  	return nil
    62  }
    63  
    64  func printHeading() {
    65  	fmt.Println("Project ID, Project Name, Billing ref")
    66  }
    67  
    68  func printResults(billingConfigs map[string]*sandboxes.BillingConf) {
    69  	for _, conf := range billingConfigs {
    70  		fmt.Printf("%v, %v, %v\n", conf.Metadata.Name, conf.Spec.Name, conf.Metadata.Labels.BillingRef)
    71  	}
    72  }
    73  

View as plain text