From 133c98e3d27bbda72417f74ae7414ef56a4f1a94 Mon Sep 17 00:00:00 2001 From: hackerchai Date: Thu, 1 Feb 2024 13:47:10 +0800 Subject: [PATCH] fix(cmd/variable,api/variable): Fix variable update bug due to update readonly vars --- internal/cmd/variable/create/create.go | 6 +++--- internal/cmd/variable/delete/delete.go | 4 ++-- internal/cmd/variable/list/list.go | 11 +++++++++-- internal/cmd/variable/update/update.go | 4 ++-- pkg/api/interface.go | 2 +- pkg/api/variable.go | 16 +++++++++++++--- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/internal/cmd/variable/create/create.go b/internal/cmd/variable/create/create.go index 4f54b1a..9547ed8 100644 --- a/internal/cmd/variable/create/create.go +++ b/internal/cmd/variable/create/create.go @@ -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 } @@ -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}) diff --git a/internal/cmd/variable/delete/delete.go b/internal/cmd/variable/delete/delete.go index 7118038..14c236a 100644 --- a/internal/cmd/variable/delete/delete.go +++ b/internal/cmd/variable/delete/delete.go @@ -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 } @@ -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 { diff --git a/internal/cmd/variable/list/list.go b/internal/cmd/variable/list/list.go index 79687f7..926c5ba 100644 --- a/internal/cmd/variable/list/list.go +++ b/internal/cmd/variable/list/list.go @@ -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 } diff --git a/internal/cmd/variable/update/update.go b/internal/cmd/variable/update/update.go index 99ee0d0..d213142 100644 --- a/internal/cmd/variable/update/update.go +++ b/internal/cmd/variable/update/update.go @@ -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 } @@ -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 { diff --git a/pkg/api/interface.go b/pkg/api/interface.go index 56068c5..69da297 100644 --- a/pkg/api/interface.go +++ b/pkg/api/interface.go @@ -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) } diff --git a/pkg/api/variable.go b/pkg/api/variable.go index 247b66d..6bd46ed 100644 --- a/pkg/api/variable.go +++ b/pkg/api/variable.go @@ -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)"` @@ -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) {