From 5c9965f10ac48bfbc25240052da87981851150bf Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Tue, 7 Dec 2021 16:10:55 -0700 Subject: [PATCH 1/7] 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) { From 83b10d40317f9bf38fb05287f4319dbf4394ae1d Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Wed, 8 Dec 2021 15:28:02 -0700 Subject: [PATCH 2/7] refactored finding of cue.mod parent Signed-off-by: Richard Jones --- plan/plan.go | 1 + state/project.go | 45 +++++++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/plan/plan.go b/plan/plan.go index f2dc0015..b5c295a5 100644 --- a/plan/plan.go +++ b/plan/plan.go @@ -25,6 +25,7 @@ type Plan struct { func Load(ctx context.Context, args ...string) (*Plan, error) { // FIXME: universe vendoring + if err := state.VendorUniverse(ctx, ""); err != nil { return nil, err } diff --git a/state/project.go b/state/project.go index 0c70c9e1..4b28e4e2 100644 --- a/state/project.go +++ b/state/project.go @@ -356,22 +356,6 @@ 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) { @@ -407,6 +391,11 @@ func cueModInit(ctx context.Context, parentDir string) error { } func VendorUniverse(ctx context.Context, p string) error { + + if p == "" { + p = getCueModParent() + } + // ensure cue module is initialized if err := cueModInit(ctx, p); err != nil { return err @@ -430,3 +419,27 @@ func VendorUniverse(ctx context.Context, p string) error { return nil } + +func getCueModParent() string { + + cwd, _ := os.Getwd() + parentDir := cwd + + // traverse the directory tree up through ancestors looking for a cue.mod folder + for { + + if _, err := os.Stat(path.Join(parentDir, "cue.mod")); !errors.Is(err, os.ErrNotExist) { + break // found it! + } + + parentDir = filepath.Dir(parentDir) + + if parentDir == string(os.PathSeparator) { + // reached the root + parentDir = cwd // reset to working directory + break + } + } + + return parentDir +} From 28cebb1bf03295fbe680f6105531686d054a4bd3 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Wed, 8 Dec 2021 16:24:16 -0700 Subject: [PATCH 3/7] added test Signed-off-by: Richard Jones --- tests/plan.bats | 11 +++++++++++ tests/plan/hello-europa/main.cue | 14 ++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/plan.bats create mode 100644 tests/plan/hello-europa/main.cue diff --git a/tests/plan.bats b/tests/plan.bats new file mode 100644 index 00000000..71810688 --- /dev/null +++ b/tests/plan.bats @@ -0,0 +1,11 @@ +setup() { + load 'helpers' + + common_setup +} + +@test "plan: hello" { + run dagger --no-cache --europa up ./plan/hello-europa + assert_success + assert_output --partial 'Hello Europa!' +} \ No newline at end of file diff --git a/tests/plan/hello-europa/main.cue b/tests/plan/hello-europa/main.cue new file mode 100644 index 00000000..615590a7 --- /dev/null +++ b/tests/plan/hello-europa/main.cue @@ -0,0 +1,14 @@ +package main + +import ( + "alpha.dagger.io/dagger/engine" + "alpha.dagger.io/os" +) + +engine.#Plan & { + actions: { + sayHello: os.#Container & { + command: "echo Hello Europa!" + } + } +} \ No newline at end of file From b5b73a3a8f344e0b0ae8d1f3499215046ffec1ac Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Wed, 8 Dec 2021 16:38:45 -0700 Subject: [PATCH 4/7] removed unnecessary leading new lines Signed-off-by: Richard Jones --- state/project.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/state/project.go b/state/project.go index 4b28e4e2..c85a50d3 100644 --- a/state/project.go +++ b/state/project.go @@ -391,7 +391,6 @@ func cueModInit(ctx context.Context, parentDir string) error { } func VendorUniverse(ctx context.Context, p string) error { - if p == "" { p = getCueModParent() } @@ -421,13 +420,11 @@ func VendorUniverse(ctx context.Context, p string) error { } func getCueModParent() string { - cwd, _ := os.Getwd() parentDir := cwd // traverse the directory tree up through ancestors looking for a cue.mod folder for { - if _, err := os.Stat(path.Join(parentDir, "cue.mod")); !errors.Is(err, os.ErrNotExist) { break // found it! } From 69ee3750b8583e2b76451b5c9a84cfc3a3d5eab3 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Wed, 8 Dec 2021 16:45:54 -0700 Subject: [PATCH 5/7] cue fmt Signed-off-by: Richard Jones --- tests/plan/hello-europa/main.cue | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/plan/hello-europa/main.cue b/tests/plan/hello-europa/main.cue index 615590a7..f268a18d 100644 --- a/tests/plan/hello-europa/main.cue +++ b/tests/plan/hello-europa/main.cue @@ -1,14 +1,12 @@ package main import ( - "alpha.dagger.io/dagger/engine" - "alpha.dagger.io/os" + "alpha.dagger.io/dagger/engine" + "alpha.dagger.io/os" ) engine.#Plan & { - actions: { - sayHello: os.#Container & { - command: "echo Hello Europa!" - } - } -} \ No newline at end of file + actions: sayHello: os.#Container & { + command: "echo Hello Europa!" + } +} From 026c7641b34e9270508ff6f3981c0eac4b82b135 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Thu, 9 Dec 2021 11:30:15 -0700 Subject: [PATCH 6/7] refactored test to use CUE validations rather than container stdout Signed-off-by: Richard Jones --- tests/plan.bats | 1 - tests/plan/hello-europa/main.cue | 8 ++++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/plan.bats b/tests/plan.bats index 71810688..7ab5bad0 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -7,5 +7,4 @@ setup() { @test "plan: hello" { run dagger --no-cache --europa up ./plan/hello-europa assert_success - assert_output --partial 'Hello Europa!' } \ No newline at end of file diff --git a/tests/plan/hello-europa/main.cue b/tests/plan/hello-europa/main.cue index f268a18d..466f34f9 100644 --- a/tests/plan/hello-europa/main.cue +++ b/tests/plan/hello-europa/main.cue @@ -6,7 +6,11 @@ import ( ) engine.#Plan & { - actions: sayHello: os.#Container & { - command: "echo Hello Europa!" + actions: { + sayHello: os.#Container & { + command: "echo Hello Europa! > /out.txt" + } + + verify: "Hello Europa!\n" & (os.#File & {from: sayHello, path: "/out.txt"}).contents } } From a80c48f241bbeb00c7a8e39ffaeb2eed07aec334 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Thu, 9 Dec 2021 11:36:30 -0700 Subject: [PATCH 7/7] removed --no-cache Signed-off-by: Richard Jones --- tests/plan.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plan.bats b/tests/plan.bats index 7ab5bad0..56c037b9 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -5,6 +5,6 @@ setup() { } @test "plan: hello" { - run dagger --no-cache --europa up ./plan/hello-europa + run dagger --europa up ./plan/hello-europa assert_success } \ No newline at end of file