|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "encoding/xml" |
6 | 7 | "errors" |
7 | 8 | "fmt" |
8 | 9 | "io" |
9 | 10 | "os" |
10 | 11 |
|
| 12 | + "github.com/AlecAivazis/survey/v2" |
11 | 13 | "github.com/ory/viper" |
12 | 14 | "github.com/spf13/cobra" |
13 | 15 | "gopkg.in/yaml.v2" |
| 16 | + |
14 | 17 |
|
15 | 18 | "knative.dev/func/pkg/config" |
16 | 19 | fn "knative.dev/func/pkg/functions" |
@@ -66,7 +69,11 @@ func runDescribe(cmd *cobra.Command, args []string, newClient ClientFactory) (er |
66 | 69 | if err != nil { |
67 | 70 | return |
68 | 71 | } |
69 | | - // TODO cfg.Prompt() |
| 72 | + if cfg.Name == "" && cfg.Path == "" { |
| 73 | + if err := cfg.Prompt(newClient); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + } |
70 | 77 |
|
71 | 78 | client, done := newClient(ClientConfig{Verbose: cfg.Verbose}) |
72 | 79 | defer done() |
@@ -133,6 +140,41 @@ func newDescribeConfig(cmd *cobra.Command, args []string) (cfg describeConfig, e |
133 | 140 | return |
134 | 141 | } |
135 | 142 |
|
| 143 | +// Prompt interactively asks the user to select a function name |
| 144 | +// if none was provided via args or flags. |
| 145 | +func (c *describeConfig) Prompt(newClient ClientFactory) error { |
| 146 | + client, done := newClient(ClientConfig{Verbose: c.Verbose}) |
| 147 | + defer done() |
| 148 | + |
| 149 | + funcs, err := client.List(context.Background(), c.Namespace) |
| 150 | + if err != nil { |
| 151 | + return fmt.Errorf("failed to list functions: %w", err) |
| 152 | + } |
| 153 | + |
| 154 | + if len(funcs) == 0 { |
| 155 | + return errors.New("no functions found to describe") |
| 156 | + } |
| 157 | + |
| 158 | + names := []string{} |
| 159 | + for _, f := range funcs { |
| 160 | + names = append(names, f.Name) |
| 161 | + } |
| 162 | + |
| 163 | + prompt := &survey.Select{ |
| 164 | + Message: "Select a function to describe:", |
| 165 | + Options: names, |
| 166 | + } |
| 167 | + |
| 168 | + var selected string |
| 169 | + if err := survey.AskOne(prompt, &selected); err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + |
| 173 | + c.Name = selected |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | + |
136 | 178 | // Output Formatting (serializers) |
137 | 179 | // ------------------------------- |
138 | 180 |
|
|
0 commit comments