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)
| 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. |
| 16 | func 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 | |
| 47 | func displayDefault(d *pb.Argument) string { |
| 48 | if d.GetValue() == nil { |
nothing calls this directly
no test coverage detected