-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
105 lines (90 loc) · 2.41 KB
/
get.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
package cmd
import (
"fmt"
"github.com/davidalpert/go-printers/v1"
"github.com/davidalpert/go-yoss/internal/cfgset"
"github.com/davidalpert/go-yoss/internal/cmd/helpers"
"github.com/davidalpert/go-yoss/internal/provider"
"github.com/spf13/cobra"
)
type GetOptions struct {
*printers.PrinterOptions
provider.Options
SupportedProviders map[string]provider.NewProviderFn
Key string
Recursive bool
//DecryptResult bool
}
func NewGetOptions(ioStreams printers.IOStreams) *GetOptions {
return &GetOptions{
PrinterOptions: printers.NewPrinterOptions().WithStreams(ioStreams).WithDefaultOutput("text"),
SupportedProviders: helpers.SupportedProviders,
}
}
func NewCmdGet(ioStreams printers.IOStreams) *cobra.Command {
o := NewGetOptions(ioStreams)
var cmd = &cobra.Command{
Use: "get <provider> <path>",
Short: "get a config value",
Aliases: []string{"g"},
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(cmd, args); err != nil {
return err
}
if err := o.Validate(); err != nil {
return err
}
return o.Run()
},
}
o.PrinterOptions.AddPrinterFlags(cmd.Flags())
o.Options.AddProviderOptions(cmd.Flags())
cmd.Flags().BoolVarP(&o.Debug, "debug", "d", false, "enable debug output")
cmd.Flags().BoolVarP(&o.Recursive, "recursive", "r", false, "recursively get all values under that path")
return cmd
}
// Complete the options
func (o *GetOptions) Complete(cmd *cobra.Command, args []string) error {
o.ProviderName = args[0]
if len(args) > 1 {
o.Key = args[1]
} else {
o.Key = "/"
}
return nil
}
// Validate the options
func (o *GetOptions) Validate() error {
if err := helpers.ValidateProviderKey(o.ProviderName); err != nil {
return err
}
return o.PrinterOptions.Validate()
}
// Run the command
func (o *GetOptions) Run() error {
p, err := o.SupportedProviders[o.ProviderName](&o.Options)
if err != nil {
return fmt.Errorf("building provider: %s", err)
}
flattened := map[string]string{}
if o.Recursive {
if many, err := p.GetValueTree(o.Key); err != nil {
return err
} else {
for k, v := range many {
flattened[k] = v
}
}
} else {
if one, err := p.GetValue(o.Key); err != nil {
return err
} else {
flattened[o.Key] = one
}
}
if o.FormatCategory() == "text" {
return o.WriteOutput(cfgset.FlattenedToString(flattened))
}
return o.WriteOutput(flattened)
}