passing struct to plan.Load

Signed-off-by: Richard Jones <richard@dagger.io>
This commit is contained in:
Richard Jones 2022-01-04 11:54:50 -07:00
parent 7292d62cd6
commit b4bcbab311
No known key found for this signature in database
GPG Key ID: CFB3A382EB166F4C
2 changed files with 27 additions and 19 deletions

View File

@ -24,8 +24,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var withParams []string
var upCmd = &cobra.Command{ var upCmd = &cobra.Command{
Use: "up", Use: "up",
Short: "Bring an environment online with latest plan and inputs", Short: "Bring an environment online with latest plan and inputs",
@ -144,7 +142,11 @@ func checkUniverseVersion(ctx context.Context, projectPath string) bool {
func europaUp(ctx context.Context, cl *client.Client, args ...string) error { func europaUp(ctx context.Context, cl *client.Client, args ...string) error {
lg := log.Ctx(ctx) lg := log.Ctx(ctx)
p, err := plan.Load(ctx, withParams, args...) p, err := plan.Load(ctx, plan.Config{
Args: args,
With: viper.GetStringSlice("with"),
})
if err != nil { if err != nil {
lg.Fatal().Err(err).Msg("failed to load plan") lg.Fatal().Err(err).Msg("failed to load plan")
} }
@ -222,7 +224,7 @@ func checkInputs(ctx context.Context, env *environment.Environment) error {
func init() { func init() {
upCmd.Flags().BoolP("force", "f", false, "Force up, disable inputs check") upCmd.Flags().BoolP("force", "f", false, "Force up, disable inputs check")
upCmd.Flags().String("output", "", "Write computed output. Prints on stdout if set to-") upCmd.Flags().String("output", "", "Write computed output. Prints on stdout if set to-")
upCmd.Flags().StringArrayVarP(&withParams, "with", "w", []string{}, "") upCmd.Flags().StringArrayP("with", "w", []string{}, "")
if err := viper.BindPFlags(upCmd.Flags()); err != nil { if err := viper.BindPFlags(upCmd.Flags()); err != nil {
panic(err) panic(err)

View File

@ -19,40 +19,46 @@ import (
) )
type Plan struct { type Plan struct {
config Config
context *plancontext.Context context *plancontext.Context
source *compiler.Value source *compiler.Value
} }
func Load(ctx context.Context, withParams []string, args ...string) (*Plan, error) { type Config struct {
log.Ctx(ctx).Debug().Interface("args", args).Msg("loading plan") Args []string
With []string
}
func Load(ctx context.Context, cfg Config) (*Plan, error) {
log.Ctx(ctx).Debug().Interface("args", cfg.Args).Msg("loading plan")
// FIXME: universe vendoring // FIXME: universe vendoring
if err := state.VendorUniverse(ctx, ""); err != nil { if err := state.VendorUniverse(ctx, ""); err != nil {
return nil, err return nil, err
} }
v, err := compiler.Build("", nil, args...) v, err := compiler.Build("", nil, cfg.Args...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(withParams) > 0 { for i, param := range cfg.With {
for i, param := range withParams { log.Ctx(ctx).Debug().Interface("with", param).Msg("compiling overlay")
log.Ctx(ctx).Debug().Interface("with", param).Msg("compiling parameter") paramV, err := compiler.Compile(fmt.Sprintf("with%v", i), param)
paramV, err := compiler.Compile(fmt.Sprintf("with%v", i), param) if err != nil {
if err != nil { return nil, err
return nil, err }
}
log.Ctx(ctx).Debug().Interface("with", param).Msg("filling parameter") log.Ctx(ctx).Debug().Interface("with", param).Msg("filling overlay")
fillErr := v.FillPath(cue.ParsePath(""), paramV) fillErr := v.FillPath(cue.ParsePath(""), paramV)
if fillErr != nil { if fillErr != nil {
return nil, fillErr return nil, fillErr
}
} }
} }
p := &Plan{ p := &Plan{
config: cfg,
context: plancontext.New(), context: plancontext.New(),
source: v, source: v,
} }