diff --git a/cmd/dagger/cmd/input/bool.go b/cmd/dagger/cmd/input/bool.go new file mode 100644 index 00000000..0dd1a691 --- /dev/null +++ b/cmd/dagger/cmd/input/bool.go @@ -0,0 +1,38 @@ +package input + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.dagger.io/dagger/cmd/dagger/logger" + "go.dagger.io/dagger/state" +) + +var boolCmd = &cobra.Command{ + Use: "bool ", + Short: "Add a boolean input", + Args: cobra.ExactArgs(2), + PreRun: func(cmd *cobra.Command, args []string) { + // Fix Viper bug for duplicate flags: + // https://github.com/spf13/viper/issues/233 + if err := viper.BindPFlags(cmd.Flags()); err != nil { + panic(err) + } + }, + Run: func(cmd *cobra.Command, args []string) { + lg := logger.New() + ctx := lg.WithContext(cmd.Context()) + + updateEnvironmentInput( + ctx, + cmd, + args[0], + state.BoolInput(readInput(ctx, args[1])), + ) + }, +} + +func init() { + if err := viper.BindPFlags(boolCmd.Flags()); err != nil { + panic(err) + } +} diff --git a/cmd/dagger/cmd/input/root.go b/cmd/dagger/cmd/input/root.go index c2e52bd4..e53448f8 100644 --- a/cmd/dagger/cmd/input/root.go +++ b/cmd/dagger/cmd/input/root.go @@ -31,6 +31,7 @@ func init() { jsonCmd, yamlCmd, listCmd, + boolCmd, unsetCmd, ) } diff --git a/state/input.go b/state/input.go index 6072f6fd..6f4500f9 100644 --- a/state/input.go +++ b/state/input.go @@ -35,6 +35,7 @@ type Input struct { JSON *jsonInput `yaml:"json,omitempty"` YAML *yamlInput `yaml:"yaml,omitempty"` File *fileInput `yaml:"file,omitempty"` + Bool *boolInput `yaml:"bool,omitempty"` } func (i Input) Compile(key string, state *State) (*compiler.Value, error) { @@ -55,6 +56,8 @@ func (i Input) Compile(key string, state *State) (*compiler.Value, error) { return i.YAML.Compile(key, state) case i.File != nil: return i.File.Compile(key, state) + case i.Bool != nil: + return i.Bool.Compile(key, state) default: return nil, fmt.Errorf("input has not been set") } @@ -201,6 +204,27 @@ func (i secretInput) PlainText() string { return string(i) } +// An input value encoded as Bool +func BoolInput(data string) Input { + i := boolInput(data) + return Input{ + Bool: &i, + } +} + +type boolInput string + +func (i boolInput) Compile(_ string, _ *State) (*compiler.Value, error) { + s := map[boolInput]struct{}{ + "true": {}, + "false": {}, + } + if _, ok := s[i]; ok { + return compiler.DecodeJSON("", []byte(i)) + } + return nil, fmt.Errorf("%q is not a valid boolean: ", i) +} + // An input value encoded as JSON func JSONInput(data string) Input { i := jsonInput(data) diff --git a/tests/cli.bats b/tests/cli.bats index dddbb986..1dc18b8e 100644 --- a/tests/cli.bats +++ b/tests/cli.bats @@ -369,6 +369,35 @@ setup() { "$DAGGER" -e "input-subdir-git" up } +@test "dagger input bool" { + "$DAGGER" init + + ## Test simple input git + dagger_new_with_plan "input-simple-bool" "$TESTDIR"/cli/input/bool + + # input git + "$DAGGER" -e "input-simple-bool" input list --show-optional + run "$DAGGER" -e "input-simple-bool" query first + assert_output 'false' + run "$DAGGER" -e "input-simple-bool" query second + assert_output '{}' + + "$DAGGER" -e "input-simple-bool" input bool first true + run "$DAGGER" -e "input-simple-bool" query first + assert_output 'true' + run "$DAGGER" -e "input-simple-bool" query second + assert_output 'true' + + "$DAGGER" -e "input-simple-bool" input bool first false + run "$DAGGER" -e "input-simple-bool" query first + assert_output 'false' + run "$DAGGER" -e "input-simple-bool" query second + assert_output '{}' + + run "$DAGGER" -e "input-simple-bool" input bool first Anything + assert_failure +} + @test "dagger input list" { "$DAGGER" init diff --git a/tests/cli/input/bool/main.cue b/tests/cli/input/bool/main.cue new file mode 100644 index 00000000..f89e9a1c --- /dev/null +++ b/tests/cli/input/bool/main.cue @@ -0,0 +1,11 @@ +package testing + +import ( + "alpha.dagger.io/dagger" +) + +first: dagger.#Input & {bool | *false} + +if first == true { + second: true +}