From 5c9965f10ac48bfbc25240052da87981851150bf Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Tue, 7 Dec 2021 16:10:55 -0700 Subject: [PATCH] passings args to cue loader to mimick cue eval Signed-off-by: Richard Jones --- cmd/dagger/cmd/up.go | 34 +++++++++++++++++----------------- plan/plan.go | 6 +++--- state/project.go | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index 3e6960cd..12ccf559 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -25,7 +25,7 @@ import ( var upCmd = &cobra.Command{ Use: "up", Short: "Bring an environment online with latest plan and inputs", - Args: cobra.NoArgs, + Args: cobra.MaximumNArgs(1), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 @@ -52,6 +52,20 @@ var upCmd = &cobra.Command{ } ctx := lg.WithContext(cmd.Context()) + cl := common.NewClient(ctx) + + if viper.GetBool("europa") { + err = europaUp(ctx, cl, args...) + + // TODO: rework telemetry + // <-doneCh + + if err != nil { + lg.Fatal().Err(err).Msg("failed to up environment") + } + + return + } project := common.CurrentProject(ctx) st := common.CurrentEnvironmentState(ctx, project) @@ -62,20 +76,6 @@ var upCmd = &cobra.Command{ doneCh := common.TrackProjectCommand(ctx, cmd, project, st) - cl := common.NewClient(ctx) - - if viper.GetBool("europa") { - err = europaUp(ctx, cl, project.Path) - - <-doneCh - - if err != nil { - lg.Fatal().Err(err).Msg("failed to up environment") - } - - return - } - env, err := environment.New(st) if err != nil { lg.Fatal().Err(err).Msg("unable to create environment") @@ -112,10 +112,10 @@ var upCmd = &cobra.Command{ }, } -func europaUp(ctx context.Context, cl *client.Client, path string) error { +func europaUp(ctx context.Context, cl *client.Client, args ...string) error { lg := log.Ctx(ctx) - p, err := plan.Load(ctx, path, "") + p, err := plan.Load(ctx, args...) if err != nil { lg.Fatal().Err(err).Msg("failed to load plan") } diff --git a/plan/plan.go b/plan/plan.go index d6802fbb..f2dc0015 100644 --- a/plan/plan.go +++ b/plan/plan.go @@ -23,13 +23,13 @@ type Plan struct { source *compiler.Value } -func Load(ctx context.Context, path, pkg string) (*Plan, error) { +func Load(ctx context.Context, args ...string) (*Plan, error) { // FIXME: universe vendoring - if err := state.VendorUniverse(ctx, path); err != nil { + if err := state.VendorUniverse(ctx, ""); err != nil { return nil, err } - v, err := compiler.Build(path, nil, pkg) + v, err := compiler.Build("", nil, args...) if err != nil { return nil, err } diff --git a/state/project.go b/state/project.go index ec180a8f..0c70c9e1 100644 --- a/state/project.go +++ b/state/project.go @@ -356,6 +356,22 @@ func (w *Project) cleanPackageName(ctx context.Context, pkg string) (string, err func cueModInit(ctx context.Context, parentDir string) error { lg := log.Ctx(ctx) + if parentDir == "" { + wd, _ := os.Getwd() + separator := string(os.PathSeparator) + dirs := strings.Split(wd, separator) + dirsLen := len(dirs) + + // traverse the directory tree starting from PWD going up to successive parents + for i := dirsLen; i > 0; i-- { + parentDir = strings.Join(dirs[:i], separator) + // look for the cue.mod filder + if _, err := os.Stat(parentDir + "/cue.mod"); !os.IsNotExist(err) { + break // found it! + } + } + } + modDir := path.Join(parentDir, "cue.mod") if err := os.Mkdir(modDir, 0755); err != nil { if !errors.Is(err, os.ErrExist) {