forked from redhat-developer/odo
/
unset.go
154 lines (128 loc) · 4.07 KB
/
unset.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
152
153
154
package config
import (
"fmt"
"strings"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/odo/cli/ui"
"github.com/openshift/odo/pkg/config"
"github.com/pkg/errors"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
)
const unsetCommandName = "unset"
var (
unsetLongDesc = ktemplates.LongDesc(`Unset an individual value in the odo configuration file.
%[1]s
%[2]s
`)
unsetExample = ktemplates.Examples(`
# Unset a configuration value in the local config
%[1]s %[2]s
%[1]s %[3]s
%[1]s %[4]s
%[1]s %[5]s
%[1]s %[6]s
%[1]s %[7]s
%[1]s %[8]s
%[1]s %[9]s
%[1]s %[10]s
%[1]s %[11]s
# Unset a env variable in the local config
%[1]s --env KAFKA_HOST --env KAFKA_PORT
`)
)
// UnsetOptions encapsulates the options for the command
type UnsetOptions struct {
paramName string
configForceFlag bool
contextDir string
envArray []string
lci *config.LocalConfigInfo
}
// NewUnsetOptions creates a new UnsetOptions instance
func NewUnsetOptions() *UnsetOptions {
return &UnsetOptions{}
}
// Complete completes UnsetOptions after they've been created
func (o *UnsetOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
if o.envArray == nil {
o.paramName = args[0]
}
cfg, err := config.NewLocalConfigInfo(o.contextDir)
if err != nil {
return err
}
o.lci = cfg
return
}
// Validate validates the UnsetOptions based on completed values
func (o *UnsetOptions) Validate() (err error) {
if !o.lci.ConfigFileExists() {
return errors.New("the directory doesn't contain a component. Use 'odo create' to create a component")
}
return
}
// Run contains the logic for the command
func (o *UnsetOptions) Run() (err error) {
// env variables have been provided
if o.envArray != nil {
envList := o.lci.GetEnvVars()
newEnvList, err := config.RemoveEnvVarsFromList(envList, o.envArray)
if err != nil {
return err
}
if err = o.lci.SetEnvVars(newEnvList); err != nil {
return err
}
log.Success("Environment variables were successfully updated")
log.Italic("\nRun `odo push --config` command to apply changes to the cluster")
return nil
}
if isSet := o.lci.IsSet(o.paramName); isSet {
if !o.configForceFlag && !ui.Proceed(fmt.Sprintf("Do you want to unset %s in the config", o.paramName)) {
fmt.Println("Aborted by the user.")
return nil
}
err = o.lci.DeleteConfiguration(strings.ToLower(o.paramName))
if err != nil {
return err
}
fmt.Println("Local config was successfully updated.")
return nil
}
return errors.New("config already unset, cannot unset a configuration which is not set")
}
// NewCmdUnset implements the config unset odo command
func NewCmdUnset(name, fullName string) *cobra.Command {
o := NewUnsetOptions()
configurationUnsetCmd := &cobra.Command{
Use: name,
Short: "Unset a value in odo config file",
Long: fmt.Sprintf(unsetLongDesc, config.FormatLocallySupportedParameters()),
Example: fmt.Sprintf(fmt.Sprint("\n", unsetExample), fullName,
config.Type, config.Name, config.MinMemory, config.MaxMemory, config.Memory, config.DebugPort, config.Ignore, config.MinCPU, config.MaxCPU, config.CPU),
Args: func(cmd *cobra.Command, args []string) error {
if o.envArray != nil {
// no args are needed
if len(args) > 0 {
return fmt.Errorf("expected 0 args")
}
return nil
}
if len(args) < 1 {
return fmt.Errorf("please provide a parameter name")
} else if len(args) > 1 {
return fmt.Errorf("only one parameter is allowed")
} else {
return nil
}
}, Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
configurationUnsetCmd.Flags().BoolVarP(&o.configForceFlag, "force", "f", false, "Don't ask for confirmation, unsetting the config directly")
configurationUnsetCmd.Flags().StringSliceVarP(&o.envArray, "env", "e", nil, "Unset the environment variables in config")
genericclioptions.AddContextFlag(configurationUnsetCmd, &o.contextDir)
return configurationUnsetCmd
}