Skip to content

Commit

Permalink
Add --no-input flag
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Oct 20, 2023
1 parent e5f2da0 commit 75c2e03
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/cli/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"context"
"errors"
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -129,6 +130,22 @@ func runExecute(cmd *cobra.Command, opts executeOptions) {
// Filter out variables which don't have value yet
varsWithoutValues := recipeutil.FilterVariablesWithoutValues(re.Variables, values)
if len(varsWithoutValues) > 0 {
if opts.NoInput {
var errMsg string
if len(varsWithoutValues) == 1 {
errMsg = fmt.Sprintf("value for variable %s is", varsWithoutValues[0].Name)
} else {
vars := make([]string, len(varsWithoutValues))
for i, v := range varsWithoutValues {
vars[i] = v.Name
}
errMsg = fmt.Sprintf("values for variables [%s] are", strings.Join(vars, ","))
}

cmd.PrintErrf("Error: %s missing and `--no-input` flag was set to true\n", errMsg)
return
}

promptedValues, err := survey.PromptUserForValues(cmd.InOrStdin(), cmd.OutOrStdout(), varsWithoutValues, values)
if err != nil {
if errors.Is(err, survey.ErrUserAborted) {
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/option/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type Values struct {
ReuseSauceValues bool
NoInput bool
CSVDelimiter rune
Flags []string

Expand All @@ -18,6 +19,7 @@ func (opts *Values) ApplyFlags(fs *pflag.FlagSet) {
fs.StringArrayVarP(&opts.Flags, "set", "s", []string{}, "Predefine values to be used in the templates. Example: `--set \"MY_VAR=foo\"`")
fs.StringVar(&opts.delimiter, "delimiter", ",", "Delimiter used when setting table variables")
fs.BoolVarP(&opts.ReuseSauceValues, "reuse-sauce-values", "r", false, "By default each sauce has their own set of values even if the variable names are same in both recipes. Setting this to `true` will reuse previous sauce values if the variable name match")
fs.BoolVar(&opts.NoInput, "no-input", false, "If set to true, the program will exit with an error code if it needs to wait for any user input. This is useful when running the program in CI/CD environment")
}

func (opts *Values) Parse() error {
Expand Down
16 changes: 16 additions & 0 deletions internal/cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) {
}

if len(varsWithoutValues) > 0 {
if opts.NoInput {
var errMsg string
if len(varsWithoutValues) == 1 {
errMsg = fmt.Sprintf("value for variable %s is", varsWithoutValues[0].Name)
} else {
vars := make([]string, len(varsWithoutValues))
for i, v := range varsWithoutValues {
vars[i] = v.Name
}
errMsg = fmt.Sprintf("values for variables [%s] are", strings.Join(vars, ","))
}

cmd.PrintErrf("Error: %s missing and `--no-input` flag was set to true\n", errMsg)
return
}

promptedValues, err := survey.PromptUserForValues(cmd.InOrStdin(), cmd.OutOrStdout(), varsWithoutValues, predefinedValues)
if err != nil {
if !errors.Is(err, survey.ErrUserAborted) {
Expand Down

0 comments on commit 75c2e03

Please sign in to comment.