From d4c8350c08ee9032e25b900d2e2e8830efcc878d Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Mon, 12 Jul 2021 10:46:32 +0200 Subject: [PATCH] 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)