diff --git a/pkg/cmd/workspace/create/cmd.go b/pkg/cmd/workspace/create/cmd.go index 0c937318..52a64d76 100644 --- a/pkg/cmd/workspace/create/cmd.go +++ b/pkg/cmd/workspace/create/cmd.go @@ -16,8 +16,14 @@ func NewCmd() *cobra.Command { This command creates a workspace with specified name and configuration file, where the file must be in the YAML format.`) example = i18n.T(` - # Create a new workspace - kusion workspace create dev -f dev.yaml`) + # Create a workspace + kusion workspace create dev -f dev.yaml + + # Create a workspace and set as current + kusion workspace create dev -f dev.yaml --current + + # Create a workspace in a specified backend + kusion workspace create prod -f prod.yaml --backend oss-prod`) ) o := NewOptions() @@ -37,5 +43,7 @@ func NewCmd() *cobra.Command { } cmd.Flags().StringVarP(&o.FilePath, "file", "f", "", i18n.T("the path of workspace configuration file")) + cmd.Flags().StringVarP(&o.Backend, "backend", "", "", i18n.T("the backend name")) + cmd.Flags().BoolVarP(&o.Current, "current", "", false, i18n.T("set the creating workspace as current")) return cmd } diff --git a/pkg/cmd/workspace/create/options.go b/pkg/cmd/workspace/create/options.go index 19be35b1..3ead9e15 100644 --- a/pkg/cmd/workspace/create/options.go +++ b/pkg/cmd/workspace/create/options.go @@ -3,13 +3,15 @@ package create import ( "fmt" + "kusionstack.io/kusion/pkg/backend" "kusionstack.io/kusion/pkg/cmd/workspace/util" - "kusionstack.io/kusion/pkg/workspace" ) type Options struct { Name string FilePath string + Backend string + Current bool } func NewOptions() *Options { @@ -26,7 +28,7 @@ func (o *Options) Complete(args []string) error { } func (o *Options) Validate() error { - if err := util.ValidateName(o.Name); err != nil { + if err := util.ValidateNotDefaultName(o.Name); err != nil { return err } if err := util.ValidateFilePath(o.FilePath); err != nil { @@ -36,14 +38,24 @@ func (o *Options) Validate() error { } func (o *Options) Run() error { - ws, err := util.GetValidWorkspaceFromFile(o.FilePath, o.Name) + storage, err := backend.NewWorkspaceStorage(o.Backend) if err != nil { return err } - if err = workspace.CreateWorkspaceByDefaultOperator(ws); err != nil { + ws, err := util.GetValidWorkspaceFromFile(o.FilePath, o.Name) + if err != nil { return err } + if err = storage.Create(ws); err != nil { + return err + } + + if o.Current { + if err = storage.SetCurrent(o.Name); err != nil { + return err + } + } fmt.Printf("create workspace %s successfully\n", o.Name) return nil } diff --git a/pkg/cmd/workspace/create/options_test.go b/pkg/cmd/workspace/create/options_test.go index 4c9f3a34..b6997fb1 100644 --- a/pkg/cmd/workspace/create/options_test.go +++ b/pkg/cmd/workspace/create/options_test.go @@ -8,8 +8,9 @@ import ( "github.com/stretchr/testify/assert" v1 "kusionstack.io/kusion/pkg/apis/core/v1" + "kusionstack.io/kusion/pkg/backend" "kusionstack.io/kusion/pkg/cmd/workspace/util" - "kusionstack.io/kusion/pkg/workspace" + workspacestorages "kusionstack.io/kusion/pkg/workspace/storages" ) func TestOptions_Complete(t *testing.T) { @@ -102,12 +103,10 @@ func TestOptions_Run(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { mockey.PatchConvey("mock create workspace", t, func() { - mockey.Mock(util.GetValidWorkspaceFromFile). - Return(&v1.Workspace{Name: "dev"}, nil). - Build() - mockey.Mock(workspace.CreateWorkspaceByDefaultOperator). - Return(nil). - Build() + mockey.Mock(backend.NewWorkspaceStorage).Return(&workspacestorages.LocalStorage{}, nil).Build() + mockey.Mock((*workspacestorages.LocalStorage).Create).Return(nil).Build() + mockey.Mock((*workspacestorages.LocalStorage).SetCurrent).Return(nil).Build() + mockey.Mock(util.GetValidWorkspaceFromFile).Return(&v1.Workspace{Name: "dev"}, nil).Build() err := tc.opts.Run() assert.Equal(t, tc.success, err == nil) diff --git a/pkg/cmd/workspace/del/cmd.go b/pkg/cmd/workspace/del/cmd.go index 50aa790e..11681c8c 100644 --- a/pkg/cmd/workspace/del/cmd.go +++ b/pkg/cmd/workspace/del/cmd.go @@ -13,11 +13,17 @@ func NewCmd() *cobra.Command { short = i18n.T(`Delete a workspace`) long = i18n.T(` - This command deletes a specified workspace.`) + This command deletes the current or a specified workspace.`) example = i18n.T(` - # Delete a workspace - kusion workspace delete dev`) + # Delete the current workspace + kusion workspace delete + + # Delete a specified workspace + kusion workspace delete dev + + # Delete a specified workspace in a specified backend + kusion workspace delete prod --backend oss-prod`) ) o := NewOptions() @@ -30,10 +36,11 @@ func NewCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) (err error) { defer util.RecoverErr(&err) util.CheckErr(o.Complete(args)) - util.CheckErr(o.Validate()) util.CheckErr(o.Run()) return }, } + + cmd.Flags().StringVarP(&o.Backend, "backend", "", "", i18n.T("the backend name")) return cmd } diff --git a/pkg/cmd/workspace/del/options.go b/pkg/cmd/workspace/del/options.go index 7dcd8cef..3d081c67 100644 --- a/pkg/cmd/workspace/del/options.go +++ b/pkg/cmd/workspace/del/options.go @@ -3,12 +3,13 @@ package del import ( "fmt" + "kusionstack.io/kusion/pkg/backend" "kusionstack.io/kusion/pkg/cmd/workspace/util" - "kusionstack.io/kusion/pkg/workspace" ) type Options struct { - Name string + Name string + Backend string } func NewOptions() *Options { @@ -24,15 +25,26 @@ func (o *Options) Complete(args []string) error { return nil } -func (o *Options) Validate() error { - if err := util.ValidateName(o.Name); err != nil { +func (o *Options) Run() error { + storage, err := backend.NewWorkspaceStorage(o.Backend) + if err != nil { return err } - return nil -} -func (o *Options) Run() error { - if err := workspace.DeleteWorkspaceByDefaultOperator(o.Name); err != nil { + // get current workspace name if not specified. + if o.Name == "" { + var name string + name, err = storage.GetCurrent() + if err != nil { + return err + } + o.Name = name + } + if err = util.ValidateNotDefaultName(o.Name); err != nil { + return err + } + + if err = storage.Delete(o.Name); err != nil { return err } fmt.Printf("delete workspace %s successfully\n", o.Name) diff --git a/pkg/cmd/workspace/del/options_test.go b/pkg/cmd/workspace/del/options_test.go index 02988b5b..bb1eeb9b 100644 --- a/pkg/cmd/workspace/del/options_test.go +++ b/pkg/cmd/workspace/del/options_test.go @@ -7,7 +7,8 @@ import ( "github.com/bytedance/mockey" "github.com/stretchr/testify/assert" - "kusionstack.io/kusion/pkg/workspace" + "kusionstack.io/kusion/pkg/backend" + workspacestorages "kusionstack.io/kusion/pkg/workspace/storages" ) func TestOptions_Complete(t *testing.T) { @@ -43,34 +44,6 @@ func TestOptions_Complete(t *testing.T) { } } -func TestOptions_Validate(t *testing.T) { - testcases := []struct { - name string - opts *Options - success bool - }{ - { - name: "valid options", - opts: &Options{ - Name: "dev", - }, - success: true, - }, - { - name: "invalid options empty name", - opts: &Options{}, - success: false, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - err := tc.opts.Validate() - assert.Equal(t, tc.success, err == nil) - }) - } -} - func TestOptions_Run(t *testing.T) { testcases := []struct { name string @@ -89,9 +62,9 @@ func TestOptions_Run(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { mockey.PatchConvey("mock delete workspace", t, func() { - mockey.Mock(workspace.DeleteWorkspaceByDefaultOperator). - Return(nil). - Build() + mockey.Mock(backend.NewWorkspaceStorage).Return(&workspacestorages.LocalStorage{}, nil).Build() + mockey.Mock((*workspacestorages.LocalStorage).GetCurrent).Return("dev", nil).Build() + mockey.Mock((*workspacestorages.LocalStorage).Delete).Return(nil).Build() err := tc.opts.Run() assert.Equal(t, tc.success, err == nil) diff --git a/pkg/cmd/workspace/list/cmd.go b/pkg/cmd/workspace/list/cmd.go index 048776c0..906ef123 100644 --- a/pkg/cmd/workspace/list/cmd.go +++ b/pkg/cmd/workspace/list/cmd.go @@ -2,15 +2,12 @@ package list import ( "errors" - "fmt" "github.com/spf13/cobra" - "gopkg.in/yaml.v3" "k8s.io/kubectl/pkg/util/templates" "kusionstack.io/kusion/pkg/cmd/util" "kusionstack.io/kusion/pkg/util/i18n" - "kusionstack.io/kusion/pkg/workspace" ) var ErrNotEmptyArgs = errors.New("no args accepted") @@ -24,9 +21,14 @@ func NewCmd() *cobra.Command { example = i18n.T(` # List all workspace names - kusion workspace list`) + kusion workspace list + + # List all workspace names in a specified backend + kusion workspace list --backend oss-prod +`) ) + o := NewOptions() cmd := &cobra.Command{ Use: "list", Short: short, @@ -35,30 +37,12 @@ func NewCmd() *cobra.Command { DisableFlagsInUseLine: true, RunE: func(cmd *cobra.Command, args []string) (err error) { defer util.RecoverErr(&err) - util.CheckErr(Validate(args)) - util.CheckErr(Run()) + util.CheckErr(o.Validate(args)) + util.CheckErr(o.Run()) return }, } - return cmd -} -func Validate(args []string) error { - if len(args) != 0 { - return ErrNotEmptyArgs - } - return nil -} - -func Run() error { - names, err := workspace.GetWorkspaceNamesByDefaultOperator() - if err != nil { - return err - } - content, err := yaml.Marshal(names) - if err != nil { - return fmt.Errorf("yaml marshal workspace names failed: %w", err) - } - fmt.Print(string(content)) - return nil + cmd.Flags().StringVarP(&o.Backend, "backend", "", "", i18n.T("the backend name")) + return cmd } diff --git a/pkg/cmd/workspace/list/cmd_test.go b/pkg/cmd/workspace/list/cmd_test.go index b695501b..7fc294e3 100644 --- a/pkg/cmd/workspace/list/cmd_test.go +++ b/pkg/cmd/workspace/list/cmd_test.go @@ -5,15 +5,12 @@ import ( "github.com/bytedance/mockey" "github.com/stretchr/testify/assert" - - "kusionstack.io/kusion/pkg/workspace" ) func TestNewCmd(t *testing.T) { t.Run("successfully list workspace", func(t *testing.T) { mockey.PatchConvey("mock cmd", t, func() { - mockey.Mock(Validate).Return(nil).Build() - mockey.Mock(Run).Return(nil).Build() + mockey.Mock((*Options).Run).Return(nil).Build() cmd := NewCmd() err := cmd.Execute() @@ -21,54 +18,3 @@ func TestNewCmd(t *testing.T) { }) }) } - -func TestValidate(t *testing.T) { - testcases := []struct { - name string - args []string - success bool - }{ - { - name: "valid args", - args: nil, - success: true, - }, - { - name: "invalid args", - args: []string{"dev"}, - success: false, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - err := Validate(tc.args) - assert.Equal(t, tc.success, err == nil) - }) - } -} - -func TestRun(t *testing.T) { - testcases := []struct { - name string - success bool - }{ - { - name: "successfully run", - success: true, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - mockey.PatchConvey("mock get workspace names", t, func() { - mockey.Mock(workspace.GetWorkspaceNamesByDefaultOperator). - Return([]string{"dev"}, nil). - Build() - - err := Run() - assert.Equal(t, tc.success, err == nil) - }) - }) - } -} diff --git a/pkg/cmd/workspace/list/options.go b/pkg/cmd/workspace/list/options.go new file mode 100644 index 00000000..9220cbcc --- /dev/null +++ b/pkg/cmd/workspace/list/options.go @@ -0,0 +1,42 @@ +package list + +import ( + "fmt" + + "gopkg.in/yaml.v3" + + "kusionstack.io/kusion/pkg/backend" +) + +type Options struct { + Backend string +} + +func NewOptions() *Options { + return &Options{} +} + +func (o *Options) Validate(args []string) error { + if len(args) != 0 { + return ErrNotEmptyArgs + } + return nil +} + +func (o *Options) Run() error { + storage, err := backend.NewWorkspaceStorage(o.Backend) + if err != nil { + return err + } + + names, err := storage.GetNames() + if err != nil { + return err + } + content, err := yaml.Marshal(names) + if err != nil { + return fmt.Errorf("yaml marshal workspace names failed: %w", err) + } + fmt.Print(string(content)) + return nil +} diff --git a/pkg/cmd/workspace/list/options_test.go b/pkg/cmd/workspace/list/options_test.go new file mode 100644 index 00000000..fc1f6f5e --- /dev/null +++ b/pkg/cmd/workspace/list/options_test.go @@ -0,0 +1,63 @@ +package list + +import ( + "testing" + + "github.com/bytedance/mockey" + "github.com/stretchr/testify/assert" + + "kusionstack.io/kusion/pkg/backend" + workspacestorages "kusionstack.io/kusion/pkg/workspace/storages" +) + +func TestOptions_Validate(t *testing.T) { + testcases := []struct { + name string + args []string + success bool + }{ + { + name: "valid args", + args: nil, + success: true, + }, + { + name: "invalid args", + args: []string{"dev"}, + success: false, + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + opts := NewOptions() + err := opts.Validate(tc.args) + assert.Equal(t, tc.success, err == nil) + }) + } +} + +func TestOptions_Run(t *testing.T) { + testcases := []struct { + name string + success bool + }{ + { + name: "successfully run", + success: true, + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + mockey.PatchConvey("mock get workspace names", t, func() { + mockey.Mock(backend.NewWorkspaceStorage).Return(&workspacestorages.LocalStorage{}, nil).Build() + mockey.Mock((*workspacestorages.LocalStorage).GetNames).Return([]string{"dev"}, nil).Build() + + opts := NewOptions() + err := opts.Run() + assert.Equal(t, tc.success, err == nil) + }) + }) + } +} diff --git a/pkg/cmd/workspace/show/cmd.go b/pkg/cmd/workspace/show/cmd.go index 78ffc155..537af85e 100644 --- a/pkg/cmd/workspace/show/cmd.go +++ b/pkg/cmd/workspace/show/cmd.go @@ -13,11 +13,17 @@ func NewCmd() *cobra.Command { short = i18n.T(`Show a workspace configuration`) long = i18n.T(` - This command gets a specified workspace configuration.`) + This command gets the current or a specified workspace configuration.`) example = i18n.T(` - # Show a workspace configuration - kusion workspace show dev`) + # Show current workspace configuration + kusion workspace show + + # Show a specified workspace configuration + kusion workspace show dev + + # Show a specified workspace in a specified backend + kusion workspace show prod --backend oss-prod`) ) o := NewOptions() @@ -30,10 +36,11 @@ func NewCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) (err error) { defer util.RecoverErr(&err) util.CheckErr(o.Complete(args)) - util.CheckErr(o.Validate()) util.CheckErr(o.Run()) return }, } + + cmd.Flags().StringVarP(&o.Backend, "backend", "", "", i18n.T("the backend name")) return cmd } diff --git a/pkg/cmd/workspace/show/options.go b/pkg/cmd/workspace/show/options.go index 436821eb..e43fb071 100644 --- a/pkg/cmd/workspace/show/options.go +++ b/pkg/cmd/workspace/show/options.go @@ -5,12 +5,13 @@ import ( "gopkg.in/yaml.v3" + "kusionstack.io/kusion/pkg/backend" "kusionstack.io/kusion/pkg/cmd/workspace/util" - "kusionstack.io/kusion/pkg/workspace" ) type Options struct { - Name string + Name string + Backend string } func NewOptions() *Options { @@ -26,15 +27,13 @@ func (o *Options) Complete(args []string) error { return nil } -func (o *Options) Validate() error { - if err := util.ValidateName(o.Name); err != nil { +func (o *Options) Run() error { + storage, err := backend.NewWorkspaceStorage(o.Backend) + if err != nil { return err } - return nil -} -func (o *Options) Run() error { - ws, err := workspace.GetWorkspaceByDefaultOperator(o.Name) + ws, err := storage.Get(o.Name) if err != nil { return err } @@ -42,6 +41,7 @@ func (o *Options) Run() error { if err != nil { return fmt.Errorf("yaml marshal workspace configuration failed: %w", err) } + fmt.Printf("show configuration of workspace %s:\n", ws.Name) fmt.Print(string(content)) return nil } diff --git a/pkg/cmd/workspace/show/options_test.go b/pkg/cmd/workspace/show/options_test.go index 141ea2cb..356eae49 100644 --- a/pkg/cmd/workspace/show/options_test.go +++ b/pkg/cmd/workspace/show/options_test.go @@ -8,7 +8,8 @@ import ( "github.com/stretchr/testify/assert" v1 "kusionstack.io/kusion/pkg/apis/core/v1" - "kusionstack.io/kusion/pkg/workspace" + "kusionstack.io/kusion/pkg/backend" + workspacestorages "kusionstack.io/kusion/pkg/workspace/storages" ) func TestOptions_Complete(t *testing.T) { @@ -44,34 +45,6 @@ func TestOptions_Complete(t *testing.T) { } } -func TestOptions_Validate(t *testing.T) { - testcases := []struct { - name string - opts *Options - success bool - }{ - { - name: "valid options", - opts: &Options{ - Name: "dev", - }, - success: true, - }, - { - name: "invalid options empty name", - opts: &Options{}, - success: false, - }, - } - - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - err := tc.opts.Validate() - assert.Equal(t, tc.success, err == nil) - }) - } -} - func TestOptions_Run(t *testing.T) { testcases := []struct { name string @@ -90,9 +63,8 @@ func TestOptions_Run(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { mockey.PatchConvey("mock show workspace", t, func() { - mockey.Mock(workspace.GetWorkspaceByDefaultOperator). - Return(&v1.Workspace{Name: "dev"}, nil). - Build() + mockey.Mock(backend.NewWorkspaceStorage).Return(&workspacestorages.LocalStorage{}, nil).Build() + mockey.Mock((*workspacestorages.LocalStorage).Get).Return(&v1.Workspace{Name: "dev"}, nil).Build() err := tc.opts.Run() assert.Equal(t, tc.success, err == nil) diff --git a/pkg/cmd/workspace/update/cmd.go b/pkg/cmd/workspace/update/cmd.go index bf6918f2..808d2775 100644 --- a/pkg/cmd/workspace/update/cmd.go +++ b/pkg/cmd/workspace/update/cmd.go @@ -16,8 +16,14 @@ func NewCmd() *cobra.Command { This command updates a workspace configuration with specified configuration file, where the file must be in the YAML format.`) example = i18n.T(` - # Update a workspace configuration - kusion workspace update dev -f dev.yaml`) + # Update the current workspace + kusion workspace update -f dev.yaml + + # Update a specified workspace and set as current + kusion workspace update dev -f dev.yaml --current + + # Update a specified workspace in a specified backend + kusion workspace update prod -f prod.yaml --backend oss-prod`) ) o := NewOptions() @@ -35,7 +41,9 @@ func NewCmd() *cobra.Command { return }, } - cmd.Flags().StringVarP(&o.FilePath, "file", "f", "", i18n.T("the path of workspace configuration file")) + cmd.Flags().StringVarP(&o.FilePath, "file", "f", "", i18n.T("the path of workspace configuration file")) + cmd.Flags().StringVarP(&o.Backend, "backend", "", "", i18n.T("the backend name")) + cmd.Flags().BoolVarP(&o.Current, "current", "", false, i18n.T("set the creating workspace as current")) return cmd } diff --git a/pkg/cmd/workspace/update/options.go b/pkg/cmd/workspace/update/options.go index 020d5986..0a84a3b1 100644 --- a/pkg/cmd/workspace/update/options.go +++ b/pkg/cmd/workspace/update/options.go @@ -3,13 +3,15 @@ package update import ( "fmt" + "kusionstack.io/kusion/pkg/backend" "kusionstack.io/kusion/pkg/cmd/workspace/util" - "kusionstack.io/kusion/pkg/workspace" ) type Options struct { Name string FilePath string + Backend string + Current bool } func NewOptions() *Options { @@ -26,9 +28,6 @@ func (o *Options) Complete(args []string) error { } func (o *Options) Validate() error { - if err := util.ValidateName(o.Name); err != nil { - return err - } if err := util.ValidateFilePath(o.FilePath); err != nil { return err } @@ -36,14 +35,25 @@ func (o *Options) Validate() error { } func (o *Options) Run() error { - ws, err := util.GetValidWorkspaceFromFile(o.FilePath, o.Name) + storage, err := backend.NewWorkspaceStorage(o.Backend) if err != nil { return err } - if err = workspace.UpdateWorkspaceByDefaultOperator(ws); err != nil { + ws, err := util.GetValidWorkspaceFromFile(o.FilePath, o.Name) + if err != nil { return err } - fmt.Printf("update workspace %s successfully\n", o.Name) + if err = storage.Update(ws); err != nil { + return err + } + + if o.Current && o.Name != "" { + if err = storage.SetCurrent(o.Name); err != nil { + return err + } + } + + fmt.Printf("update workspace %s successfully\n", ws.Name) return nil } diff --git a/pkg/cmd/workspace/update/options_test.go b/pkg/cmd/workspace/update/options_test.go index d896fae0..3a5839dc 100644 --- a/pkg/cmd/workspace/update/options_test.go +++ b/pkg/cmd/workspace/update/options_test.go @@ -8,8 +8,9 @@ import ( "github.com/stretchr/testify/assert" v1 "kusionstack.io/kusion/pkg/apis/core/v1" + "kusionstack.io/kusion/pkg/backend" "kusionstack.io/kusion/pkg/cmd/workspace/util" - "kusionstack.io/kusion/pkg/workspace" + workspacestorages "kusionstack.io/kusion/pkg/workspace/storages" ) func TestOptions_Complete(t *testing.T) { @@ -59,13 +60,6 @@ func TestOptions_Validate(t *testing.T) { }, success: true, }, - { - name: "invalid options empty name", - opts: &Options{ - FilePath: "dev.yaml", - }, - success: false, - }, { name: "invalid options empty file path", opts: &Options{ @@ -102,12 +96,9 @@ func TestOptions_Run(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { mockey.PatchConvey("mock update workspace", t, func() { - mockey.Mock(util.GetValidWorkspaceFromFile). - Return(&v1.Workspace{Name: "dev"}, nil). - Build() - mockey.Mock(workspace.UpdateWorkspaceByDefaultOperator). - Return(nil). - Build() + mockey.Mock(backend.NewWorkspaceStorage).Return(&workspacestorages.LocalStorage{}, nil).Build() + mockey.Mock((*workspacestorages.LocalStorage).Update).Return(nil).Build() + mockey.Mock(util.GetValidWorkspaceFromFile).Return(&v1.Workspace{Name: "dev"}, nil).Build() err := tc.opts.Run() assert.Equal(t, tc.success, err == nil) diff --git a/pkg/cmd/workspace/util/util.go b/pkg/cmd/workspace/util/util.go index 3f62ffb4..28007d61 100644 --- a/pkg/cmd/workspace/util/util.go +++ b/pkg/cmd/workspace/util/util.go @@ -9,23 +9,28 @@ import ( v1 "kusionstack.io/kusion/pkg/apis/core/v1" "kusionstack.io/kusion/pkg/workspace" + "kusionstack.io/kusion/pkg/workspace/storages" ) var ( - ErrNotOneArgs = errors.New("only one arg accepted") - ErrEmptyName = errors.New("empty workspace name") - ErrEmptyFilePath = errors.New("empty configuration file path") + ErrMoreThanOneArgs = errors.New("more than one args are not accepted") + ErrEmptyName = errors.New("empty workspace name") + ErrInvalidDefault = errors.New("invalid default workspace") + ErrEmptyFilePath = errors.New("empty configuration file path") ) // GetNameFromArgs returns workspace name specified by args. func GetNameFromArgs(args []string) (string, error) { - if len(args) != 1 { - return "", ErrNotOneArgs + if len(args) > 1 { + return "", ErrMoreThanOneArgs } - return args[0], nil + if len(args) == 1 { + return args[0], nil + } + return "", nil } -// ValidateName returns the workspace name is valid or not. +// ValidateName returns the workspace name is valid or not, which is used for getting and updating workspace. func ValidateName(name string) error { if name == "" { return ErrEmptyName @@ -33,6 +38,18 @@ func ValidateName(name string) error { return nil } +// ValidateNotDefaultName returns true if ValidateName is true and the workspace name is not default, which is +// used for creating and deleting workspace. +func ValidateNotDefaultName(name string) error { + if err := ValidateName(name); err != nil { + return err + } + if name == storages.DefaultWorkspace { + return ErrInvalidDefault + } + return nil +} + // ValidateFilePath returns the configuration file path is valid or not. func ValidateFilePath(filePath string) error { if filePath == "" { @@ -53,7 +70,7 @@ func GetValidWorkspaceFromFile(filePath, name string) (*v1.Workspace, error) { return nil, fmt.Errorf("yaml unmarshal file %s failed: %w", filePath, err) } - workspace.CompleteWorkspace(ws, name) + ws.Name = name if err = workspace.ValidateWorkspace(ws); err != nil { return nil, fmt.Errorf("invalid workspace configuration: %w", err) }