Skip to content

Commit

Permalink
chore: parse cluster from cmd args (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Apr 13, 2023
1 parent 1cbc10e commit 103d18b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
7 changes: 5 additions & 2 deletions pkg/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

previewcmd "kusionstack.io/kusion/pkg/cmd/preview"
"kusionstack.io/kusion/pkg/cmd/spec"
"kusionstack.io/kusion/pkg/cmd/util"
"kusionstack.io/kusion/pkg/engine/backend"
_ "kusionstack.io/kusion/pkg/engine/backend/init"
"kusionstack.io/kusion/pkg/engine/models"
Expand Down Expand Up @@ -283,7 +284,8 @@ func Apply(
}
close(ac.MsgCh)
} else {
cluster := planResources.ParseCluster()
// parse cluster in arguments
cluster := util.ParseClusterArgument(o.Arguments)
_, st := ac.Apply(&operation.ApplyRequest{
Request: opsmodels.Request{
Tenant: changes.Project().Tenant,
Expand Down Expand Up @@ -321,7 +323,8 @@ func Apply(
// if err != nil {
// return err
// }
func Watch(o *ApplyOptions,
func Watch(
o *ApplyOptions,
planResources *models.Spec,
changes *opsmodels.Changes,
) error {
Expand Down
10 changes: 6 additions & 4 deletions pkg/cmd/preview/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

compilecmd "kusionstack.io/kusion/pkg/cmd/compile"
"kusionstack.io/kusion/pkg/cmd/spec"
"kusionstack.io/kusion/pkg/cmd/util"
"kusionstack.io/kusion/pkg/engine/backend"
"kusionstack.io/kusion/pkg/engine/models"
"kusionstack.io/kusion/pkg/engine/operation"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (o *PreviewOptions) Run() error {
}

// Get compile result
spec, err := spec.GenerateSpecWithSpinner(&generator.Options{
sp, err := spec.GenerateSpecWithSpinner(&generator.Options{
WorkDir: o.WorkDir,
Filenames: o.Filenames,
Settings: o.Settings,
Expand All @@ -77,7 +78,7 @@ func (o *PreviewOptions) Run() error {
}

// return immediately if no resource found in stack
if spec == nil || len(spec.Resources) == 0 {
if sp == nil || len(sp.Resources) == 0 {
fmt.Println(pretty.GreenBold("\nNo resource found in this stack."))
return nil
}
Expand All @@ -89,7 +90,7 @@ func (o *PreviewOptions) Run() error {
}

// Compute changes for preview
changes, err := Preview(o, stateStorage, spec, project, stack)
changes, err := Preview(o, stateStorage, sp, project, stack)
if err != nil {
return err
}
Expand Down Expand Up @@ -168,7 +169,8 @@ func Preview(

log.Info("Start call pc.Preview() ...")

cluster := planResources.ParseCluster()
// parse cluster in arguments
cluster := util.ParseClusterArgument(o.Arguments)
rsp, s := pc.Preview(&operation.PreviewRequest{
Request: opsmodels.Request{
Tenant: project.Tenant,
Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"errors"
"strings"
)

func RecoverErr(err *error) {
Expand All @@ -22,3 +23,15 @@ func CheckErr(err error) {
panic(err)
}
}

func ParseClusterArgument(args []string) string {
var cluster string
for _, argument := range args {
// e.g. cluster=xxx
if strings.HasPrefix(argument, "cluster=") {
split := strings.Split(argument, "=")
cluster = split[1]
}
}
return cluster
}
25 changes: 25 additions & 0 deletions pkg/cmd/util/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,28 @@ func TestCheckErr(t *testing.T) {
CheckErr(err)
})
}

func TestParseClusterArgument(t *testing.T) {
type args struct {
args []string
}
tests := []struct {
name string
args args
want string
}{
{
name: "parse_cluster",
args: args{args: []string{"cluster=fake"}},
want: "fake",
},
{name: "parse_empty", args: args{args: nil}, want: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParseClusterArgument(tt.args.args); got != tt.want {
t.Errorf("ParseClusterArgument() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 103d18b

Please sign in to comment.