diff --git a/cmd/dagger/cmd/common/common.go b/cmd/dagger/cmd/common/common.go index 29a8865a..271b82ce 100644 --- a/cmd/dagger/cmd/common/common.go +++ b/cmd/dagger/cmd/common/common.go @@ -84,14 +84,10 @@ func CurrentEnvironmentState(ctx context.Context, workspace *state.Workspace) *s } // Re-compute an environment (equivalent to `dagger up`). -func EnvironmentUp(ctx context.Context, state *state.State, noCache bool) *environment.Environment { +func EnvironmentUp(ctx context.Context, cl *client.Client, state *state.State, noCache bool) *environment.Environment { lg := log.Ctx(ctx) - c, err := client.New(ctx, "", noCache) - if err != nil { - lg.Fatal().Err(err).Msg("unable to create client") - } - result, err := c.Do(ctx, state, func(ctx context.Context, environment *environment.Environment, s solver.Solver) error { + result, err := cl.Do(ctx, state, func(ctx context.Context, environment *environment.Environment, s solver.Solver) error { log.Ctx(ctx).Debug().Msg("bringing environment up") return environment.Up(ctx, s) }) @@ -163,3 +159,15 @@ func ValueDocOneLine(val *compiler.Value) string { } return strings.Join(docs, " ") } + +// NewClient creates a new client +func NewClient(ctx context.Context) *client.Client { + lg := log.Ctx(ctx) + + cl, err := client.New(ctx, "", false) + if err != nil { + lg.Fatal().Err(err).Msg("unable to create client") + } + + return cl +} diff --git a/cmd/dagger/cmd/compute.go b/cmd/dagger/cmd/compute.go index b0e9829d..cdc2e2da 100644 --- a/cmd/dagger/cmd/compute.go +++ b/cmd/dagger/cmd/compute.go @@ -8,6 +8,7 @@ import ( "strings" "cuelang.org/go/cue" + "go.dagger.io/dagger/client" "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/compiler" @@ -163,7 +164,11 @@ var computeCmd = &cobra.Command{ } } - environment := common.EnvironmentUp(ctx, st, viper.GetBool("no-cache")) + cl, err := client.New(ctx, "", false) + if err != nil { + lg.Fatal().Err(err).Msg("unable to create client") + } + environment := common.EnvironmentUp(ctx, cl, st, viper.GetBool("no-cache")) v := compiler.NewValue() if err := v.FillPath(cue.MakePath(), environment.Plan()); err != nil { diff --git a/cmd/dagger/cmd/input/container.go b/cmd/dagger/cmd/input/container.go index c9da5d41..527d8fc9 100644 --- a/cmd/dagger/cmd/input/container.go +++ b/cmd/dagger/cmd/input/container.go @@ -3,6 +3,7 @@ package input import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/state" ) @@ -22,7 +23,7 @@ var containerCmd = &cobra.Command{ lg := logger.New() ctx := lg.WithContext(cmd.Context()) - updateEnvironmentInput(ctx, args[0], state.DockerInput(args[1])) + updateEnvironmentInput(ctx, common.NewClient(ctx), args[0], state.DockerInput(args[1])) }, } diff --git a/cmd/dagger/cmd/input/dir.go b/cmd/dagger/cmd/input/dir.go index 4ecbe5c0..fb5b998c 100644 --- a/cmd/dagger/cmd/input/dir.go +++ b/cmd/dagger/cmd/input/dir.go @@ -43,7 +43,7 @@ var dirCmd = &cobra.Command{ p = "./" + p } - updateEnvironmentInput(ctx, args[0], + updateEnvironmentInput(ctx, common.NewClient(ctx), args[0], state.DirInput( p, viper.GetStringSlice("include"), diff --git a/cmd/dagger/cmd/input/git.go b/cmd/dagger/cmd/input/git.go index 4fdd5898..5e6e582d 100644 --- a/cmd/dagger/cmd/input/git.go +++ b/cmd/dagger/cmd/input/git.go @@ -3,6 +3,7 @@ package input import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/state" ) @@ -32,7 +33,7 @@ var gitCmd = &cobra.Command{ subDir = args[3] } - updateEnvironmentInput(ctx, args[0], state.GitInput(args[1], ref, subDir)) + updateEnvironmentInput(ctx, common.NewClient(ctx), args[0], state.GitInput(args[1], ref, subDir)) }, } diff --git a/cmd/dagger/cmd/input/json.go b/cmd/dagger/cmd/input/json.go index 03f5ec9d..7b50513d 100644 --- a/cmd/dagger/cmd/input/json.go +++ b/cmd/dagger/cmd/input/json.go @@ -3,6 +3,7 @@ package input import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/state" ) @@ -24,6 +25,7 @@ var jsonCmd = &cobra.Command{ updateEnvironmentInput( ctx, + common.NewClient(ctx), args[0], state.JSONInput(readInput(ctx, args[1])), ) diff --git a/cmd/dagger/cmd/input/root.go b/cmd/dagger/cmd/input/root.go index ef1ae3cf..8c32d613 100644 --- a/cmd/dagger/cmd/input/root.go +++ b/cmd/dagger/cmd/input/root.go @@ -8,7 +8,10 @@ import ( "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" + "go.dagger.io/dagger/client" "go.dagger.io/dagger/cmd/dagger/cmd/common" + "go.dagger.io/dagger/environment" + "go.dagger.io/dagger/solver" "go.dagger.io/dagger/state" ) @@ -32,13 +35,27 @@ func init() { ) } -func updateEnvironmentInput(ctx context.Context, target string, input state.Input) { +func updateEnvironmentInput(ctx context.Context, cl *client.Client, target string, input state.Input) { lg := log.Ctx(ctx) workspace := common.CurrentWorkspace(ctx) st := common.CurrentEnvironmentState(ctx, workspace) + st.SetInput(target, input) + _, err := cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error { + // the inputs are set, check for cue errors by scanning all the inputs + _, err := env.ScanInputs(ctx, true) + if err != nil { + return err + } + return nil + }) + + if err != nil { + lg.Fatal().Err(err).Str("environment", st.Name).Msg("invalid input") + } + if err := workspace.Save(ctx, st); err != nil { lg.Fatal().Err(err).Str("environment", st.Name).Msg("cannot update environment") } diff --git a/cmd/dagger/cmd/input/secret.go b/cmd/dagger/cmd/input/secret.go index 19cc0167..6eca0935 100644 --- a/cmd/dagger/cmd/input/secret.go +++ b/cmd/dagger/cmd/input/secret.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/state" "golang.org/x/term" @@ -43,6 +44,7 @@ var secretCmd = &cobra.Command{ updateEnvironmentInput( ctx, + common.NewClient(ctx), args[0], state.SecretInput(secret), ) diff --git a/cmd/dagger/cmd/input/text.go b/cmd/dagger/cmd/input/text.go index d5cfbe10..d154a4d1 100644 --- a/cmd/dagger/cmd/input/text.go +++ b/cmd/dagger/cmd/input/text.go @@ -3,6 +3,7 @@ package input import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/state" ) @@ -24,6 +25,7 @@ var textCmd = &cobra.Command{ updateEnvironmentInput( ctx, + common.NewClient(ctx), args[0], state.TextInput(readInput(ctx, args[1])), ) diff --git a/cmd/dagger/cmd/input/yaml.go b/cmd/dagger/cmd/input/yaml.go index db1b0025..78495617 100644 --- a/cmd/dagger/cmd/input/yaml.go +++ b/cmd/dagger/cmd/input/yaml.go @@ -3,6 +3,7 @@ package input import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/state" ) @@ -24,6 +25,7 @@ var yamlCmd = &cobra.Command{ updateEnvironmentInput( ctx, + common.NewClient(ctx), args[0], state.YAMLInput(readInput(ctx, args[1])), ) diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index 8acd1210..8f6b474b 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -38,10 +38,12 @@ var upCmd = &cobra.Command{ workspace := common.CurrentWorkspace(ctx) st := common.CurrentEnvironmentState(ctx, workspace) - // check that all inputs are set - checkInputs(ctx, st) + cl := common.NewClient(ctx) - result := common.EnvironmentUp(ctx, st, viper.GetBool("no-cache")) + // check that all inputs are set + checkInputs(ctx, cl, st) + + result := common.EnvironmentUp(ctx, cl, st, viper.GetBool("no-cache")) st.Computed = result.Computed().JSON().PrettyString() if err := workspace.Save(ctx, st); err != nil { @@ -52,19 +54,12 @@ var upCmd = &cobra.Command{ }, } -func checkInputs(ctx context.Context, st *state.State) { +func checkInputs(ctx context.Context, cl *client.Client, st *state.State) { lg := log.Ctx(ctx) warnOnly := viper.GetBool("force") - // FIXME: find a way to merge this with the EnvironmentUp client to avoid - // creating the client + solver twice - c, err := client.New(ctx, "", false) - if err != nil { - lg.Fatal().Err(err).Msg("unable to create client") - } - notConcreteInputs := []*compiler.Value{} - _, err = c.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error { + _, err := cl.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error { inputs, err := env.ScanInputs(ctx, true) if err != nil { return err diff --git a/tests/cli.bats b/tests/cli.bats index b696c8ac..01041356 100644 --- a/tests/cli.bats +++ b/tests/cli.bats @@ -137,6 +137,14 @@ setup() { dagger_new_with_plan input "$TESTDIR"/cli/input/simple + # wrong input type + run "$DAGGER" input -e "input" secret "input" "my input" + assert_failure + + # invalid input + run "$DAGGER" input -e "input" secret "input.notexist" "my input" + assert_failure + # simple input "$DAGGER" input -e "input" text "input" "my input" "$DAGGER" up -e "input"