forked from redhat-developer/odo
/
unlink.go
104 lines (81 loc) · 3.39 KB
/
unlink.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
package component
import (
"fmt"
"github.com/openshift/odo/pkg/odo/genericclioptions"
appCmd "github.com/openshift/odo/pkg/odo/cli/application"
projectCmd "github.com/openshift/odo/pkg/odo/cli/project"
"github.com/openshift/odo/pkg/odo/util/completion"
"github.com/openshift/odo/pkg/odo/util"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"github.com/spf13/cobra"
)
// UnlinkRecommendedCommandName is the recommended unlink command name
const UnlinkRecommendedCommandName = "unlink"
var (
unlinkExample = ktemplates.Examples(`# Unlink the 'my-postgresql' service from the current component
%[1]s my-postgresql
# Unlink the 'my-postgresql' service from the 'nodejs' component
%[1]s my-postgresql --component nodejs
# Unlink the 'backend' component from the current component (backend must have a single exposed port)
%[1]s backend
# Unlink the 'backend' service from the 'nodejs' component
%[1]s backend --component nodejs
# Unlink the backend's 8080 port from the current component
%[1]s backend --port 8080`)
unlinkLongDesc = `Unlink component or service from a component.
For this command to be successful, the service or component needs to have been linked prior to the invocation using 'odo link'`
)
// UnlinkOptions encapsulates the options for the odo link command
type UnlinkOptions struct {
componentContext string
*commonLinkOptions
}
// NewUnlinkOptions creates a new UnlinkOptions instance
func NewUnlinkOptions() *UnlinkOptions {
options := UnlinkOptions{}
options.commonLinkOptions = newCommonLinkOptions()
return &options
}
// Complete completes UnlinkOptions after they've been created
func (o *UnlinkOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
err = o.complete(name, cmd, args)
o.operation = o.Client.UnlinkSecret
return err
}
// Validate validates the UnlinkOptions based on completed values
func (o *UnlinkOptions) Validate() (err error) {
return o.validate(false)
}
// Run contains the logic for the odo link command
func (o *UnlinkOptions) Run() (err error) {
return o.run()
}
// NewCmdUnlink implements the link odo command
func NewCmdUnlink(name, fullName string) *cobra.Command {
o := NewUnlinkOptions()
unlinkCmd := &cobra.Command{
Use: fmt.Sprintf("%s <service> --component [component] OR %s <component> --component [component]", name, name),
Short: "Unlink component to a service or component",
Long: unlinkLongDesc,
Example: fmt.Sprintf(unlinkExample, fullName),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
unlinkCmd.PersistentFlags().StringVar(&o.port, "port", "", "Port of the backend to which to unlink")
unlinkCmd.PersistentFlags().BoolVarP(&o.wait, "wait", "w", false, "If enabled the link will return only when the component is fully running after the link is deleted")
// Add a defined annotation in order to appear in the help menu
unlinkCmd.Annotations = map[string]string{"command": "component"}
unlinkCmd.SetUsageTemplate(util.CmdUsageTemplate)
//Adding `--project` flag
projectCmd.AddProjectFlag(unlinkCmd)
//Adding `--application` flag
appCmd.AddApplicationFlag(unlinkCmd)
//Adding `--component` flag
AddComponentFlag(unlinkCmd)
// Adding context flag
genericclioptions.AddContextFlag(unlinkCmd, &o.componentContext)
completion.RegisterCommandHandler(unlinkCmd, completion.UnlinkCompletionHandler)
return unlinkCmd
}