From 6f57ed1b9db0a500e945e305de450efdba3892cb Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 15 Apr 2021 12:22:32 -0700 Subject: [PATCH] input: support yaml and json inputs Signed-off-by: Andrea Luzzardi --- cmd/dagger/cmd/input/json.go | 33 +++++++++++ cmd/dagger/cmd/input/root.go | 2 + cmd/dagger/cmd/input/text.go | 2 +- cmd/dagger/cmd/input/yaml.go | 33 +++++++++++ cmd/dagger/cmd/root.go | 4 +- tests/cli.bats | 59 +++++++++++++++---- tests/cli/input/{ => artifact}/main.cue | 0 .../input/{ => artifact}/testdata/testfile | 0 tests/cli/input/simple/main.cue | 10 ++++ 9 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 cmd/dagger/cmd/input/json.go create mode 100644 cmd/dagger/cmd/input/yaml.go rename tests/cli/input/{ => artifact}/main.cue (100%) rename tests/cli/input/{ => artifact}/testdata/testfile (100%) create mode 100644 tests/cli/input/simple/main.cue diff --git a/cmd/dagger/cmd/input/json.go b/cmd/dagger/cmd/input/json.go new file mode 100644 index 00000000..b08b9210 --- /dev/null +++ b/cmd/dagger/cmd/input/json.go @@ -0,0 +1,33 @@ +package input + +import ( + "dagger.io/go/cmd/dagger/logger" + "dagger.io/go/dagger" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var jsonCmd = &cobra.Command{ + Use: "json TARGET VALUE", + Short: "Add a JSON 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()) + + updateDeploymentInput(ctx, args[0], dagger.JSONInput(args[1])) + }, +} + +func init() { + if err := viper.BindPFlags(jsonCmd.Flags()); err != nil { + panic(err) + } +} diff --git a/cmd/dagger/cmd/input/root.go b/cmd/dagger/cmd/input/root.go index d212ab67..30b01ece 100644 --- a/cmd/dagger/cmd/input/root.go +++ b/cmd/dagger/cmd/input/root.go @@ -22,6 +22,8 @@ func init() { containerCmd, secretCmd, textCmd, + jsonCmd, + yamlCmd, ) } diff --git a/cmd/dagger/cmd/input/text.go b/cmd/dagger/cmd/input/text.go index 10b04262..2b1e495e 100644 --- a/cmd/dagger/cmd/input/text.go +++ b/cmd/dagger/cmd/input/text.go @@ -9,7 +9,7 @@ import ( var textCmd = &cobra.Command{ Use: "text TARGET VALUE", - Short: "Add an input text", + Short: "Add a text input", Args: cobra.ExactArgs(2), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: diff --git a/cmd/dagger/cmd/input/yaml.go b/cmd/dagger/cmd/input/yaml.go new file mode 100644 index 00000000..1d3718d0 --- /dev/null +++ b/cmd/dagger/cmd/input/yaml.go @@ -0,0 +1,33 @@ +package input + +import ( + "dagger.io/go/cmd/dagger/logger" + "dagger.io/go/dagger" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var yamlCmd = &cobra.Command{ + Use: "yaml TARGET VALUE", + Short: "Add a YAML 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()) + + updateDeploymentInput(ctx, args[0], dagger.YAMLInput(args[1])) + }, +} + +func init() { + if err := viper.BindPFlags(yamlCmd.Flags()); err != nil { + panic(err) + } +} diff --git a/cmd/dagger/cmd/root.go b/cmd/dagger/cmd/root.go index 8fbfe21c..70a8883d 100644 --- a/cmd/dagger/cmd/root.go +++ b/cmd/dagger/cmd/root.go @@ -4,7 +4,7 @@ import ( "os" "strings" - inputCmd "dagger.io/go/cmd/dagger/cmd/input" + "dagger.io/go/cmd/dagger/cmd/input" "dagger.io/go/cmd/dagger/cmd/output" "dagger.io/go/cmd/dagger/cmd/plan" "dagger.io/go/cmd/dagger/logger" @@ -37,7 +37,7 @@ func init() { loginCmd, logoutCmd, plan.Cmd, - inputCmd.Cmd, + input.Cmd, output.Cmd, ) diff --git a/tests/cli.bats b/tests/cli.bats index d401a0a5..283ab510 100644 --- a/tests/cli.bats +++ b/tests/cli.bats @@ -109,21 +109,55 @@ setup() { assert_output --partial '"foo": "value"' } -@test "dagger input" { - "$DAGGER" new --plan-dir "$TESTDIR"/cli/input "input" +@test "dagger input text" { + "$DAGGER" new --plan-dir "$TESTDIR"/cli/input/simple "input" - # missing input - "$DAGGER" up -d "input" - run "$DAGGER" -l error query -d "input" + "$DAGGER" input -d "input" text "input" "my input" + "$DAGGER" up -d "input" + run "$DAGGER" -l error query -d "input" input + assert_success + assert_output '"my input"' + + "$DAGGER" input -d "input" text "nested.input" "nested input" + "$DAGGER" up -d "input" + run "$DAGGER" -l error query -d "input" nested assert_success assert_output '{ - "foo": "bar" + "input": "nested input" }' +} - # input dir - "$DAGGER" input -d "input" dir "source" "$TESTDIR"/cli/input/testdata - "$DAGGER" "${DAGGER_BINARY_ARGS[@]}" up -d "input" - "$DAGGER" up -d "input" +@test "dagger input json" { + "$DAGGER" new --plan-dir "$TESTDIR"/cli/input/simple "input" + + "$DAGGER" input -d "input" json "structured" '{"a": "foo", "b": 42}' + "$DAGGER" up -d "input" + run "$DAGGER" -l error query -d "input" structured + assert_success + assert_output '{ + "a": "foo", + "b": 42 +}' +} + +@test "dagger input yaml" { + "$DAGGER" new --plan-dir "$TESTDIR"/cli/input/simple "input" + + "$DAGGER" input -d "input" yaml "structured" '{"a": "foo", "b": 42}' + "$DAGGER" up -d "input" + run "$DAGGER" -l error query -d "input" structured + assert_success + assert_output '{ + "a": "foo", + "b": 42 +}' +} + +@test "dagger input dir" { + "$DAGGER" new --plan-dir "$TESTDIR"/cli/input/artifact "input" + + "$DAGGER" input -d "input" dir "source" "$TESTDIR"/cli/input/artifact/testdata + "$DAGGER" up -d "input" run "$DAGGER" -l error query -d "input" assert_success assert_output '{ @@ -131,8 +165,11 @@ setup() { "foo": "bar", "source": {} }' +} + +@test "dagger input git" { + "$DAGGER" new --plan-dir "$TESTDIR"/cli/input/artifact "input" - # input git "$DAGGER" input -d "input" git "source" https://github.com/samalba/dagger-test-simple.git "$DAGGER" up -d "input" run "$DAGGER" -l error query -d "input" diff --git a/tests/cli/input/main.cue b/tests/cli/input/artifact/main.cue similarity index 100% rename from tests/cli/input/main.cue rename to tests/cli/input/artifact/main.cue diff --git a/tests/cli/input/testdata/testfile b/tests/cli/input/artifact/testdata/testfile similarity index 100% rename from tests/cli/input/testdata/testfile rename to tests/cli/input/artifact/testdata/testfile diff --git a/tests/cli/input/simple/main.cue b/tests/cli/input/simple/main.cue new file mode 100644 index 00000000..347631ff --- /dev/null +++ b/tests/cli/input/simple/main.cue @@ -0,0 +1,10 @@ +package testing + +input: string + +nested: input: string + +structured: { + a: string + b: int +}