...

Source file src/edge-infra.dev/cmd/edge/monitoring/metermaid/cli/list.go

Documentation: edge-infra.dev/cmd/edge/monitoring/metermaid/cli

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/peterbourgon/ff/v3/ffcli"
    10  
    11  	metrics "edge-infra.dev/pkg/lib/gcp/monitoring/metrics"
    12  	"edge-infra.dev/pkg/lib/gcp/monitoring/monutil"
    13  )
    14  
    15  type listArgsT struct {
    16  	byProject bool
    17  	byMetric  bool
    18  	labels    string
    19  }
    20  
    21  var (
    22  	listArgs    listArgsT
    23  	listFlagSet = newListFlagSet(&listArgs)
    24  )
    25  
    26  func newListFlagSet(listArgs *listArgsT) *flag.FlagSet {
    27  	listf := newFlagSet("list")
    28  	listf.BoolVar(&listArgs.byProject, "by-project", false, "Lists metric descriptors sorted by project ID. (cannot be used with --by-metric or --by-label)")
    29  	listf.BoolVar(&listArgs.byMetric, "by-metric", false, "Lists metric descriptors sorted by metric. (cannot be used with --by-project or --by-label)")
    30  	return listf
    31  }
    32  
    33  var listCmd = &ffcli.Command{
    34  	Name:       "list",
    35  	ShortUsage: "list [flags]",
    36  	ShortHelp:  "list metric descriptors from a project",
    37  	LongHelp: strings.TrimSpace(`
    38  Lists metric descriptors for one or more projects that match the specified metric type.
    39  `),
    40  	FlagSet: withGlobalFlags(listFlagSet),
    41  	Exec:    runList,
    42  }
    43  
    44  func runList(ctx context.Context, args []string) error {
    45  	var err error
    46  
    47  	if len(args) > 0 {
    48  		Fatalf("too many non-flag arguments: %q", args)
    49  	}
    50  	if !checkListFlags() || !checkGlobalFlags() {
    51  		return flag.ErrHelp
    52  	}
    53  
    54  	projects := strings.Split(strings.ReplaceAll(projectID, " ", ""), ",")
    55  	d, err := retrieveDescriptors(ctx, projects)
    56  	if err != nil {
    57  		return Errorf("failed to get retrieve of metricDescriptors: %w", err)
    58  	}
    59  
    60  	// necessary when comparing specific projects as foreman is a multi-project source
    61  	if filterForeman {
    62  		projects := strings.Split(strings.ReplaceAll(projectID, " ", ""), ",")
    63  		f, err := d.FilterProjects(projects)
    64  		if err != nil {
    65  			return err
    66  		}
    67  		d = f
    68  	}
    69  
    70  	if len(d.Descriptor) == 0 && !silent {
    71  		fmt.Println(monutil.White("No metric descriptors found for the specified project(s) as specified."))
    72  	}
    73  
    74  	// output the list to the appropriate template
    75  	switch {
    76  	case listArgs.byMetric && !jsonFormat:
    77  		_ = metrics.TmplMetricList.Execute(Stdout, d.ListMetrics())
    78  
    79  	case listArgs.byProject && !jsonFormat:
    80  		_ = metrics.TmplProjectList.Execute(Stdout, d.ListProjects())
    81  
    82  	case listArgs.byMetric && jsonFormat:
    83  		_ = metrics.TmplJSON.Execute(Stdout, d.ListMetrics())
    84  
    85  	case listArgs.byProject && jsonFormat:
    86  		_ = metrics.TmplJSON.Execute(Stdout, d.ListProjects())
    87  
    88  	case !listArgs.byMetric && !listArgs.byProject && jsonFormat:
    89  		_ = metrics.TmplJSON.Execute(Stdout, d)
    90  
    91  	default:
    92  		_ = metrics.TmplDescList.Execute(Stdout, d)
    93  	}
    94  
    95  	return nil
    96  }
    97  
    98  // checkListFlags validates the required List subcommand flags have been provided.
    99  func checkListFlags() bool {
   100  	// filter descriptors for those that contain the specified label(s)
   101  	if len(listArgs.labels) != 0 {
   102  		// filterLabels = true
   103  		// labelFilter = strings.Split(listArgs.labels, ",")
   104  
   105  		// TBD - remove stop error once label support is complete
   106  		fmt.Println("Error: 'with-label' not supported yet - coming soon")
   107  		return false
   108  	}
   109  
   110  	if listArgs.byMetric && listArgs.byProject {
   111  		fmt.Println("Error: 'by-metric' and 'by-project' were selected - only one sort method allowed")
   112  		return false
   113  	}
   114  
   115  	return true
   116  }
   117  

View as plain text