-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathdescribe.go
More file actions
244 lines (208 loc) · 6.61 KB
/
Copy pathdescribe.go
File metadata and controls
244 lines (208 loc) · 6.61 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package cmd
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/ory/viper"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"knative.dev/func/pkg/config"
fn "knative.dev/func/pkg/functions"
)
func NewDescribeCmd(newClient ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "describe <name>",
Short: "Describe a function",
Long: `Describe a function
Prints the name, route and event subscriptions for a deployed function in
the current directory or from the directory specified with --path.
`,
Example: `
# Show the details of a function as declared in the local func.yaml
{{rootCmdUse}} describe
# Show the details of the function in the directory with yaml output
{{rootCmdUse}} describe --output yaml --path myotherfunc
`,
SuggestFor: []string{"ifno", "fino", "get"},
ValidArgsFunction: CompleteFunctionList,
Aliases: []string{"info", "desc"},
PreRunE: bindEnv("cluster", "cluster-token", "output", "path", "namespace", "verbose"),
RunE: func(cmd *cobra.Command, args []string) error {
return runDescribe(cmd, args, newClient)
},
}
// Config
cfg, err := config.NewDefault()
if err != nil {
fmt.Fprintf(cmd.OutOrStdout(), "error loading config at '%v'. %v\n", config.File(), err)
}
// Function Context
f, _ := fn.NewFunction(effectivePath())
if f.Initialized() {
cfg = cfg.Apply(f)
}
// Flags
cmd.Flags().String("cluster", cfg.Cluster, "Specify a cluster api url for your function deployment. ($FUNC_CLUSTER)")
cmd.Flags().String("cluster-token", "", "Bearer token for cluster authentication. ($FUNC_CLUSTER_TOKEN)")
cmd.Flags().StringP("output", "o", "human", "Output format (human|plain|json|yaml|url) ($FUNC_OUTPUT)")
cmd.Flags().StringP("namespace", "n", defaultNamespace(fn.Function{}, false), "The namespace in which to look for the named function. ($FUNC_NAMESPACE)")
addPathFlag(cmd)
addVerboseFlag(cmd, cfg.Verbose)
if err := cmd.RegisterFlagCompletionFunc("output", CompleteOutputFormatList); err != nil {
fmt.Println("internal: error while calling RegisterFlagCompletionFunc: ", err)
}
return cmd
}
func runDescribe(cmd *cobra.Command, args []string, newClient ClientFactory) (err error) {
cfg, err := newDescribeConfig(cmd, args)
if err != nil {
return
}
// TODO cfg.Prompt()
var details fn.Instance
if cfg.Name != "" { // Describe by name if provided
// don't use local.yaml auth because we are not concerned about func at path
if cfg.Cluster != "" {
cleanup, overrideErr := setupClusterOverride(cfg.Cluster, cfg.ClusterToken, cfg.Namespace, fn.Local{}, cmd.OutOrStderr())
if overrideErr != nil {
return overrideErr
}
defer cleanup()
}
client, done := newClient(ClientConfig{Verbose: cfg.Verbose})
defer done()
details, err = client.Describe(cmd.Context(), cfg.Name, cfg.Namespace, fn.Function{})
if err != nil {
return err
}
} else {
f, err := fn.NewFunction(cfg.Path)
if err != nil {
return err
}
if !f.Initialized() {
return NewErrNotInitializedFromPath(f.Root, "describe")
}
// use local auth - function was **most likely** created locally
if cfg.Cluster != "" {
cleanup, overrideErr := setupClusterOverride(cfg.Cluster, cfg.ClusterToken, cfg.Namespace, f.Local, cmd.OutOrStderr())
if overrideErr != nil {
return overrideErr
}
defer cleanup()
}
client, done := newClient(ClientConfig{Verbose: cfg.Verbose})
defer done()
details, err = client.Describe(cmd.Context(), "", "", f)
if err != nil {
return err
}
}
write(os.Stdout, info(details), cfg.Output)
return
}
// CLI Configuration (parameters)
// ------------------------------
type describeConfig struct {
Cluster string
ClusterToken string
Name string
Namespace string
Output string
Path string
Verbose bool
}
func newDescribeConfig(cmd *cobra.Command, args []string) (cfg describeConfig, err error) {
var name string
if len(args) > 0 {
name = args[0]
}
cfg = describeConfig{
Cluster: viper.GetString("cluster"),
ClusterToken: viper.GetString("cluster-token"),
Name: name,
Namespace: viper.GetString("namespace"),
Output: viper.GetString("output"),
Path: viper.GetString("path"),
Verbose: viper.GetBool("verbose"),
}
if cfg.Name == "" && cmd.Flags().Changed("namespace") {
// logically inconsistent to supply only a namespace.
// Either use the function's local state in its entirety, or specify
// both a name and a namespace to ignore any local function source.
err = fmt.Errorf("must also specify a name when specifying namespace")
}
if cfg.Name != "" && cmd.Flags().Changed("path") {
// logically inconsistent to provide both a name and a path to source.
// Either use the function's local state on disk (--path), or specify
// a name and a namespace to ignore any local function source.
err = ErrNameAndPathConflict
}
return
}
// Output Formatting (serializers)
// -------------------------------
type info fn.Instance
func (i info) Human(w io.Writer) error {
fmt.Fprintln(w, "Function name:")
fmt.Fprintf(w, " %v\n", i.Name)
fmt.Fprintln(w, "Function is built in image:")
fmt.Fprintf(w, " %v\n", i.Image)
fmt.Fprintln(w, "Function is deployed in namespace:")
fmt.Fprintf(w, " %v\n", i.Namespace)
fmt.Fprintln(w, "Routes:")
for _, route := range i.Routes {
fmt.Fprintf(w, " %v\n", route)
}
fmt.Fprintln(w, "Function is ready:")
fmt.Fprintf(w, " %v\n", i.Ready)
fmt.Fprintln(w, "Deployer:")
fmt.Fprintf(w, " %v\n", i.Deployer)
if len(i.Subscriptions) > 0 {
fmt.Fprintln(w, "Subscriptions (Source, Type, Broker):")
for _, s := range i.Subscriptions {
fmt.Fprintf(w, " %v %v %v\n", s.Source, s.Type, s.Broker)
}
}
if len(i.Labels) > 0 {
fmt.Fprintln(w, "Labels:")
for k, v := range i.Labels {
fmt.Fprintf(w, " %v: %v\n", k, v)
}
}
return nil
}
func (i info) Plain(w io.Writer) error {
fmt.Fprintf(w, "Name %v\n", i.Name)
fmt.Fprintf(w, "Image %v\n", i.Image)
fmt.Fprintf(w, "Namespace %v\n", i.Namespace)
for _, route := range i.Routes {
fmt.Fprintf(w, "Route %v\n", route)
}
fmt.Fprintf(w, "Ready %v\n", i.Ready)
fmt.Fprintf(w, "Deployer %v\n", i.Deployer)
if len(i.Subscriptions) > 0 {
for _, s := range i.Subscriptions {
fmt.Fprintf(w, "Subscription %v %v %v\n", s.Source, s.Type, s.Broker)
}
}
if len(i.Labels) > 0 {
for k, v := range i.Labels {
fmt.Fprintf(w, "Label %v %v\n", k, v)
}
}
return nil
}
func (i info) JSON(w io.Writer) error {
return json.NewEncoder(w).Encode(i)
}
func (i info) YAML(w io.Writer) error {
return yaml.NewEncoder(w).Encode(i)
}
func (i info) URL(w io.Writer) error {
if len(i.Routes) > 0 {
fmt.Fprintf(w, "%s\n", i.Routes[0])
}
return nil
}