Merge pull request #332 from dagger/dagger-inputs
support yaml and json inputs
This commit is contained in:
commit
14ca028918
39
cmd/dagger/cmd/input/json.go
Normal file
39
cmd/dagger/cmd/input/json.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
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> [-f] <VALUE|PATH>",
|
||||||
|
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(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)
|
||||||
|
}
|
||||||
|
}
|
@ -2,11 +2,14 @@ package input
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
"dagger.io/go/cmd/dagger/cmd/common"
|
"dagger.io/go/cmd/dagger/cmd/common"
|
||||||
"dagger.io/go/dagger"
|
"dagger.io/go/dagger"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cmd exposes the top-level command
|
// Cmd exposes the top-level command
|
||||||
@ -22,6 +25,8 @@ func init() {
|
|||||||
containerCmd,
|
containerCmd,
|
||||||
secretCmd,
|
secretCmd,
|
||||||
textCmd,
|
textCmd,
|
||||||
|
jsonCmd,
|
||||||
|
yamlCmd,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,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")
|
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)
|
||||||
|
}
|
||||||
|
@ -8,8 +8,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var textCmd = &cobra.Command{
|
var textCmd = &cobra.Command{
|
||||||
Use: "text TARGET VALUE",
|
Use: "text <TARGET> [-f] <VALUE|PATH>",
|
||||||
Short: "Add an input text",
|
Short: "Add a text input",
|
||||||
Args: cobra.ExactArgs(2),
|
Args: cobra.ExactArgs(2),
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
// Fix Viper bug for duplicate flags:
|
// Fix Viper bug for duplicate flags:
|
||||||
@ -22,11 +22,17 @@ var textCmd = &cobra.Command{
|
|||||||
lg := logger.New()
|
lg := logger.New()
|
||||||
ctx := lg.WithContext(cmd.Context())
|
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() {
|
func init() {
|
||||||
|
textCmd.Flags().BoolP("file", "f", false, "Read value from file")
|
||||||
|
|
||||||
if err := viper.BindPFlags(textCmd.Flags()); err != nil {
|
if err := viper.BindPFlags(textCmd.Flags()); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
39
cmd/dagger/cmd/input/yaml.go
Normal file
39
cmd/dagger/cmd/input/yaml.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
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> [-f] <VALUE|PATH>",
|
||||||
|
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(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)
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"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/output"
|
||||||
"dagger.io/go/cmd/dagger/cmd/plan"
|
"dagger.io/go/cmd/dagger/cmd/plan"
|
||||||
"dagger.io/go/cmd/dagger/logger"
|
"dagger.io/go/cmd/dagger/logger"
|
||||||
@ -37,7 +37,7 @@ func init() {
|
|||||||
loginCmd,
|
loginCmd,
|
||||||
logoutCmd,
|
logoutCmd,
|
||||||
plan.Cmd,
|
plan.Cmd,
|
||||||
inputCmd.Cmd,
|
input.Cmd,
|
||||||
output.Cmd,
|
output.Cmd,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -109,21 +109,93 @@ setup() {
|
|||||||
assert_output --partial '"foo": "value"'
|
assert_output --partial '"foo": "value"'
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "dagger input" {
|
@test "dagger input text" {
|
||||||
"$DAGGER" new --plan-dir "$TESTDIR"/cli/input "input"
|
"$DAGGER" new --plan-dir "$TESTDIR"/cli/input/simple "input"
|
||||||
|
|
||||||
# missing input
|
# simple input
|
||||||
"$DAGGER" up -d "input"
|
"$DAGGER" input -d "input" text "input" "my input"
|
||||||
run "$DAGGER" -l error query -d "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
|
||||||
assert_success
|
assert_success
|
||||||
assert_output '{
|
assert_output '{
|
||||||
"foo": "bar"
|
"input": "nested input"
|
||||||
}'
|
}'
|
||||||
|
|
||||||
# input dir
|
# file input
|
||||||
"$DAGGER" input -d "input" dir "source" "$TESTDIR"/cli/input/testdata
|
"$DAGGER" input -d "input" text "input" -f "$TESTDIR"/cli/input/simple/testdata/input.txt
|
||||||
"$DAGGER" "${DAGGER_BINARY_ARGS[@]}" up -d "input"
|
"$DAGGER" up -d "input"
|
||||||
"$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" {
|
||||||
|
"$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
|
||||||
|
}'
|
||||||
|
|
||||||
|
"$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" {
|
||||||
|
"$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
|
||||||
|
}'
|
||||||
|
|
||||||
|
"$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" {
|
||||||
|
"$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"
|
run "$DAGGER" -l error query -d "input"
|
||||||
assert_success
|
assert_success
|
||||||
assert_output '{
|
assert_output '{
|
||||||
@ -131,8 +203,11 @@ setup() {
|
|||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
"source": {}
|
"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" input -d "input" git "source" https://github.com/samalba/dagger-test-simple.git
|
||||||
"$DAGGER" up -d "input"
|
"$DAGGER" up -d "input"
|
||||||
run "$DAGGER" -l error query -d "input"
|
run "$DAGGER" -l error query -d "input"
|
||||||
|
10
tests/cli/input/simple/main.cue
Normal file
10
tests/cli/input/simple/main.cue
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package testing
|
||||||
|
|
||||||
|
input: string
|
||||||
|
|
||||||
|
nested: input: string
|
||||||
|
|
||||||
|
structured: {
|
||||||
|
a: string
|
||||||
|
b: int
|
||||||
|
}
|
4
tests/cli/input/simple/testdata/input.json
vendored
Normal file
4
tests/cli/input/simple/testdata/input.json
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"a": "from file",
|
||||||
|
"b": 42
|
||||||
|
}
|
1
tests/cli/input/simple/testdata/input.txt
vendored
Normal file
1
tests/cli/input/simple/testdata/input.txt
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
from file
|
2
tests/cli/input/simple/testdata/input.yaml
vendored
Normal file
2
tests/cli/input/simple/testdata/input.yaml
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
a: "from file"
|
||||||
|
b: 42
|
Reference in New Issue
Block a user