-
Notifications
You must be signed in to change notification settings - Fork 42
/
configure_flags.go
151 lines (121 loc) · 4.06 KB
/
configure_flags.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright © 2023 Doppler <support@doppler.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"errors"
"fmt"
"github.com/DopplerHQ/cli/pkg/configuration"
"github.com/DopplerHQ/cli/pkg/models"
"github.com/DopplerHQ/cli/pkg/printer"
"github.com/DopplerHQ/cli/pkg/utils"
"github.com/spf13/cobra"
)
var configureFlagsCmd = &cobra.Command{
Use: "flags",
Short: "View current flags",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
values := map[string]bool{}
flags := models.GetFlags()
for _, flag := range flags {
value := configuration.GetFlag(flag)
values[flag] = value
}
printer.Flags(values, utils.OutputJSON)
},
}
var configureFlagsGetCmd = &cobra.Command{
Use: "get [flag]",
Short: "Get the value of a flag",
ValidArgsFunction: FlagsValidArgs,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
plain := utils.GetBoolFlag(cmd, "plain")
flag := args[0]
if !configuration.IsValidFlag(flag) {
utils.HandleError(errors.New("invalid flag " + flag))
}
enabled := configuration.GetFlag(flag)
printer.Flag(flag, enabled, utils.OutputJSON, plain, false)
},
}
var configureFlagsEnableCmd = &cobra.Command{
Use: "enable [flag]",
Short: "Enable a flag",
ValidArgsFunction: FlagsValidArgs,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
flag := args[0]
if !configuration.IsValidFlag(flag) {
utils.HandleError(errors.New("invalid flag " + flag))
}
const value = true
configuration.SetFlag(flag, value)
if !utils.Silent {
printer.Flag(flag, value, utils.OutputJSON, false, false)
}
},
}
var configureFlagsDisableCmd = &cobra.Command{
Use: "disable [flag]",
Short: "Disable a flag",
ValidArgsFunction: FlagsValidArgs,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
flag := args[0]
if !configuration.IsValidFlag(flag) {
utils.HandleError(errors.New("invalid flag " + flag))
}
const value = false
configuration.SetFlag(flag, value)
if !utils.Silent {
printer.Flag(flag, value, utils.OutputJSON, false, false)
}
},
}
var configureFlagsResetCmd = &cobra.Command{
Use: "reset [flag]",
Short: "Reset a flag to its default",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
flag := args[0]
if !configuration.IsValidFlag(flag) {
utils.HandleError(errors.New("invalid flag " + flag))
}
yes := utils.GetBoolFlag(cmd, "yes")
defaultValue := configuration.GetFlagDefault(flag)
if !yes {
utils.PrintWarning(fmt.Sprintf("This will reset the %s flag to %t", flag, defaultValue))
if !utils.ConfirmationPrompt("Continue?", false) {
utils.Log("Aborting")
return
}
}
configuration.SetFlag(flag, defaultValue)
printer.Flag(flag, defaultValue, utils.OutputJSON, false, false)
},
}
func FlagsValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
persistentValidArgsFunction(cmd)
return models.GetFlags(), cobra.ShellCompDirectiveNoFileComp
}
func init() {
configureCmd.AddCommand(configureFlagsCmd)
configureFlagsGetCmd.Flags().Bool("plain", false, "print value without formatting")
configureFlagsCmd.AddCommand(configureFlagsGetCmd)
configureFlagsCmd.AddCommand(configureFlagsEnableCmd)
configureFlagsCmd.AddCommand(configureFlagsDisableCmd)
configureFlagsResetCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
configureFlagsCmd.AddCommand(configureFlagsResetCmd)
rootCmd.AddCommand(configureFlagsCmd)
}