...
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 var getCmd = &ffcli.Command{
16 Name: "get",
17 ShortUsage: "get [flags]",
18 ShortHelp: "Get a specific metric descriptor by name",
19 LongHelp: strings.TrimSpace(`
20 Fetches the specific metric descriptor by name.
21 `),
22 FlagSet: globalFlags,
23 Exec: runGet,
24 }
25
26 func runGet(ctx context.Context, args []string) error {
27 var err error
28
29 if len(args) > 0 {
30 Fatalf("too many non-flag arguments: %q", args)
31 }
32 if !checkGetFlags() || !checkGlobalFlags() {
33 return flag.ErrHelp
34 }
35
36 client, err := metrics.New(ctx, projectID)
37 if err != nil {
38 return Errorf("failed to create metric client: %w", err)
39 }
40
41 d, err := client.GetDescriptor(metricPrefix, metricName)
42 if err != nil {
43 return err
44 }
45
46 if jsonFormat {
47 _ = metrics.TmplJSON.Execute(Stdout, d)
48 } else {
49 _ = metrics.TmplDesc.Execute(Stdout, d)
50 fmt.Printf("\n%s", monutil.White(mmBroomTitle))
51 }
52
53 return nil
54 }
55
56
57 func checkGetFlags() bool {
58 if monutil.IsList(projectID) {
59 fmt.Println("Error: 'get' currently supports single project/metric queries only - please specify only one project ID for the 'get' action")
60 return false
61 }
62
63 return true
64 }
65
View as plain text