Skip to content

Commit 0cdd5cd

Browse files
committed
feat(describe): new implementation with prompt
1 parent 86062c2 commit 0cdd5cd

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

cmd/describe.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package cmd
22

33
import (
4+
"context"
45
"encoding/json"
56
"encoding/xml"
67
"errors"
78
"fmt"
89
"io"
910
"os"
1011

12+
"github.com/AlecAivazis/survey/v2"
1113
"github.com/ory/viper"
1214
"github.com/spf13/cobra"
1315
"gopkg.in/yaml.v2"
16+
1417

1518
"knative.dev/func/pkg/config"
1619
fn "knative.dev/func/pkg/functions"
@@ -66,7 +69,11 @@ func runDescribe(cmd *cobra.Command, args []string, newClient ClientFactory) (er
6669
if err != nil {
6770
return
6871
}
69-
// TODO cfg.Prompt()
72+
if cfg.Name == "" && cfg.Path == "" {
73+
if err := cfg.Prompt(newClient); err != nil {
74+
return err
75+
}
76+
}
7077

7178
client, done := newClient(ClientConfig{Verbose: cfg.Verbose})
7279
defer done()
@@ -133,6 +140,41 @@ func newDescribeConfig(cmd *cobra.Command, args []string) (cfg describeConfig, e
133140
return
134141
}
135142

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+
136178
// Output Formatting (serializers)
137179
// -------------------------------
138180

0 commit comments

Comments
 (0)