Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update workspace cmds, add backend and current flags #942

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pkg/cmd/workspace/create/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
20 changes: 16 additions & 4 deletions pkg/cmd/workspace/create/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
healthjyk marked this conversation as resolved.
Show resolved Hide resolved
return err
}
if err := util.ValidateFilePath(o.FilePath); err != nil {
Expand All @@ -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
}
13 changes: 6 additions & 7 deletions pkg/cmd/workspace/create/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions pkg/cmd/workspace/del/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
28 changes: 20 additions & 8 deletions pkg/cmd/workspace/del/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
37 changes: 5 additions & 32 deletions pkg/cmd/workspace/del/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
36 changes: 10 additions & 26 deletions pkg/cmd/workspace/list/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand All @@ -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
}
56 changes: 1 addition & 55 deletions pkg/cmd/workspace/list/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,16 @@ 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()
assert.Nil(t, err)
})
})
}

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)
})
})
}
}
Loading
Loading