forked from redhat-developer/odo
/
update.go
92 lines (75 loc) · 2.36 KB
/
update.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
package registry
import (
// Built-in packages
"fmt"
// Third-party packages
"github.com/pkg/errors"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"
// odo packages
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/preference"
"github.com/openshift/odo/pkg/util"
)
const updateCommandName = "update"
// "odo registry update" command description and examples
var (
updateLongDesc = ktemplates.LongDesc(`Update devfile registry URL`)
updateExample = ktemplates.Examples(`# Update devfile registry URL
%[1]s CheRegistry https://che-devfile-registry-update.openshift.io
`)
)
// UpdateOptions encapsulates the options for the "odo registry update" command
type UpdateOptions struct {
operation string
registryName string
registryURL string
forceFlag bool
}
// NewUpdateOptions creates a new UpdateOptions instance
func NewUpdateOptions() *UpdateOptions {
return &UpdateOptions{}
}
// Complete completes UpdateOptions after they've been created
func (o *UpdateOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
o.operation = "update"
o.registryName = args[0]
o.registryURL = args[1]
return
}
// Validate validates the UpdateOptions based on completed values
func (o *UpdateOptions) Validate() (err error) {
err = util.ValidateURL(o.registryURL)
if err != nil {
return err
}
return
}
// Run contains the logic for "odo registry update" command
func (o *UpdateOptions) Run() (err error) {
cfg, err := preference.New()
if err != nil {
return errors.Wrap(err, "unable to update registry")
}
err = cfg.RegistryHandler(o.operation, o.registryName, o.registryURL, o.forceFlag)
if err != nil {
return err
}
return nil
}
// NewCmdUpdate implements the "odo registry update" command
func NewCmdUpdate(name, fullName string) *cobra.Command {
o := NewUpdateOptions()
registryUpdateCmd := &cobra.Command{
Use: fmt.Sprintf("%s <registry name> <registry URL>", name),
Short: updateLongDesc,
Long: updateLongDesc,
Example: fmt.Sprintf(fmt.Sprint(updateExample), fullName),
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
registryUpdateCmd.Flags().BoolVarP(&o.forceFlag, "force", "f", false, "Don't ask for confirmation, update the registry directly")
return registryUpdateCmd
}