Merge pull request #514 from samalba/up-check-input

Up check input
This commit is contained in:
Andrea Luzzardi 2021-05-27 13:31:35 -07:00 committed by GitHub
commit 792dcacdff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 7 deletions

View File

@ -47,7 +47,10 @@ var listCmd = &cobra.Command{
} }
_, err = c.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error { _, err = c.Do(ctx, st, func(ctx context.Context, env *environment.Environment, s solver.Solver) error {
inputs := env.ScanInputs(ctx) inputs, err := env.ScanInputs(ctx, false)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintln(w, "Input\tType\tValue\tSet by user\tDescription") fmt.Fprintln(w, "Input\tType\tValue\tSet by user\tDescription")

View File

@ -1,9 +1,20 @@
package cmd package cmd
import ( import (
"context"
"os"
"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/environment"
"go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state"
"golang.org/x/term"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -25,6 +36,10 @@ 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
checkInputs(ctx, st)
result := common.EnvironmentUp(ctx, st, viper.GetBool("no-cache")) result := common.EnvironmentUp(ctx, st, viper.GetBool("no-cache"))
st.Computed = result.Computed().JSON().PrettyString() st.Computed = result.Computed().JSON().PrettyString()
@ -34,8 +49,53 @@ var upCmd = &cobra.Command{
}, },
} }
func checkInputs(ctx context.Context, st *state.State) {
lg := log.Ctx(ctx)
warnOnly := viper.GetBool("force") || !term.IsTerminal(int(os.Stdout.Fd()))
// 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 {
inputs, err := env.ScanInputs(ctx, true)
if err != nil {
return err
}
for _, i := range inputs {
if i.IsConcreteR(cue.Optional(true)) != nil {
notConcreteInputs = append(notConcreteInputs, i)
}
}
return nil
})
if err != nil {
lg.Fatal().Err(err).Msg("failed to query environment")
}
for _, i := range notConcreteInputs {
if warnOnly {
lg.Warn().Str("input", i.Path().String()).Msg("required input is missing")
} else {
lg.Error().Str("input", i.Path().String()).Msg("required input is missing")
}
}
if !warnOnly && len(notConcreteInputs) > 0 {
lg.Fatal().Int("missing", len(notConcreteInputs)).Msg("some required inputs are not set, please re-run with `--force` if you think it's a mistake")
}
}
func init() { func init() {
upCmd.Flags().Bool("no-cache", false, "Disable all run cache") upCmd.Flags().Bool("no-cache", false, "Disable all run cache")
upCmd.Flags().BoolP("force", "f", false, "Force up, disable inputs check")
if err := viper.BindPFlags(upCmd.Flags()); err != nil { if err := viper.BindPFlags(upCmd.Flags()); err != nil {
panic(err) panic(err)

View File

@ -165,9 +165,9 @@ func (e *Environment) LocalDirs() map[string]string {
return dirs return dirs
} }
// Up missing values in environment configuration, and write them to state. // prepare initializes the Environment with inputs and plan code
func (e *Environment) Up(ctx context.Context, s solver.Solver) error { func (e *Environment) prepare(ctx context.Context) (*compiler.Value, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "environment.Up") span, _ := opentracing.StartSpanFromContext(ctx, "environment.Prepare")
defer span.Finish() defer span.Finish()
// Reset the computed values // Reset the computed values
@ -175,9 +175,23 @@ func (e *Environment) Up(ctx context.Context, s solver.Solver) error {
src := compiler.NewValue() src := compiler.NewValue()
if err := src.FillPath(cue.MakePath(), e.plan); err != nil { if err := src.FillPath(cue.MakePath(), e.plan); err != nil {
return err return nil, err
} }
if err := src.FillPath(cue.MakePath(), e.input); err != nil { if err := src.FillPath(cue.MakePath(), e.input); err != nil {
return nil, err
}
return src, nil
}
// Up missing values in environment configuration, and write them to state.
func (e *Environment) Up(ctx context.Context, s solver.Solver) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "environment.Up")
defer span.Finish()
// Set user inputs and plan code
src, err := e.prepare(ctx)
if err != nil {
return err return err
} }
@ -294,6 +308,17 @@ func newPipelineRunner(computed *compiler.Value, s solver.Solver) cueflow.Runner
}) })
} }
func (e *Environment) ScanInputs(ctx context.Context) []*compiler.Value { func (e *Environment) ScanInputs(ctx context.Context, mergeUserInputs bool) ([]*compiler.Value, error) {
return scanInputs(ctx, e.plan) src := e.plan
if mergeUserInputs {
// Set user inputs and plan code
var err error
src, err = e.prepare(ctx)
if err != nil {
return nil, err
}
}
return scanInputs(ctx, src), nil
} }