dagger input bool implementation

Signed-off-by: Guillaume de Rouville <guillaume.derouville@gmail.com>
This commit is contained in:
Guillaume de Rouville 2021-08-24 16:00:53 +02:00
parent 36d0ebd863
commit 6fe0ec367a
5 changed files with 103 additions and 0 deletions

View File

@ -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 <TARGET> <true|false>",
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)
}
}

View File

@ -31,6 +31,7 @@ func init() {
jsonCmd,
yamlCmd,
listCmd,
boolCmd,
unsetCmd,
)
}

View File

@ -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: <true|false>", i)
}
// An input value encoded as JSON
func JSONInput(data string) Input {
i := jsonInput(data)

View File

@ -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

View File

@ -0,0 +1,11 @@
package testing
import (
"alpha.dagger.io/dagger"
)
first: dagger.#Input & {bool | *false}
if first == true {
second: true
}