From 84acad870684cfe1c7bb1e83bb3d14d01502c9ba Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Fri, 9 Jul 2021 17:15:46 +0200 Subject: [PATCH 1/5] cmd: refacto client creation to use only one (fixes #798) Signed-off-by: Sam Alba --- cmd/dagger/cmd/common/common.go | 8 ++------ cmd/dagger/cmd/compute.go | 7 ++++++- cmd/dagger/cmd/up.go | 22 ++++++++++------------ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/cmd/dagger/cmd/common/common.go b/cmd/dagger/cmd/common/common.go index 29a8865a..df55ca8a 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) }) 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/up.go b/cmd/dagger/cmd/up.go index 8acd1210..bafe585e 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -38,10 +38,15 @@ var upCmd = &cobra.Command{ workspace := common.CurrentWorkspace(ctx) st := common.CurrentEnvironmentState(ctx, workspace) - // check that all inputs are set - checkInputs(ctx, st) + cl, err := client.New(ctx, "", false) + if err != nil { + lg.Fatal().Err(err).Msg("unable to create client") + } - 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 +57,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 From d4c8350c08ee9032e25b900d2e2e8830efcc878d Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 12 Jul 2021 10:46:32 +0200 Subject: [PATCH 2/5] cmd: centralize code for buildkit client creation Signed-off-by: Sam Alba --- cmd/dagger/cmd/common/common.go | 12 ++++++++++++ cmd/dagger/cmd/input/container.go | 3 ++- cmd/dagger/cmd/input/dir.go | 2 +- cmd/dagger/cmd/input/git.go | 3 ++- cmd/dagger/cmd/input/json.go | 2 ++ cmd/dagger/cmd/input/secret.go | 2 ++ cmd/dagger/cmd/input/text.go | 2 ++ cmd/dagger/cmd/input/yaml.go | 2 ++ cmd/dagger/cmd/up.go | 5 +---- 9 files changed, 26 insertions(+), 7 deletions(-) diff --git a/cmd/dagger/cmd/common/common.go b/cmd/dagger/cmd/common/common.go index df55ca8a..271b82ce 100644 --- a/cmd/dagger/cmd/common/common.go +++ b/cmd/dagger/cmd/common/common.go @@ -159,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/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/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 bafe585e..8f6b474b 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -38,10 +38,7 @@ var upCmd = &cobra.Command{ workspace := common.CurrentWorkspace(ctx) st := common.CurrentEnvironmentState(ctx, workspace) - cl, err := client.New(ctx, "", false) - if err != nil { - lg.Fatal().Err(err).Msg("unable to create client") - } + cl := common.NewClient(ctx) // check that all inputs are set checkInputs(ctx, cl, st) From b37db8eedb6961ebb9ba64143865f22c4bd5b53a Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 12 Jul 2021 10:46:48 +0200 Subject: [PATCH 3/5] cmd: implemented input validation when updating environment Signed-off-by: Sam Alba --- cmd/dagger/cmd/input/root.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/dagger/cmd/input/root.go b/cmd/dagger/cmd/input/root.go index ef1ae3cf..c681db4e 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,26 @@ 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 { + _, 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") } From 2e883ae47cb9e0d47613e1cf5d7a4af5105b9cb0 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 12 Jul 2021 10:52:36 +0200 Subject: [PATCH 4/5] test: implemented test for invalid input Signed-off-by: Sam Alba --- tests/cli.bats | 8 ++++++++ 1 file changed, 8 insertions(+) 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" From f726a4b84de877b3261a7d4c459ba8943b366633 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 12 Jul 2021 11:53:20 +0200 Subject: [PATCH 5/5] cmd: added comment for input validation (per Andrea's pr comment) Signed-off-by: Sam Alba --- cmd/dagger/cmd/input/root.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/dagger/cmd/input/root.go b/cmd/dagger/cmd/input/root.go index c681db4e..8c32d613 100644 --- a/cmd/dagger/cmd/input/root.go +++ b/cmd/dagger/cmd/input/root.go @@ -44,6 +44,7 @@ func updateEnvironmentInput(ctx context.Context, cl *client.Client, target strin 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