diff --git a/cmd/dagger/cmd/compute.go b/cmd/dagger/cmd/compute.go index 4151dc5e..033e5f38 100644 --- a/cmd/dagger/cmd/compute.go +++ b/cmd/dagger/cmd/compute.go @@ -41,7 +41,7 @@ var computeCmd = &cobra.Command{ for _, input := range viper.GetStringSlice("input-string") { parts := strings.SplitN(input, "=", 2) k, v := parts[0], parts[1] - err := st.AddInput(k, dagger.TextInput(v)) + err := st.SetInput(k, dagger.TextInput(v)) if err != nil { lg. Fatal(). @@ -54,7 +54,7 @@ var computeCmd = &cobra.Command{ for _, input := range viper.GetStringSlice("input-dir") { parts := strings.SplitN(input, "=", 2) k, v := parts[0], parts[1] - err := st.AddInput(k, dagger.DirInput(v, []string{})) + err := st.SetInput(k, dagger.DirInput(v, []string{})) if err != nil { lg. Fatal(). @@ -67,7 +67,7 @@ var computeCmd = &cobra.Command{ for _, input := range viper.GetStringSlice("input-git") { parts := strings.SplitN(input, "=", 2) k, v := parts[0], parts[1] - err := st.AddInput(k, dagger.GitInput(v, "", "")) + err := st.SetInput(k, dagger.GitInput(v, "", "")) if err != nil { lg. Fatal(). @@ -98,7 +98,7 @@ var computeCmd = &cobra.Command{ lg.Fatal().Msg("invalid json") } - err = st.AddInput("", dagger.JSONInput(string(content))) + err = st.SetInput("", dagger.JSONInput(string(content))) if err != nil { lg.Fatal().Err(err).Msg("failed to add input") } @@ -121,7 +121,7 @@ var computeCmd = &cobra.Command{ content = plaintext } - err = st.AddInput("", dagger.YAMLInput(string(content))) + err = st.SetInput("", dagger.YAMLInput(string(content))) if err != nil { lg.Fatal().Err(err).Msg("failed to add input") } diff --git a/cmd/dagger/cmd/input/root.go b/cmd/dagger/cmd/input/root.go index 57dd9500..d212ab67 100644 --- a/cmd/dagger/cmd/input/root.go +++ b/cmd/dagger/cmd/input/root.go @@ -34,7 +34,7 @@ func updateDeploymentInput(ctx context.Context, target string, input dagger.Inpu } st := common.GetCurrentDeploymentState(ctx, store) - st.AddInput(target, input) + st.SetInput(target, input) if err := store.UpdateDeployment(ctx, st, nil); err != nil { lg.Fatal().Err(err).Str("deploymentId", st.ID).Str("deploymentName", st.Name).Msg("cannot update deployment") diff --git a/cmd/dagger/cmd/new.go b/cmd/dagger/cmd/new.go index 764a933e..9c3e3966 100644 --- a/cmd/dagger/cmd/new.go +++ b/cmd/dagger/cmd/new.go @@ -102,6 +102,7 @@ func getPlanSource(ctx context.Context) dagger.Input { if planDir != "" { checkFirstSet() + src = dagger.DirInput(planDir, []string{"*.cue", "cue.mod"}) } diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index e935a5c7..8d75cc9f 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -31,7 +31,7 @@ var upCmd = &cobra.Command{ state := common.GetCurrentDeploymentState(ctx, store) // TODO: Implement options: --no-cache - common.DeploymentUp(ctx, state, false) + common.DeploymentUp(ctx, state, true) }, } diff --git a/dagger/deployment.go b/dagger/deployment.go index c4b6a64a..5dfd5dbd 100644 --- a/dagger/deployment.go +++ b/dagger/deployment.go @@ -40,7 +40,15 @@ type inputKV struct { Value Input `json:"value,omitempty"` } -func (s *DeploymentState) AddInput(key string, value Input) error { +func (s *DeploymentState) SetInput(key string, value Input) error { + for i, inp := range s.Inputs { + if inp.Key != key { + continue + } + // Remove existing inputs with the same key + s.Inputs = append(s.Inputs[:i], s.Inputs[i+1:]...) + } + s.Inputs = append(s.Inputs, inputKV{Key: key, Value: value}) return nil } diff --git a/dagger/input.go b/dagger/input.go index a00db28f..c27db8c4 100644 --- a/dagger/input.go +++ b/dagger/input.go @@ -3,6 +3,7 @@ package dagger import ( "encoding/json" "fmt" + "path/filepath" "dagger.io/go/dagger/compiler" ) @@ -65,6 +66,12 @@ func (i Input) Compile() (*compiler.Value, error) { // An input artifact loaded from a local directory func DirInput(path string, include []string) Input { + // resolve absolute path + path, err := filepath.Abs(path) + if err != nil { + panic(err) + } + return Input{ Type: InputTypeDir, Dir: &dirInput{ diff --git a/dagger/input_test.go b/dagger/input_test.go index c1089051..b959881d 100644 --- a/dagger/input_test.go +++ b/dagger/input_test.go @@ -10,13 +10,13 @@ func TestInputDir(t *testing.T) { st := &DeploymentState{ PlanSource: DirInput("/tmp/source", []string{}), } - require.NoError(t, st.AddInput("www.source", DirInput(".", []string{}))) + require.NoError(t, st.SetInput("www.source", DirInput("/", []string{}))) deployment, err := NewDeployment(st) require.NoError(t, err) localdirs := deployment.LocalDirs() require.Len(t, localdirs, 2) - require.Contains(t, localdirs, ".") + require.Contains(t, localdirs, "/") require.Contains(t, localdirs, "/tmp/source") } diff --git a/dagger/store_test.go b/dagger/store_test.go index abe7cf33..47449be8 100644 --- a/dagger/store_test.go +++ b/dagger/store_test.go @@ -62,7 +62,7 @@ func TestStoreLookupByPath(t *testing.T) { st := &DeploymentState{ Name: "test", } - require.NoError(t, st.AddInput("foo", DirInput("/test/path", []string{}))) + require.NoError(t, st.SetInput("foo", DirInput("/test/path", []string{}))) require.NoError(t, store.CreateDeployment(ctx, st)) // Lookup by path @@ -72,7 +72,7 @@ func TestStoreLookupByPath(t *testing.T) { require.Equal(t, st.ID, r.ID) // Add a new path - require.NoError(t, st.AddInput("bar", DirInput("/test/anotherpath", []string{}))) + require.NoError(t, st.SetInput("bar", DirInput("/test/anotherpath", []string{}))) require.NoError(t, store.UpdateDeployment(ctx, st, nil)) // Lookup by the previous path diff --git a/tests/cli/input/main.cue b/tests/cli/input/main.cue new file mode 100644 index 00000000..fd0f7b94 --- /dev/null +++ b/tests/cli/input/main.cue @@ -0,0 +1,25 @@ +package testing + +import ( + "dagger.io/llb" + "dagger.io/dagger" +) + +source: dagger.#Artifact +foo: "bar" + +bar: { + string + + #up: [ + llb.#FetchContainer & {ref: "busybox"}, + llb.#Exec & { + args: ["cp", "/source/testfile", "/out"] + mount: "/source": from: source + }, + llb.#Export & { + format: "string" + source: "/out" + }, + ] +} diff --git a/tests/cli/input/testdata/testfile b/tests/cli/input/testdata/testfile new file mode 100644 index 00000000..20495ffb --- /dev/null +++ b/tests/cli/input/testdata/testfile @@ -0,0 +1 @@ +thisisatest diff --git a/tests/test-cli.sh b/tests/test-cli.sh index af8a011e..276d50b1 100644 --- a/tests/test-cli.sh +++ b/tests/test-cli.sh @@ -9,7 +9,10 @@ test::cli() { test::cli::list "$dagger" test::cli::newdir "$dagger" + test::cli::newgit "$dagger" test::cli::query "$dagger" + test::cli::plan "$dagger" + test::cli::input "$dagger" } test::cli::list() { @@ -54,6 +57,27 @@ test::cli::newdir() { "$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "simple" -c } +test::cli::newgit() { + local dagger="$1" + + # Create temporary store + local DAGGER_STORE + DAGGER_STORE="$(mktemp -d -t dagger-store-XXXXXX)" + export DAGGER_STORE + + test::one "CLI: new git: --plan-git" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" new --plan-git https://github.com/samalba/dagger-test.git simple + + test::one "CLI: new git: verify plan can be upped" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" up -d "simple" + + test::one "CLI: new git: verify we have the right plan" --stdout='{ + foo: "value" + bar: "another value" +}' \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "simple" -c +} + test::cli::query() { local dagger="$1" @@ -80,3 +104,57 @@ test::cli::query() { test::one "CLI: query: non concrete" --exit=1 \ "$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "nonconcrete" -c } + +test::cli::plan() { + local dagger="$1" + + # Create temporary store + local DAGGER_STORE + DAGGER_STORE="$(mktemp -d -t dagger-store-XXXXXX)" + export DAGGER_STORE + + test::one "CLI: new" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" new --plan-dir "$d"/cli/simple simple + + test::one "CLI: plan dir" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" -d "simple" plan dir "$d"/cli/nonconcrete + + test::one "CLI: plan dir: query non-concrete" --exit=1 \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" -d "simple" query -c + + test::one "CLI: plan git" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" -d "simple" plan git https://github.com/samalba/dagger-test.git + + test::one "CLI: plan git: verify we have the right plan" --stdout='{ + foo: "value" + bar: "another value" +}' \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "simple" -c +} + +test::cli::input() { + local dagger="$1" + + # Create temporary store + local DAGGER_STORE + DAGGER_STORE="$(mktemp -d -t dagger-store-XXXXXX)" + export DAGGER_STORE + + test::one "CLI: new input" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" new --plan-dir "$d"/cli/input "input" + + test::one "CLI: up: missing input" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" up -d "input" --stdout='{"foo":"bar"}' + + test::one "CLI: input dir" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" input -d "input" dir "source" ./tests/cli/input/testdata + + test::one "CLI: up: input is set with input dir" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" up -d "input" --stdout='{"bar":"thisisatest\n","foo":"bar","source":{}}' + + test::one "CLI: input git" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" input -d "input" git "source" https://github.com/samalba/dagger-test-simple.git + + test::one "CLI: up: input is set with input git" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" up -d "input" --stdout='{"bar":"testgit\n","foo":"bar","source":{}}' +}