Skip to content

Commit 103d18b

Browse files
authored
chore: parse cluster from cmd args (#319)
1 parent 1cbc10e commit 103d18b

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

pkg/cmd/apply/options.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
previewcmd "kusionstack.io/kusion/pkg/cmd/preview"
1414
"kusionstack.io/kusion/pkg/cmd/spec"
15+
"kusionstack.io/kusion/pkg/cmd/util"
1516
"kusionstack.io/kusion/pkg/engine/backend"
1617
_ "kusionstack.io/kusion/pkg/engine/backend/init"
1718
"kusionstack.io/kusion/pkg/engine/models"
@@ -283,7 +284,8 @@ func Apply(
283284
}
284285
close(ac.MsgCh)
285286
} else {
286-
cluster := planResources.ParseCluster()
287+
// parse cluster in arguments
288+
cluster := util.ParseClusterArgument(o.Arguments)
287289
_, st := ac.Apply(&operation.ApplyRequest{
288290
Request: opsmodels.Request{
289291
Tenant: changes.Project().Tenant,
@@ -321,7 +323,8 @@ func Apply(
321323
// if err != nil {
322324
// return err
323325
// }
324-
func Watch(o *ApplyOptions,
326+
func Watch(
327+
o *ApplyOptions,
325328
planResources *models.Spec,
326329
changes *opsmodels.Changes,
327330
) error {

pkg/cmd/preview/options.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
compilecmd "kusionstack.io/kusion/pkg/cmd/compile"
1010
"kusionstack.io/kusion/pkg/cmd/spec"
11+
"kusionstack.io/kusion/pkg/cmd/util"
1112
"kusionstack.io/kusion/pkg/engine/backend"
1213
"kusionstack.io/kusion/pkg/engine/models"
1314
"kusionstack.io/kusion/pkg/engine/operation"
@@ -62,7 +63,7 @@ func (o *PreviewOptions) Run() error {
6263
}
6364

6465
// Get compile result
65-
spec, err := spec.GenerateSpecWithSpinner(&generator.Options{
66+
sp, err := spec.GenerateSpecWithSpinner(&generator.Options{
6667
WorkDir: o.WorkDir,
6768
Filenames: o.Filenames,
6869
Settings: o.Settings,
@@ -77,7 +78,7 @@ func (o *PreviewOptions) Run() error {
7778
}
7879

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

9192
// Compute changes for preview
92-
changes, err := Preview(o, stateStorage, spec, project, stack)
93+
changes, err := Preview(o, stateStorage, sp, project, stack)
9394
if err != nil {
9495
return err
9596
}
@@ -168,7 +169,8 @@ func Preview(
168169

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

171-
cluster := planResources.ParseCluster()
172+
// parse cluster in arguments
173+
cluster := util.ParseClusterArgument(o.Arguments)
172174
rsp, s := pc.Preview(&operation.PreviewRequest{
173175
Request: opsmodels.Request{
174176
Tenant: project.Tenant,

pkg/cmd/util/helpers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package util
22

33
import (
44
"errors"
5+
"strings"
56
)
67

78
func RecoverErr(err *error) {
@@ -22,3 +23,15 @@ func CheckErr(err error) {
2223
panic(err)
2324
}
2425
}
26+
27+
func ParseClusterArgument(args []string) string {
28+
var cluster string
29+
for _, argument := range args {
30+
// e.g. cluster=xxx
31+
if strings.HasPrefix(argument, "cluster=") {
32+
split := strings.Split(argument, "=")
33+
cluster = split[1]
34+
}
35+
}
36+
return cluster
37+
}

pkg/cmd/util/helpers_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ func TestCheckErr(t *testing.T) {
3232
CheckErr(err)
3333
})
3434
}
35+
36+
func TestParseClusterArgument(t *testing.T) {
37+
type args struct {
38+
args []string
39+
}
40+
tests := []struct {
41+
name string
42+
args args
43+
want string
44+
}{
45+
{
46+
name: "parse_cluster",
47+
args: args{args: []string{"cluster=fake"}},
48+
want: "fake",
49+
},
50+
{name: "parse_empty", args: args{args: nil}, want: ""},
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
if got := ParseClusterArgument(tt.args.args); got != tt.want {
55+
t.Errorf("ParseClusterArgument() = %v, want %v", got, tt.want)
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)