Skip to content

Commit ca1064d

Browse files
author
Yuanlin Lin
authored
Merge pull request #84 from zeabur/yisheng/zea-2542-fix-cli-variable-mutation
fix(cmd/variable,api/variable): Fix variable update bug due to update readonly vars
2 parents b9fc674 + 133c98e commit ca1064d

File tree

6 files changed

+30
-13
lines changed

6 files changed

+30
-13
lines changed

internal/cmd/variable/create/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func runCreateVariableNonInteractive(f *cmdutil.Factory, opts *Options) error {
9898
)
9999
s.Start()
100100

101-
varList, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
101+
varList, _, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
102102
if err != nil {
103103
return err
104104
}
@@ -122,8 +122,8 @@ func runCreateVariableNonInteractive(f *cmdutil.Factory, opts *Options) error {
122122
}
123123
s.Stop()
124124

125-
f.Log.Infof("Successfully created variables of service: %s", opts.name)
126-
125+
f.Log.Infof("Successfully created variables of service: %s\n", opts.name)
126+
127127
table := make([][]string, 0, len(varMap))
128128
for k, v := range varMap {
129129
table = append(table, []string{k, v})

internal/cmd/variable/delete/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func runDeleteVariableInteractive(f *cmdutil.Factory, opts *Options) error {
7171
)
7272
s.Start()
7373

74-
varList, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
74+
varList, _, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
7575
if err != nil {
7676
return err
7777
}
@@ -137,7 +137,7 @@ func runDeleteVariableNonInteractive(f *cmdutil.Factory, opts *Options) error {
137137
}
138138
s.Stop()
139139

140-
f.Log.Infof("Successfully deleted variables of service: %s", opts.name)
140+
f.Log.Infof("Successfully deleted variables of service: %s\n", opts.name)
141141

142142
table := make([][]string, 0, len(opts.keys))
143143
for k, v := range opts.keys {

internal/cmd/variable/list/list.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,25 @@ func runListVariablesNonInteractive(f *cmdutil.Factory, opts *Options) error {
7373
spinner.WithSuffix(fmt.Sprintf(" Fetching environment variablesof service %s ...", opts.name)),
7474
)
7575
s.Start()
76-
variableList, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
76+
variableList, readonlyVariableList, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
7777
if err != nil {
7878
return err
7979
}
8080
s.Stop()
8181

82-
if len(variableList) == 0 {
82+
if len(variableList) == 0 && len(readonlyVariableList) == 0 {
8383
f.Log.Infof("No variables found")
8484
return nil
8585
}
8686

87+
f.Log.Infof("Variables of service: %s\n", opts.name)
8788
f.Printer.Table(variableList.Header(), variableList.Rows())
8889

90+
if len(readonlyVariableList) != 0 {
91+
fmt.Println()
92+
f.Log.Infof("Readonly variables of service: %s\n", opts.name)
93+
f.Printer.Table(readonlyVariableList.Header(), readonlyVariableList.Rows())
94+
}
95+
8996
return nil
9097
}

internal/cmd/variable/update/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func runUpdateVariableInteractive(f *cmdutil.Factory, opts *Options) error {
6767
)
6868
s.Start()
6969

70-
varList, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
70+
varList, _, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
7171
if err != nil {
7272
return err
7373
}
@@ -126,7 +126,7 @@ func runUpdateVariableNonInteractive(f *cmdutil.Factory, opts *Options) error {
126126
}
127127
s.Stop()
128128

129-
f.Log.Infof("Successfully updated variables of service: %s", opts.name)
129+
f.Log.Infof("Successfully updated variables of service: %s\n", opts.name)
130130

131131
table := make([][]string, 0, len(opts.keys))
132132
for k, v := range opts.keys {

pkg/api/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type (
6565
}
6666

6767
VariableAPI interface {
68-
ListVariables(ctx context.Context, serviceID string, environmentID string) (model.Variables, error)
68+
ListVariables(ctx context.Context, serviceID string, environmentID string) (model.Variables, model.Variables, error)
6969
UpdateVariables(ctx context.Context, serviceID string, environmentID string, data map[string]string) (bool, error)
7070
}
7171

pkg/api/variable.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/zeabur/cli/pkg/model"
66
)
77

8-
func (c *client) ListVariables(ctx context.Context, serviceID string, environmentID string) (model.Variables, error) {
8+
func (c *client) ListVariables(ctx context.Context, serviceID string, environmentID string) (model.Variables, model.Variables, error) {
99
var query struct {
1010
Service struct {
1111
Variables model.Variables `graphql:"variables(environmentID: $environmentID, exposed: true)"`
@@ -18,10 +18,20 @@ func (c *client) ListVariables(ctx context.Context, serviceID string, environmen
1818
})
1919

2020
if err != nil {
21-
return nil, err
21+
return nil, nil, err
2222
}
2323

24-
return query.Service.Variables, nil
24+
variableList := make(model.Variables, 0, len(query.Service.Variables))
25+
readonlyVariableList := make(model.Variables, 0, len(query.Service.Variables))
26+
for _, variable := range query.Service.Variables {
27+
if variable.ServiceID == serviceID {
28+
variableList = append(variableList, variable)
29+
} else {
30+
readonlyVariableList = append(readonlyVariableList, variable)
31+
}
32+
}
33+
34+
return variableList, readonlyVariableList, nil
2535
}
2636

2737
func (c *client) UpdateVariables(ctx context.Context, serviceID string, environmentID string, data map[string]string) (bool, error) {

0 commit comments

Comments
 (0)