/
get_config_section.go
137 lines (117 loc) · 3.54 KB
/
get_config_section.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package config
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"text/tabwriter"
"github.com/go-logfmt/logfmt"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/calyptia/api/types"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/formatters"
)
func NewCmdGetConfigSections(config *cfg.Config) *cobra.Command {
var last uint
var before string
var outputFormat, goTemplate string
var showIDs bool
cmd := &cobra.Command{
Use: "config_sections", // child of `get`
Short: "List config sections",
Long: "List all snipets of config sections,\n" +
"sorted by creation time in descending order.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
params := types.ConfigSectionsParams{}
if last != 0 {
params.Last = &last
}
if before != "" {
params.Before = &before
}
cc, err := config.Cloud.ConfigSections(ctx, config.ProjectID, params)
if err != nil {
return fmt.Errorf("cloud: %w", err)
}
if strings.HasPrefix(outputFormat, "go-template") {
return formatters.ApplyGoTemplate(cmd.OutOrStdout(), outputFormat, goTemplate, cc.Items)
}
switch outputFormat {
case "json":
return json.NewEncoder(cmd.OutOrStdout()).Encode(cc)
case "yml", "yaml":
return yaml.NewEncoder(cmd.OutOrStdout()).Encode(cc)
default:
return renderConfigSectionsTable(cmd.OutOrStdout(), cc, showIDs)
}
},
}
fs := cmd.Flags()
fs.UintVarP(&last, "last", "l", 0, "Last `N` config sections. 0 means no limit")
fs.StringVar(&before, "before", "", "Only show config sections created before the given cursor")
fs.BoolVar(&showIDs, "show-ids", false, "Show config section IDs. Only applies when output format is table")
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("output-format", formatters.CompleteOutputFormat)
return cmd
}
func renderConfigSectionsTable(w io.Writer, cc types.ConfigSections, showIDs bool) error {
tw := tabwriter.NewWriter(w, 0, 4, 1, ' ', 0)
if showIDs {
if _, err := fmt.Fprint(tw, "ID\t"); err != nil {
return err
}
}
fmt.Fprintln(tw, "KIND\tNAME\tPROPERTIES\tAGE")
for _, cs := range cc.Items {
if showIDs {
_, err := fmt.Fprintf(tw, "%s\t", cs.ID)
if err != nil {
return err
}
}
props, err := pairsToLogfmt(cs.Properties, true)
if err != nil {
return err
}
name := pairsName(cs.Properties)
_, err = fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", cs.Kind, name, props, formatters.FmtTime(cs.CreatedAt))
if err != nil {
return err
}
}
if err := tw.Flush(); err != nil {
return err
}
if cc.EndCursor != nil {
_, err := fmt.Fprintf(w, "\n\n# Previous page:\n\tcalyptia get config_sections --before %s\n", *cc.EndCursor)
if err != nil {
return err
}
}
return nil
}
func pairsToLogfmt(pp types.Pairs, skipName bool) (string, error) {
var buff bytes.Buffer
enc := logfmt.NewEncoder(&buff)
for _, p := range pp {
if skipName && strings.EqualFold(p.Key, "Name") {
continue
}
err := enc.EncodeKeyval(p.Key, p.Value)
if err != nil {
return "", fmt.Errorf("encode property key-val: %w", err)
}
}
enc.Reset()
return buff.String(), nil
}
func pairsName(pp types.Pairs) string {
if v, ok := pp.Get("Name"); ok {
return fmt.Sprintf("%v", v)
}
return ""
}