cmd: refacto client creation to use only one (fixes #798)

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-07-09 17:15:46 +02:00
parent e40307af29
commit 84acad8706
3 changed files with 18 additions and 19 deletions

View File

@ -84,14 +84,10 @@ func CurrentEnvironmentState(ctx context.Context, workspace *state.Workspace) *s
} }
// Re-compute an environment (equivalent to `dagger up`). // 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) lg := log.Ctx(ctx)
c, err := client.New(ctx, "", noCache) result, err := cl.Do(ctx, state, func(ctx context.Context, environment *environment.Environment, s solver.Solver) error {
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 {
log.Ctx(ctx).Debug().Msg("bringing environment up") log.Ctx(ctx).Debug().Msg("bringing environment up")
return environment.Up(ctx, s) return environment.Up(ctx, s)
}) })

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"go.dagger.io/dagger/client"
"go.dagger.io/dagger/cmd/dagger/cmd/common" "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/compiler" "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() v := compiler.NewValue()
if err := v.FillPath(cue.MakePath(), environment.Plan()); err != nil { if err := v.FillPath(cue.MakePath(), environment.Plan()); err != nil {

View File

@ -38,10 +38,15 @@ var upCmd = &cobra.Command{
workspace := common.CurrentWorkspace(ctx) workspace := common.CurrentWorkspace(ctx)
st := common.CurrentEnvironmentState(ctx, workspace) st := common.CurrentEnvironmentState(ctx, workspace)
// check that all inputs are set cl, err := client.New(ctx, "", false)
checkInputs(ctx, st) 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() st.Computed = result.Computed().JSON().PrettyString()
if err := workspace.Save(ctx, st); err != nil { 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) lg := log.Ctx(ctx)
warnOnly := viper.GetBool("force") 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{} 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) inputs, err := env.ScanInputs(ctx, true)
if err != nil { if err != nil {
return err return err