-
Notifications
You must be signed in to change notification settings - Fork 114
/
view.go
98 lines (79 loc) · 2.48 KB
/
view.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
package config
import (
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"
sigyaml "sigs.k8s.io/yaml"
"github.com/akuity/kargo/internal/cli/config"
"github.com/akuity/kargo/internal/cli/io"
"github.com/akuity/kargo/internal/cli/option"
"github.com/akuity/kargo/internal/cli/templates"
)
type viewOptions struct {
genericiooptions.IOStreams
*genericclioptions.PrintFlags
Config config.CLIConfig
RawByteData bool
}
func newViewCommand(cfg config.CLIConfig, streams genericiooptions.IOStreams) *cobra.Command {
cmdOpts := &viewOptions{
Config: cfg,
IOStreams: streams,
PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput("yaml"),
}
cmd := &cobra.Command{
Use: "view",
Short: "Display the CLI config",
Args: option.NoArgs,
Example: templates.Example(`
# Display the CLI config as YAML output
kargo config view
# Display the CLI config as JSON output
kargo config view -o json
# Display the CLI config including sensitive data
kargo config view --raw
# Display the unmasked bearer token in the CLI config
kargo config view --raw --output=jsonpath='{.bearerToken}'
`),
RunE: func(*cobra.Command, []string) error {
return cmdOpts.run()
},
}
// Register the option flags on the command.
cmdOpts.addFlags(cmd)
// Set the input/output streams for the command.
io.SetIOStreams(cmd, cmdOpts.IOStreams)
return cmd
}
// addFlags adds the flags for the get freight options to the provided command.
func (o *viewOptions) addFlags(cmd *cobra.Command) {
o.PrintFlags.AddFlags(cmd)
cmd.Flags().BoolVar(&o.RawByteData, "raw", o.RawByteData, "Display raw byte data and sensitive data")
}
// run displays the CLI config using the provided output format.
func (o *viewOptions) run() error {
cfg := o.Config
if !o.RawByteData {
cfg = config.MaskedConfig(cfg)
}
b, err := sigyaml.Marshal(cfg)
if err != nil {
return err
}
var rawData map[string]any
if err = sigyaml.Unmarshal(b, &rawData); err != nil {
return err
}
u := unstructured.Unstructured{Object: rawData}
// NOTE: This is a workaround to be able to print the object using the
// printer, which requires the object to have a kind set.
// We may want to consider making the CLIConfig a proper "Kubernetes
// API object" in the future.
u.SetKind("CLIConfig")
printer, err := o.PrintFlags.ToPrinter()
if err != nil {
return err
}
return printer.PrintObj(&u, o.Out)
}