-
Notifications
You must be signed in to change notification settings - Fork 10
/
get.go
78 lines (70 loc) · 1.76 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
package get
import (
"fmt"
"io"
"github.com/AlecAivazis/survey/v2"
"github.com/OctopusDeploy/cli/pkg/config"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func NewCmdGet(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "get [key]",
Short: "Gets the value of config key for Octopus CLI",
RunE: func(cmd *cobra.Command, args []string) error {
key := ""
if len(args) > 0 {
key = args[0]
}
return getRun(f.IsPromptEnabled(), f.Ask, key, cmd.OutOrStdout())
},
}
return cmd
}
func getRun(isPromptEnabled bool, ask question.Asker, key string, out io.Writer) error {
if key != "" {
if !config.IsValidKey(key) {
return fmt.Errorf("the key '%s' is not a valid", key)
}
}
value := ""
configFile := viper.New()
configFile.SetConfigFile(viper.ConfigFileUsed())
configFile.ReadInConfig()
if isPromptEnabled && key == "" {
k, err := promptMissing(ask)
if err != nil {
return err
}
key = k
}
value = configFile.GetString(key)
if value == "" && !configFile.InConfig(key) {
return fmt.Errorf("unable to get value for key: %s", key)
}
fmt.Fprintln(out, value)
return nil
}
func promptMissing(ask question.Asker) (string, error) {
keys := []string{
constants.ConfigApiKey,
constants.ConfigSpace,
constants.ConfigNoPrompt,
constants.ConfigUrl,
constants.ConfigOutputFormat,
constants.ConfigShowOctopus,
constants.ConfigEditor,
// constants.ConfigProxyUrl,
}
var selectKey string
if err := ask(&survey.Select{
Options: keys,
Message: "What key you would you would like to see the value of?",
}, &selectKey); err != nil {
return "", err
}
return selectKey, nil
}