forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
133 lines (115 loc) · 3.5 KB
/
get.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package cmd
import (
"encoding/json"
"fmt"
"io"
"github.com/spf13/cobra"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/util"
)
// GetOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type GetOptions struct {
CommonOptions
Output string
}
var (
get_long = templates.LongDesc(`
Display one or many resources.
` + valid_resources + `
`)
get_example = templates.Examples(`
# List all pipeines
jx get pipeline
# List all URLs for services in the current namespace
jx get url
`)
)
// NewCmdGet creates a command object for the generic "get" action, which
// retrieves one or more resources from a server.
func NewCmdGet(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &GetOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "get TYPE [flags]",
Short: "Display one or many resources",
Long: get_long,
Example: get_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
SuggestFor: []string{"list", "ps"},
}
cmd.AddCommand(NewCmdGetActivity(f, out, errOut))
cmd.AddCommand(NewCmdGetAddon(f, out, errOut))
cmd.AddCommand(NewCmdGetApplications(f, out, errOut))
cmd.AddCommand(NewCmdGetAWSInfo(f, out, errOut))
cmd.AddCommand(NewCmdGetBranchPattern(f, out, errOut))
cmd.AddCommand(NewCmdGetBuild(f, out, errOut))
cmd.AddCommand(NewCmdGetBuildPack(f, out, errOut))
cmd.AddCommand(NewCmdGetChat(f, out, errOut))
cmd.AddCommand(NewCmdGetConfig(f, out, errOut))
cmd.AddCommand(NewCmdGetCVE(f, out, errOut))
cmd.AddCommand(NewCmdGetDevPod(f, out, errOut))
cmd.AddCommand(NewCmdGetEnv(f, out, errOut))
cmd.AddCommand(NewCmdGetGit(f, out, errOut))
cmd.AddCommand(NewCmdGetHelmBin(f, out, errOut))
cmd.AddCommand(NewCmdGetIssue(f, out, errOut))
cmd.AddCommand(NewCmdGetIssues(f, out, errOut))
cmd.AddCommand(NewCmdGetPipeline(f, out, errOut))
cmd.AddCommand(NewCmdGetPostPreviewJob(f, out, errOut))
cmd.AddCommand(NewCmdGetPreview(f, out, errOut))
cmd.AddCommand(NewCmdGetQuickstartLocation(f, out, errOut))
cmd.AddCommand(NewCmdGetTeam(f, out, errOut))
cmd.AddCommand(NewCmdGetToken(f, out, errOut))
cmd.AddCommand(NewCmdGetTracker(f, out, errOut))
cmd.AddCommand(NewCmdGetURL(f, out, errOut))
cmd.AddCommand(NewCmdGetUser(f, out, errOut))
return cmd
}
// Run implements this command
func (o *GetOptions) Run() error {
return o.Cmd.Help()
}
// outputEmptyListWarning outputs a warning indicating that no items are available to display
func outputEmptyListWarning(out io.Writer) error {
_, err := fmt.Fprintf(out, "%s\n", "No resources found.")
return err
}
func (o *GetOptions) addGetFlags(cmd *cobra.Command) {
o.Cmd = cmd
cmd.Flags().StringVarP(&o.Output, "output", "o", "", "The output format such as 'yaml'")
}
// renderResult renders the result in a given output format
func (o *GetOptions) renderResult(value interface{}, format string) error {
switch format {
case "json":
data, err := json.Marshal(value)
if err != nil {
return err
}
_, e := o.Out.Write(data)
return e
case "yaml":
data, err := yaml.Marshal(value)
if err != nil {
return err
}
_, e := o.Out.Write(data)
return e
default:
return fmt.Errorf("Unsupported output format: %s", format)
}
}
func formatInt32(n int32) string {
return util.Int32ToA(n)
}