From 46ab44a69f0542cc2555f5b12c164bdb0a46f8e5 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 15 Apr 2021 12:47:30 -0700 Subject: [PATCH] input: support file/stdin source for text/json/yaml Signed-off-by: Andrea Luzzardi --- cmd/dagger/cmd/input/json.go | 10 ++++-- cmd/dagger/cmd/input/root.go | 35 ++++++++++++++++++++ cmd/dagger/cmd/input/text.go | 10 ++++-- cmd/dagger/cmd/input/yaml.go | 10 ++++-- tests/cli.bats | 38 ++++++++++++++++++++++ tests/cli/input/simple/testdata/input.json | 4 +++ tests/cli/input/simple/testdata/input.txt | 1 + tests/cli/input/simple/testdata/input.yaml | 2 ++ 8 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 tests/cli/input/simple/testdata/input.json create mode 100644 tests/cli/input/simple/testdata/input.txt create mode 100644 tests/cli/input/simple/testdata/input.yaml diff --git a/cmd/dagger/cmd/input/json.go b/cmd/dagger/cmd/input/json.go index b08b9210..0636a442 100644 --- a/cmd/dagger/cmd/input/json.go +++ b/cmd/dagger/cmd/input/json.go @@ -8,7 +8,7 @@ import ( ) var jsonCmd = &cobra.Command{ - Use: "json TARGET VALUE", + Use: "json [-f] ", Short: "Add a JSON input", Args: cobra.ExactArgs(2), PreRun: func(cmd *cobra.Command, args []string) { @@ -22,11 +22,17 @@ var jsonCmd = &cobra.Command{ lg := logger.New() ctx := lg.WithContext(cmd.Context()) - updateDeploymentInput(ctx, args[0], dagger.JSONInput(args[1])) + updateDeploymentInput( + ctx, + args[0], + dagger.JSONInput(readInput(ctx, args[1])), + ) }, } func init() { + jsonCmd.Flags().BoolP("file", "f", false, "Read value from file") + 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 30b01ece..07ad939e 100644 --- a/cmd/dagger/cmd/input/root.go +++ b/cmd/dagger/cmd/input/root.go @@ -2,11 +2,14 @@ package input import ( "context" + "io" + "os" "dagger.io/go/cmd/dagger/cmd/common" "dagger.io/go/dagger" "github.com/rs/zerolog/log" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // Cmd exposes the top-level command @@ -43,3 +46,35 @@ func updateDeploymentInput(ctx context.Context, target string, input dagger.Inpu } lg.Info().Str("deploymentId", st.ID).Str("deploymentName", st.Name).Msg("updated deployment") } + +func readInput(ctx context.Context, source string) string { + lg := log.Ctx(ctx) + + if !viper.GetBool("file") { + return source + } + + if source == "-" { + // stdin source + data, err := io.ReadAll(os.Stdin) + if err != nil { + lg. + Fatal(). + Err(err). + Msg("failed to read input from stdin") + } + return string(data) + } + + // file source + data, err := os.ReadFile(source) + if err != nil { + lg. + Fatal(). + Err(err). + Str("path", source). + Msg("failed to read input from file") + } + + return string(data) +} diff --git a/cmd/dagger/cmd/input/text.go b/cmd/dagger/cmd/input/text.go index 2b1e495e..4c6961ad 100644 --- a/cmd/dagger/cmd/input/text.go +++ b/cmd/dagger/cmd/input/text.go @@ -8,7 +8,7 @@ import ( ) var textCmd = &cobra.Command{ - Use: "text TARGET VALUE", + Use: "text [-f] ", Short: "Add a text input", Args: cobra.ExactArgs(2), PreRun: func(cmd *cobra.Command, args []string) { @@ -22,11 +22,17 @@ var textCmd = &cobra.Command{ lg := logger.New() ctx := lg.WithContext(cmd.Context()) - updateDeploymentInput(ctx, args[0], dagger.TextInput(args[1])) + updateDeploymentInput( + ctx, + args[0], + dagger.TextInput(readInput(ctx, args[1])), + ) }, } func init() { + textCmd.Flags().BoolP("file", "f", false, "Read value from file") + if err := viper.BindPFlags(textCmd.Flags()); err != nil { panic(err) } diff --git a/cmd/dagger/cmd/input/yaml.go b/cmd/dagger/cmd/input/yaml.go index 1d3718d0..d2862ffd 100644 --- a/cmd/dagger/cmd/input/yaml.go +++ b/cmd/dagger/cmd/input/yaml.go @@ -8,7 +8,7 @@ import ( ) var yamlCmd = &cobra.Command{ - Use: "yaml TARGET VALUE", + Use: "yaml [-f] ", Short: "Add a YAML input", Args: cobra.ExactArgs(2), PreRun: func(cmd *cobra.Command, args []string) { @@ -22,11 +22,17 @@ var yamlCmd = &cobra.Command{ lg := logger.New() ctx := lg.WithContext(cmd.Context()) - updateDeploymentInput(ctx, args[0], dagger.YAMLInput(args[1])) + updateDeploymentInput( + ctx, + args[0], + dagger.YAMLInput(readInput(ctx, args[1])), + ) }, } func init() { + yamlCmd.Flags().BoolP("file", "f", false, "Read value from file") + if err := viper.BindPFlags(yamlCmd.Flags()); err != nil { panic(err) } diff --git a/tests/cli.bats b/tests/cli.bats index 283ab510..907a3c45 100644 --- a/tests/cli.bats +++ b/tests/cli.bats @@ -112,12 +112,14 @@ setup() { @test "dagger input text" { "$DAGGER" new --plan-dir "$TESTDIR"/cli/input/simple "input" + # simple 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"' + # nested input "$DAGGER" input -d "input" text "nested.input" "nested input" "$DAGGER" up -d "input" run "$DAGGER" -l error query -d "input" nested @@ -125,6 +127,24 @@ setup() { assert_output '{ "input": "nested input" }' + + # file input + "$DAGGER" input -d "input" text "input" -f "$TESTDIR"/cli/input/simple/testdata/input.txt + "$DAGGER" up -d "input" + run "$DAGGER" -l error query -d "input" input + assert_success + assert_output '"from file\n"' + + # invalid file + run "$DAGGER" input -d "input" text "input" -f "$TESTDIR"/cli/input/simple/testdata/notexist + assert_failure + + # stdin input + echo -n "from stdin" | "$DAGGER" input -d "input" text "input" -f - + "$DAGGER" up -d "input" + run "$DAGGER" -l error query -d "input" input + assert_success + assert_output '"from stdin"' } @test "dagger input json" { @@ -138,6 +158,15 @@ setup() { "a": "foo", "b": 42 }' + + "$DAGGER" input -d "input" json "structured" -f "$TESTDIR"/cli/input/simple/testdata/input.json + "$DAGGER" up -d "input" + run "$DAGGER" -l error query -d "input" structured + assert_success + assert_output '{ + "a": "from file", + "b": 42 +}' } @test "dagger input yaml" { @@ -151,6 +180,15 @@ setup() { "a": "foo", "b": 42 }' + + "$DAGGER" input -d "input" yaml "structured" -f "$TESTDIR"/cli/input/simple/testdata/input.yaml + "$DAGGER" up -d "input" + run "$DAGGER" -l error query -d "input" structured + assert_success + assert_output '{ + "a": "from file", + "b": 42 +}' } @test "dagger input dir" { diff --git a/tests/cli/input/simple/testdata/input.json b/tests/cli/input/simple/testdata/input.json new file mode 100644 index 00000000..9e84fe81 --- /dev/null +++ b/tests/cli/input/simple/testdata/input.json @@ -0,0 +1,4 @@ +{ + "a": "from file", + "b": 42 +} diff --git a/tests/cli/input/simple/testdata/input.txt b/tests/cli/input/simple/testdata/input.txt new file mode 100644 index 00000000..8098d258 --- /dev/null +++ b/tests/cli/input/simple/testdata/input.txt @@ -0,0 +1 @@ +from file diff --git a/tests/cli/input/simple/testdata/input.yaml b/tests/cli/input/simple/testdata/input.yaml new file mode 100644 index 00000000..5d0b1ae1 --- /dev/null +++ b/tests/cli/input/simple/testdata/input.yaml @@ -0,0 +1,2 @@ +a: "from file" +b: 42