Merge pull request #808 from samalba/inputs-validation

Inputs validation
This commit is contained in:
Andrea Luzzardi 2021-07-12 12:18:37 +02:00 committed by GitHub
commit 9f907fea62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 66 additions and 23 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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]))
},
}

View File

@ -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"),

View File

@ -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))
},
}

View File

@ -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])),
)

View File

@ -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")
}

View File

@ -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),
)

View File

@ -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])),
)

View File

@ -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])),
)

View File

@ -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

View File

@ -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"