cmd: centralize code for buildkit client creation

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-07-12 10:46:32 +02:00
parent 84acad8706
commit d4c8350c08
9 changed files with 26 additions and 7 deletions

View File

@ -159,3 +159,15 @@ func ValueDocOneLine(val *compiler.Value) string {
} }
return strings.Join(docs, " ") 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
}

View File

@ -3,6 +3,7 @@ package input
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
) )
@ -22,7 +23,7 @@ var containerCmd = &cobra.Command{
lg := logger.New() lg := logger.New()
ctx := lg.WithContext(cmd.Context()) ctx := lg.WithContext(cmd.Context())
updateEnvironmentInput(ctx, args[0], state.DockerInput(args[1])) updateEnvironmentInput(ctx, common.NewClient(ctx), args[0], state.DockerInput(args[1]))
}, },
} }

View File

@ -43,7 +43,7 @@ var dirCmd = &cobra.Command{
p = "./" + p p = "./" + p
} }
updateEnvironmentInput(ctx, args[0], updateEnvironmentInput(ctx, common.NewClient(ctx), args[0],
state.DirInput( state.DirInput(
p, p,
viper.GetStringSlice("include"), viper.GetStringSlice("include"),

View File

@ -3,6 +3,7 @@ package input
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
) )
@ -32,7 +33,7 @@ var gitCmd = &cobra.Command{
subDir = args[3] 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))
}, },
} }

View File

@ -3,6 +3,7 @@ package input
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
) )
@ -24,6 +25,7 @@ var jsonCmd = &cobra.Command{
updateEnvironmentInput( updateEnvironmentInput(
ctx, ctx,
common.NewClient(ctx),
args[0], args[0],
state.JSONInput(readInput(ctx, args[1])), state.JSONInput(readInput(ctx, args[1])),
) )

View File

@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
"golang.org/x/term" "golang.org/x/term"
@ -43,6 +44,7 @@ var secretCmd = &cobra.Command{
updateEnvironmentInput( updateEnvironmentInput(
ctx, ctx,
common.NewClient(ctx),
args[0], args[0],
state.SecretInput(secret), state.SecretInput(secret),
) )

View File

@ -3,6 +3,7 @@ package input
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
) )
@ -24,6 +25,7 @@ var textCmd = &cobra.Command{
updateEnvironmentInput( updateEnvironmentInput(
ctx, ctx,
common.NewClient(ctx),
args[0], args[0],
state.TextInput(readInput(ctx, args[1])), state.TextInput(readInput(ctx, args[1])),
) )

View File

@ -3,6 +3,7 @@ package input
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
) )
@ -24,6 +25,7 @@ var yamlCmd = &cobra.Command{
updateEnvironmentInput( updateEnvironmentInput(
ctx, ctx,
common.NewClient(ctx),
args[0], args[0],
state.YAMLInput(readInput(ctx, args[1])), state.YAMLInput(readInput(ctx, args[1])),
) )

View File

@ -38,10 +38,7 @@ var upCmd = &cobra.Command{
workspace := common.CurrentWorkspace(ctx) workspace := common.CurrentWorkspace(ctx)
st := common.CurrentEnvironmentState(ctx, workspace) st := common.CurrentEnvironmentState(ctx, workspace)
cl, err := client.New(ctx, "", false) cl := common.NewClient(ctx)
if err != nil {
lg.Fatal().Err(err).Msg("unable to create client")
}
// check that all inputs are set // check that all inputs are set
checkInputs(ctx, cl, st) checkInputs(ctx, cl, st)