MCPcopy
hub / github.com/redspread/spread / InteractiveArgs

Function InteractiveArgs

pkg/data/parameter.go:16–45  ·  view source on GitHub ↗

InteractiveArgs hosts an interactive session using a Reader and Writer which prompts for input which is used to populate the provided field. If required is true then only fields without defaults will be prompted for.

(r io.ReadCloser, w io.Writer, field *pb.Field, required bool)

Source from the content-addressed store, hash-verified

14// InteractiveArgs hosts an interactive session using a Reader and Writer which prompts for input which is used to
15// populate the provided field. If required is true then only fields without defaults will be prompted for.
16func InteractiveArgs(r io.ReadCloser, w io.Writer, field *pb.Field, required bool) error {
17 param := field.GetParam()
18
19 defaultVal := param.GetDefault()
20 // don't prompt if only checking for required and has default
21 if required && defaultVal != nil {
22 return nil
23 }
24
25 fmt.Fprintln(w, "Name: ", param.Name)
26 fmt.Fprintln(w, "Prompt: ", param.Prompt)
27 fmt.Fprint(w, "Input: ", displayDefault(defaultVal))
28 reader := bufio.NewReader(r)
29 text, err := reader.ReadString('\n')
30 if err != nil {
31 return err
32 }
33
34 // use default if no input given
35 args := []*pb.Argument{defaultVal}
36 if len(text) > 1 {
37 _, str := field.GetValue().(*pb.Field_Str)
38 args, err = ParseArguments(text[:len(text)-1], str)
39 if err != nil {
40 return err
41 }
42 }
43
44 return ApplyArguments(field, args...)
45}
46
47func displayDefault(d *pb.Argument) string {
48 if d.GetValue() == nil {

Callers

nothing calls this directly

Calls 6

displayDefaultFunction · 0.85
ParseArgumentsFunction · 0.85
ApplyArgumentsFunction · 0.85
GetParamMethod · 0.80
GetDefaultMethod · 0.80
GetValueMethod · 0.45

Tested by

no test coverage detected