-
Notifications
You must be signed in to change notification settings - Fork 2
/
accounts_list.go
72 lines (58 loc) · 1.6 KB
/
accounts_list.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
package accounts
import (
"encoding/json"
"fmt"
"os"
"github.com/armory-io/kayentactl/internal/options"
"github.com/armory-io/kayentactl/pkg/kayenta"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
types, outFormat string
)
var shorthandMap = map[string]string{
"metrics": "METRICS_STORE",
"config": "CONFIGURATION_STORE",
"object": "OBJECT_STORE",
}
// getCmd represents the get command
var accountListCmd = &cobra.Command{
Use: "list",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
globals, _ := options.Globals(cmd)
kc := kayenta.NewDefaultClient(kayenta.ClientBaseURL(globals.KayentaURL))
credentials, err := kc.GetCredentials()
if err != nil {
log.Fatalf("could not get list of available accounts: %s", err.Error())
}
// TODO: add type filtering
var (
output []byte
)
switch outFormat {
case "json":
output, err = outputJson(credentials)
case "pretty":
// TODO: implement a pretty output instead of just falling back to json
output, err = outputPretty(credentials)
}
if err != nil {
log.Fatalf(err.Error())
}
fmt.Fprint(os.Stdout, string(output))
},
}
func outputJson(creds []kayenta.AccountCredential) ([]byte, error) {
return json.MarshalIndent(creds, "", " ")
}
func outputPretty(creds []kayenta.AccountCredential) ([]byte, error) {
return outputJson(creds)
}
func init() {
accountsCmd.AddCommand(accountListCmd)
accountListCmd.Flags().StringVarP(&types, "types", "t", "", "filter by account type")
accountListCmd.Flags().StringVarP(&outFormat, "output", "o", "pretty", "output format: json|pretty")
}