forked from redhat-developer/odo
/
describe.go
107 lines (87 loc) · 3.19 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
package component
import (
"encoding/json"
"fmt"
"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/cmd/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 {
outputFlag string
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)
return
}
// Validate validates the describe parameters
func (do *DescribeOptions) Validate() (err error) {
isExists, err := component.Exists(do.Context.Client, do.componentName, do.Context.Application)
if err != nil {
return err
}
if !isExists {
return fmt.Errorf("component %s does not exist", do.componentName)
}
return odoutil.CheckOutputFlag(do.outputFlag)
}
// Run has the logic to perform the required actions as part of command
func (do *DescribeOptions) Run() (err error) {
componentDesc, err := component.GetComponent(do.Context.Client, do.componentName, do.Context.Application, do.Context.Project)
if err != nil {
return err
}
if do.outputFlag == "json" {
out, err := json.Marshal(componentDesc)
if err != nil {
return err
}
fmt.Println(string(out))
} else {
odoutil.PrintComponentInfo(do.Context.Client, do.componentName, componentDesc, do.Context.Application)
}
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 the given component",
Long: `Describe the given component.`,
Example: fmt.Sprintf(describeExample, fullName),
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(do, cmd, args)
},
}
// Add a defined annotation in order to appear in the help menu
describeCmd.Annotations = map[string]string{"command": "component"}
describeCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
completion.RegisterCommandHandler(describeCmd, completion.ComponentNameCompletionHandler)
describeCmd.Flags().StringVarP(&do.outputFlag, "output", "o", "", "output in json format")
// Adding --context flag
genericclioptions.AddContextFlag(describeCmd, &do.componentContext)
//Adding `--project` flag
projectCmd.AddProjectFlag(describeCmd)
//Adding `--application` flag
appCmd.AddApplicationFlag(describeCmd)
return describeCmd
}