Files
odo/pkg/odo/cli/preference/set.go
Parthvi Vala b1fbfaa6f1 Preference cleanup (1/n) (#5822)
* Preference(Timeout) int > time.Duration

* Fix odo preference --help for unset examples

* Preference(PushTimeout) int > time.Duration

* Change set, and unset messages

* Preference(RegistryCacheTime) int > time.Duration

* use cobra.ExactArgs for set, and unset

* mockgen

* Unit tests and integration tests

* Fix unit test failure

* Update k8s.io/utils pkg

* Philippe's review

* Fix error message

* Philippe's review

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add minimum acceptable value for preferences accepting time.Difference type

* Fix unit test failure

Signed-off-by: Parthvi Vala <pvala@redhat.com>

* Add migration plan with a warning, add better error message for incompatible formats

Signed-off-by: Parthvi Vala <pvala@redhat.com>
2022-06-23 14:27:39 +00:00

116 lines
3.3 KiB
Go

package preference
import (
"context"
"fmt"
"strings"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/util"
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/redhat-developer/odo/pkg/preference"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
)
const setCommandName = "set"
var (
setLongDesc = ktemplates.LongDesc(`Set an individual value in the odo preference file.
%[1]s`)
setExample = ktemplates.Examples(`
# All available preference values you can set`)
)
// SetOptions encapsulates the options for the command
type SetOptions struct {
// Clients
clientset *clientset.Clientset
// Flags
forceFlag bool
// Parameters
paramName string
paramValue string
}
// NewSetOptions creates a new SetOptions instance
func NewSetOptions() *SetOptions {
return &SetOptions{}
}
func (o *SetOptions) SetClientset(clientset *clientset.Clientset) {
o.clientset = clientset
}
// Complete completes SetOptions after they've been created
func (o *SetOptions) Complete(cmdline cmdline.Cmdline, args []string) (err error) {
o.paramName = strings.ToLower(args[0])
o.paramValue = args[1]
return
}
// Validate validates the SetOptions based on completed values
func (o *SetOptions) Validate() (err error) {
return
}
// Run contains the logic for the command
func (o *SetOptions) Run(ctx context.Context) (err error) {
if !o.forceFlag {
if isSet := o.clientset.PreferenceClient.IsSet(o.paramName); isSet {
// TODO: could add a logic to check if the new value set by the user is not same as the current value
if !ui.Proceed(fmt.Sprintf("%v is already set. Do you want to override it in the config", o.paramName)) {
log.Info("Aborted by the user")
return nil
}
}
}
err = o.clientset.PreferenceClient.SetConfiguration(o.paramName, o.paramValue)
if err != nil {
return err
}
log.Successf("Value of '%s' preference was set to '%s'", o.paramName, o.paramValue)
return nil
}
// NewCmdSet implements the config set odo command
func NewCmdSet(name, fullName string) *cobra.Command {
o := NewSetOptions()
preferenceSetCmd := &cobra.Command{
Use: name,
Short: "Set a value in the odo preference file",
Long: fmt.Sprintf(setLongDesc, preference.FormatSupportedParameters()),
Example: func(exampleString, fullName string) string {
prefClient, err := preference.NewClient()
if err != nil {
util.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
}
properties := prefClient.NewPreferenceList()
for _, property := range properties.Items {
value := property.Default
exampleString += fmt.Sprintf("\n %s %s %v", fullName, property.Name, value)
}
return "\n" + exampleString
}(setExample, fullName),
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
clientset.Add(preferenceSetCmd, clientset.PREFERENCE)
preferenceSetCmd.Flags().BoolVarP(&o.forceFlag, "force", "f", false, "Don't ask for confirmation, set the preference directly")
return preferenceSetCmd
}