-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_resource_profile.go
95 lines (82 loc) · 3.44 KB
/
get_resource_profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package resourceprofile
import (
"encoding/json"
"fmt"
"strings"
"text/tabwriter"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
cloud "github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/formatters"
)
func NewCmdGetResourceProfiles(config *cfg.Config) *cobra.Command {
var coreInstanceKey string
var last uint
var outputFormat, goTemplate string
var showIDs bool
var environment string
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "resource_profiles",
Short: "Display latest resource profiles from an aggregator",
RunE: func(cmd *cobra.Command, args []string) error {
var environmentID string
if environment != "" {
var err error
environmentID, err = completer.LoadEnvironmentID(environment)
if err != nil {
return err
}
}
coreInstanceID, err := completer.LoadCoreInstanceID(coreInstanceKey, environmentID)
if err != nil {
return err
}
pp, err := config.Cloud.ResourceProfiles(config.Ctx, coreInstanceID, cloud.ResourceProfilesParams{
Last: &last,
})
if err != nil {
return fmt.Errorf("could not fetch your resource profiles: %w", err)
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, pp.Items)
}
switch outputFormat {
case "table":
tw := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 4, 1, ' ', 0)
if showIDs {
fmt.Fprintf(tw, "ID\t")
}
fmt.Fprintln(tw, "NAME\tSTORAGE-MAX-CHUNKS-UP\tSTORAGE-SYNC-FULL\tSTORAGE-BACKLOG-MEM-LIMIT\tSTORAGE-VOLUME-SIZE\tSTORAGE-MAX-CHUNKS-PAUSE\tCPU-BUFFER-WORKERS\tCPU-LIMIT\tCPU-REQUEST\tMEM-LIMIT\tMEM-REQUEST\tAGE")
for _, p := range pp.Items {
if showIDs {
fmt.Fprintf(tw, "%s\t", p.ID)
}
fmt.Fprintf(tw, "%s\t%d\t%v\t%s\t%s\t%v\t%d\t%s\t%s\t%s\t%s\t%s\n", p.Name, p.StorageMaxChunksUp, p.StorageSyncFull, p.StorageBacklogMemLimit, p.StorageVolumeSize, p.StorageMaxChunksPause, p.CPUBufferWorkers, p.CPULimit, p.CPURequest, p.MemoryLimit, p.MemoryRequest, formatters.FmtTime(p.CreatedAt))
}
tw.Flush()
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(pp.Items)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(pp.Items)
default:
return fmt.Errorf("unknown output format %q", outputFormat)
}
return nil
},
}
fs := cmd.Flags()
fs.StringVar(&coreInstanceKey, "core-instance", "", "Parent core-instance ID or name")
fs.UintVarP(&last, "last", "l", 0, "Last `N` pipelines. 0 means no limit")
fs.BoolVar(&showIDs, "show-ids", false, "Include resource profile IDs in table output")
fs.StringVar(&environment, "environment", "", "Calyptia environment name")
fs.StringVarP(&outputFormat, "output-format", "o", "table", "Output format. Allowed: table, json, yaml, go-template, go-template-file")
fs.StringVar(&goTemplate, "template", "", "Template string or path to use when -o=go-template, -o=go-template-file. The template format is golang templates\n[http://golang.org/pkg/text/template/#pkg-overview]")
_ = cmd.RegisterFlagCompletionFunc("environment", completer.CompleteEnvironments)
_ = cmd.RegisterFlagCompletionFunc("output-format", formatters.CompleteOutputFormat)
_ = cmd.RegisterFlagCompletionFunc("core-instance", completer.CompleteCoreInstances)
_ = cmd.MarkFlagRequired("core-instance") // TODO: use default aggregator ID from config cmd.
return cmd
}