From b393ecc0343842717c36a3d8bc18734e661055a2 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Wed, 16 Jun 2021 14:58:05 +0200 Subject: [PATCH] cleanup workspace interface Signed-off-by: Andrea Luzzardi --- cmd/dagger/cmd/new.go | 6 +++++- state/workspace.go | 10 ++++++++-- state/workspace_test.go | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/dagger/cmd/new.go b/cmd/dagger/cmd/new.go index 944475f4..ae065ab8 100644 --- a/cmd/dagger/cmd/new.go +++ b/cmd/dagger/cmd/new.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/viper" "go.dagger.io/dagger/cmd/dagger/cmd/common" "go.dagger.io/dagger/cmd/dagger/logger" + "go.dagger.io/dagger/state" ) var newCmd = &cobra.Command{ @@ -55,7 +56,10 @@ var newCmd = &cobra.Command{ module = p } - ws, err := workspace.Create(ctx, name, module, viper.GetString("package")) + ws, err := workspace.Create(ctx, name, state.CreateOpts{ + Module: module, + Package: viper.GetString("package"), + }) if err != nil { lg.Fatal().Err(err).Msg("failed to create environment") } diff --git a/state/workspace.go b/state/workspace.go index 2857b67a..3e71ceb0 100644 --- a/state/workspace.go +++ b/state/workspace.go @@ -229,7 +229,12 @@ func (w *Workspace) Save(ctx context.Context, st *State) error { return nil } -func (w *Workspace) Create(ctx context.Context, name, module, pkg string) (*State, error) { +type CreateOpts struct { + Module string + Package string +} + +func (w *Workspace) Create(ctx context.Context, name string, opts CreateOpts) (*State, error) { envPath, err := filepath.Abs(w.envPath(name)) if err != nil { return nil, err @@ -247,6 +252,7 @@ func (w *Workspace) Create(ctx context.Context, name, module, pkg string) (*Stat // 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 { @@ -265,7 +271,7 @@ func (w *Workspace) Create(ctx context.Context, name, module, pkg string) (*Stat Workspace: w.Path, Plan: Plan{ Module: module, - Package: pkg, + Package: opts.Package, }, Name: name, } diff --git a/state/workspace_test.go b/state/workspace_test.go index 2073bd91..8083f0e3 100644 --- a/state/workspace_test.go +++ b/state/workspace_test.go @@ -27,7 +27,7 @@ func TestWorkspace(t *testing.T) { require.Equal(t, root, workspace.Path) // Create - st, err := workspace.Create(ctx, "test", "", "") + st, err := workspace.Create(ctx, "test", CreateOpts{}) require.NoError(t, err) require.Equal(t, "test", st.Name) @@ -73,7 +73,7 @@ func TestEncryption(t *testing.T) { workspace, err := Init(ctx, root) require.NoError(t, err) - _, err = workspace.Create(ctx, "test", "", "") + _, err = workspace.Create(ctx, "test", CreateOpts{}) require.NoError(t, err) // Set a plaintext input, make sure it is not encrypted