forked from redhat-developer/odo
/
describe.go
114 lines (91 loc) · 3.36 KB
/
describe.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
package component
import (
"fmt"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/machineoutput"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/component"
appCmd "github.com/openshift/odo/pkg/odo/cli/application"
projectCmd "github.com/openshift/odo/pkg/odo/cli/project"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/odo/util/completion"
ktemplates "k8s.io/kubernetes/pkg/kubectl/util/templates"
"github.com/spf13/cobra"
)
// DescribeRecommendedCommandName is the recommended describe command name
const DescribeRecommendedCommandName = "describe"
var describeExample = ktemplates.Examples(` # Describe nodejs component
%[1]s nodejs
`)
// DescribeOptions is a dummy container to attach complete, validate and run pattern
type DescribeOptions struct {
componentContext string
*ComponentOptions
}
// NewDescribeOptions returns new instance of ListOptions
func NewDescribeOptions() *DescribeOptions {
return &DescribeOptions{"", &ComponentOptions{}}
}
// Complete completes describe args
func (do *DescribeOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
err = do.ComponentOptions.Complete(name, cmd, args)
if err != nil {
return err
}
return nil
}
// Validate validates the describe parameters
func (do *DescribeOptions) Validate() (err error) {
if do.Context.Project == "" || do.Application == "" {
return odoutil.ThrowContextError()
}
return nil
}
// Run has the logic to perform the required actions as part of command
func (do *DescribeOptions) Run() (err error) {
var componentDesc component.Component
state := component.GetComponentState(do.Context.Client, do.componentName, do.Context.Application)
if state == component.StateTypeNotPushed || state == component.StateTypeUnknown {
componentDesc, err = component.GetComponentFromConfig(do.LocalConfigInfo)
componentDesc.Status.State = state
if err != nil {
return err
}
} else {
componentDesc, err = component.GetComponent(do.Context.Client, do.componentName, do.Context.Application, do.Context.Project)
if err != nil {
return err
}
}
if log.IsJSON() {
componentDesc.Spec.Ports = do.LocalConfigInfo.GetPorts()
machineoutput.OutputSuccess(componentDesc)
} else {
odoutil.PrintComponentInfo(do.Context.Client, do.componentName, componentDesc, do.Context.Application, do.Context.Project)
}
return
}
// NewCmdDescribe implements the describe odo command
func NewCmdDescribe(name, fullName string) *cobra.Command {
do := NewDescribeOptions()
var describeCmd = &cobra.Command{
Use: fmt.Sprintf("%s [component_name]", name),
Short: "Describe component",
Long: `Describe component.`,
Example: fmt.Sprintf(describeExample, fullName),
Args: cobra.RangeArgs(0, 1),
Annotations: map[string]string{"machineoutput": "json", "command": "component"},
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(do, cmd, args)
},
}
describeCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
completion.RegisterCommandHandler(describeCmd, completion.ComponentNameCompletionHandler)
// Adding --context flag
genericclioptions.AddContextFlag(describeCmd, &do.componentContext)
//Adding `--project` flag
projectCmd.AddProjectFlag(describeCmd)
//Adding `--application` flag
appCmd.AddApplicationFlag(describeCmd)
return describeCmd
}