cleanup workspace interface

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-06-16 14:58:05 +02:00 committed by Solomon Hykes
parent 7f456b9131
commit b393ecc034
3 changed files with 15 additions and 5 deletions

View File

@ -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")
}

View File

@ -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/<name>/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,
}

View File

@ -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