From 919d5576d03bbc28195f69c06cc2cd1e374f0899 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 1 Jul 2021 19:00:58 +0200 Subject: [PATCH] plan: default cue module to . Fixes #699 Signed-off-by: Andrea Luzzardi --- cmd/dagger/cmd/new.go | 39 ++++++++++++++++++++------------------- state/workspace.go | 34 +++++++--------------------------- state/workspace_test.go | 8 ++++++-- tests/cli.bats | 2 +- 4 files changed, 34 insertions(+), 49 deletions(-) diff --git a/cmd/dagger/cmd/new.go b/cmd/dagger/cmd/new.go index ae065ab8..470de68c 100644 --- a/cmd/dagger/cmd/new.go +++ b/cmd/dagger/cmd/new.go @@ -37,26 +37,27 @@ var newCmd = &cobra.Command{ name := args[0] module := viper.GetString("module") - if module != "" { - p, err := filepath.Abs(module) - if err != nil { - lg.Fatal().Err(err).Str("path", module).Msg("unable to resolve path") - } - - if !strings.HasPrefix(p, workspace.Path) { - lg.Fatal().Err(err).Str("path", module).Msg("module is outside the workspace") - } - p, err = filepath.Rel(workspace.Path, p) - if err != nil { - lg.Fatal().Err(err).Str("path", module).Msg("unable to resolve path") - } - if !strings.HasPrefix(p, ".") { - p = "./" + p - } - module = p + if module == "" { + lg.Fatal().Msg("missing --module") + } + p, err := filepath.Abs(module) + if err != nil { + lg.Fatal().Err(err).Str("path", module).Msg("unable to resolve path") } - ws, err := workspace.Create(ctx, name, state.CreateOpts{ + if !strings.HasPrefix(p, workspace.Path) { + lg.Fatal().Err(err).Str("path", module).Msg("module is outside the workspace") + } + p, err = filepath.Rel(workspace.Path, p) + if err != nil { + lg.Fatal().Err(err).Str("path", module).Msg("unable to resolve path") + } + if !strings.HasPrefix(p, ".") { + p = "./" + p + } + module = p + + ws, err := workspace.Create(ctx, name, state.Plan{ Module: module, Package: viper.GetString("package"), }) @@ -70,7 +71,7 @@ var newCmd = &cobra.Command{ } func init() { - newCmd.Flags().StringP("module", "m", "", "references the local path of the cue module to use as a plan, relative to the workspace root") + newCmd.Flags().StringP("module", "m", ".", "references the local path of the cue module to use as a plan, relative to the workspace root") newCmd.Flags().StringP("package", "p", "", "references the name of the Cue package within the module to use as a plan. Default: defer to cue loader") if err := viper.BindPFlags(newCmd.Flags()); err != nil { panic(err) diff --git a/state/workspace.go b/state/workspace.go index 3e71ceb0..d0c7befd 100644 --- a/state/workspace.go +++ b/state/workspace.go @@ -229,17 +229,16 @@ func (w *Workspace) Save(ctx context.Context, st *State) error { return nil } -type CreateOpts struct { - Module string - Package string -} - -func (w *Workspace) Create(ctx context.Context, name string, opts CreateOpts) (*State, error) { +func (w *Workspace) Create(ctx context.Context, name string, plan Plan) (*State, error) { envPath, err := filepath.Abs(w.envPath(name)) if err != nil { return nil, err } + if _, err := os.Stat(plan.Module); err != nil { + return nil, err + } + // Environment directory if err := os.MkdirAll(envPath, 0755); err != nil { if errors.Is(err, os.ErrExist) { @@ -250,30 +249,11 @@ func (w *Workspace) Create(ctx context.Context, name string, opts CreateOpts) (* manifestPath := path.Join(envPath, manifestFile) - // Backward compat: if no plan module has been provided, - // use `.dagger/env//plan` - module := opts.Module - if module == "" { - planPath := path.Join(envPath, planDir) - if err := os.Mkdir(planPath, 0755); err != nil { - return nil, err - } - - planRelPath, err := filepath.Rel(w.Path, planPath) - if err != nil { - return nil, err - } - module = planRelPath - } - st := &State{ Path: envPath, Workspace: w.Path, - Plan: Plan{ - Module: module, - Package: opts.Package, - }, - Name: name, + Plan: plan, + Name: name, } data, err := yaml.Marshal(st) diff --git a/state/workspace_test.go b/state/workspace_test.go index bd2f7a13..28b86810 100644 --- a/state/workspace_test.go +++ b/state/workspace_test.go @@ -30,7 +30,9 @@ func TestWorkspace(t *testing.T) { require.Equal(t, root, workspace.Path) // Create - st, err := workspace.Create(ctx, "test", CreateOpts{}) + st, err := workspace.Create(ctx, "test", Plan{ + Module: ".", + }) require.NoError(t, err) require.Equal(t, "test", st.Name) @@ -78,7 +80,9 @@ func TestEncryption(t *testing.T) { workspace, err := Init(ctx, root) require.NoError(t, err) - _, err = workspace.Create(ctx, "test", CreateOpts{}) + _, err = workspace.Create(ctx, "test", Plan{ + Module: ".", + }) require.NoError(t, err) // Set a plaintext input, make sure it is not encrypted diff --git a/tests/cli.bats b/tests/cli.bats index 044377fa..b2db1e56 100644 --- a/tests/cli.bats +++ b/tests/cli.bats @@ -27,7 +27,7 @@ setup() { assert_success refute_output - run "$DAGGER" new "test" + run "$DAGGER" new "test" --module "$DAGGER_WORKSPACE" assert_success run "$DAGGER" list