Skip to content

Commit

Permalink
Merge pull request #84 from zeabur/yisheng/zea-2542-fix-cli-variable-…
Browse files Browse the repository at this point in the history
…mutation

fix(cmd/variable,api/variable): Fix variable update bug due to update readonly vars
  • Loading branch information
yuaanlin authored Feb 1, 2024
2 parents b9fc674 + 133c98e commit ca1064d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/variable/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func runCreateVariableNonInteractive(f *cmdutil.Factory, opts *Options) error {
)
s.Start()

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

f.Log.Infof("Successfully created variables of service: %s", opts.name)
f.Log.Infof("Successfully created variables of service: %s\n", opts.name)

table := make([][]string, 0, len(varMap))
for k, v := range varMap {
table = append(table, []string{k, v})
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/variable/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func runDeleteVariableInteractive(f *cmdutil.Factory, opts *Options) error {
)
s.Start()

varList, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
varList, _, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
if err != nil {
return err
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func runDeleteVariableNonInteractive(f *cmdutil.Factory, opts *Options) error {
}
s.Stop()

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

table := make([][]string, 0, len(opts.keys))
for k, v := range opts.keys {
Expand Down
11 changes: 9 additions & 2 deletions internal/cmd/variable/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,25 @@ func runListVariablesNonInteractive(f *cmdutil.Factory, opts *Options) error {
spinner.WithSuffix(fmt.Sprintf(" Fetching environment variablesof service %s ...", opts.name)),
)
s.Start()
variableList, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
variableList, readonlyVariableList, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
if err != nil {
return err
}
s.Stop()

if len(variableList) == 0 {
if len(variableList) == 0 && len(readonlyVariableList) == 0 {
f.Log.Infof("No variables found")
return nil
}

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

if len(readonlyVariableList) != 0 {
fmt.Println()
f.Log.Infof("Readonly variables of service: %s\n", opts.name)
f.Printer.Table(readonlyVariableList.Header(), readonlyVariableList.Rows())
}

return nil
}
4 changes: 2 additions & 2 deletions internal/cmd/variable/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func runUpdateVariableInteractive(f *cmdutil.Factory, opts *Options) error {
)
s.Start()

varList, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
varList, _, err := f.ApiClient.ListVariables(context.Background(), opts.id, opts.environmentID)
if err != nil {
return err
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func runUpdateVariableNonInteractive(f *cmdutil.Factory, opts *Options) error {
}
s.Stop()

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

table := make([][]string, 0, len(opts.keys))
for k, v := range opts.keys {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type (
}

VariableAPI interface {
ListVariables(ctx context.Context, serviceID string, environmentID string) (model.Variables, error)
ListVariables(ctx context.Context, serviceID string, environmentID string) (model.Variables, model.Variables, error)
UpdateVariables(ctx context.Context, serviceID string, environmentID string, data map[string]string) (bool, error)
}

Expand Down
16 changes: 13 additions & 3 deletions pkg/api/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/zeabur/cli/pkg/model"
)

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

if err != nil {
return nil, err
return nil, nil, err
}

return query.Service.Variables, nil
variableList := make(model.Variables, 0, len(query.Service.Variables))
readonlyVariableList := make(model.Variables, 0, len(query.Service.Variables))
for _, variable := range query.Service.Variables {
if variable.ServiceID == serviceID {
variableList = append(variableList, variable)
} else {
readonlyVariableList = append(readonlyVariableList, variable)
}
}

return variableList, readonlyVariableList, nil
}

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

0 comments on commit ca1064d

Please sign in to comment.