From bff1bf73a725ec850e508246b9429eb8f5f07f81 Mon Sep 17 00:00:00 2001 From: Bruno Rosendo Date: Thu, 1 Sep 2022 16:48:30 +0200 Subject: [PATCH] cli: instantiate viper instead of using a singleton closes #76 --- client/client.go | 4 +-- cmd/close.go | 12 +++---- cmd/delete.go | 8 +++-- cmd/diff.go | 10 ++---- cmd/du.go | 10 ++---- cmd/info.go | 10 ++---- cmd/list.go | 10 ++---- cmd/logs.go | 10 ++---- cmd/ls.go | 11 +++--- cmd/mv.go | 12 +++---- cmd/open.go | 10 ++---- cmd/ping.go | 10 ++---- cmd/quota_show.go | 10 ++---- cmd/rm.go | 11 ++---- cmd/root.go | 73 ++++++++++++++++++++++------------------ cmd/secrets_add.go | 10 ++---- cmd/secrets_delete.go | 10 ++---- cmd/secrets_list.go | 10 ++---- cmd/start.go | 19 +++++------ cmd/status.go | 9 ++--- main.go | 6 ++-- pkg/workflows/manager.go | 13 ++----- 22 files changed, 115 insertions(+), 173 deletions(-) diff --git a/client/client.go b/client/client.go index fac233c..5e7eca3 100644 --- a/client/client.go +++ b/client/client.go @@ -14,8 +14,8 @@ import ( httptransport "github.com/go-openapi/runtime/client" ) -// ApiClient provides a new API client used to communicate with the REANA server. -func ApiClient() (*API, error) { +// NewApiClient provides a new API client used to communicate with the REANA server. +func NewApiClient(viper *viper.Viper) (*API, error) { // disable certificate security checks http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{ InsecureSkipVerify: true, diff --git a/cmd/close.go b/cmd/close.go index 456dfe0..db9ce5a 100644 --- a/cmd/close.go +++ b/cmd/close.go @@ -37,7 +37,7 @@ type closeOptions struct { } // newCloseCmd creates a command to close an interactive session. -func newCloseCmd() *cobra.Command { +func newCloseCmd(api *client.API) *cobra.Command { o := &closeOptions{} cmd := &cobra.Command{ @@ -46,7 +46,7 @@ func newCloseCmd() *cobra.Command { Long: closeDesc, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return o.run(cmd) + return o.run(cmd, api) }, } @@ -62,17 +62,13 @@ func newCloseCmd() *cobra.Command { return cmd } -func (o *closeOptions) run(cmd *cobra.Command) error { +func (o *closeOptions) run(cmd *cobra.Command, api *client.API) error { closeParams := operations.NewCloseInteractiveSessionParams() closeParams.SetAccessToken(&o.token) closeParams.SetWorkflowIDOrName(o.workflow) - api, err := client.ApiClient() - if err != nil { - return err - } log.Infof("Closing an interactive session on %s", o.workflow) - _, err = api.Operations.CloseInteractiveSession(closeParams) + _, err := api.Operations.CloseInteractiveSession(closeParams) if err != nil { return err } diff --git a/cmd/delete.go b/cmd/delete.go index 0102ddf..b4c2495 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -10,6 +10,7 @@ package cmd import ( "fmt" + "reanahub/reana-client-go/client" "reanahub/reana-client-go/pkg/displayer" "reanahub/reana-client-go/pkg/workflows" @@ -39,7 +40,7 @@ type deleteOptions struct { } // newDeleteCmd creates a command to delete a workflow. -func newDeleteCmd() *cobra.Command { +func newDeleteCmd(api *client.API) *cobra.Command { o := &deleteOptions{} cmd := &cobra.Command{ @@ -48,7 +49,7 @@ func newDeleteCmd() *cobra.Command { Long: deleteDesc, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return o.run(cmd) + return o.run(cmd, api) }, } @@ -72,8 +73,9 @@ func newDeleteCmd() *cobra.Command { return cmd } -func (o *deleteOptions) run(cmd *cobra.Command) error { +func (o *deleteOptions) run(cmd *cobra.Command, api *client.API) error { err := workflows.UpdateStatus( + api, o.token, o.workflow, "deleted", diff --git a/cmd/diff.go b/cmd/diff.go index cac7c7a..68d8076 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -48,7 +48,7 @@ type diffOptions struct { } // newDiffCmd creates a command to show diff between two workflows. -func newDiffCmd() *cobra.Command { +func newDiffCmd(api *client.API) *cobra.Command { o := &diffOptions{} cmd := &cobra.Command{ @@ -59,7 +59,7 @@ func newDiffCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { o.workflowA = args[0] o.workflowB = args[1] - return o.run(cmd) + return o.run(cmd, api) }, } @@ -74,7 +74,7 @@ files in the two workspaces are shown.`) return cmd } -func (o *diffOptions) run(cmd *cobra.Command) error { +func (o *diffOptions) run(cmd *cobra.Command, api *client.API) error { diffParams := operations.NewGetWorkflowDiffParams() diffParams.SetAccessToken(&o.token) diffParams.SetWorkflowIDOrNamea(o.workflowA) @@ -83,10 +83,6 @@ func (o *diffOptions) run(cmd *cobra.Command) error { contextLines := fmt.Sprintf("%d", o.unified) diffParams.SetContextLines(&contextLines) - api, err := client.ApiClient() - if err != nil { - return err - } diffResp, err := api.Operations.GetWorkflowDiff(diffParams) if err != nil { return err diff --git a/cmd/du.go b/cmd/du.go index 5de371a..964aa6a 100644 --- a/cmd/du.go +++ b/cmd/du.go @@ -48,7 +48,7 @@ type duOptions struct { } // newDuCmd creates a command to get workspace disk usage. -func newDuCmd() *cobra.Command { +func newDuCmd(api *client.API) *cobra.Command { o := &duOptions{} cmd := &cobra.Command{ @@ -57,7 +57,7 @@ func newDuCmd() *cobra.Command { Long: duDesc, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return o.run(cmd) + return o.run(cmd, api) }, } @@ -84,7 +84,7 @@ func newDuCmd() *cobra.Command { return cmd } -func (o *duOptions) run(cmd *cobra.Command) error { +func (o *duOptions) run(cmd *cobra.Command, api *client.API) error { filters, err := filterer.NewFilters(nil, config.DuMultiFilters, o.filter) if err != nil { return err @@ -103,10 +103,6 @@ func (o *duOptions) run(cmd *cobra.Command) error { } duParams.SetParameters(additionalParams) - api, err := client.ApiClient() - if err != nil { - return err - } duResp, err := api.Operations.GetWorkflowDiskUsage(duParams) if err != nil { return err diff --git a/cmd/info.go b/cmd/info.go index 7e49cbe..269ed38 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -32,7 +32,7 @@ type infoOptions struct { } // newInfoCmd creates a command to list cluster general information. -func newInfoCmd() *cobra.Command { +func newInfoCmd(api *client.API) *cobra.Command { o := &infoOptions{} cmd := &cobra.Command{ @@ -41,7 +41,7 @@ func newInfoCmd() *cobra.Command { Long: infoDesc, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return o.run(cmd) + return o.run(cmd, api) }, } @@ -52,14 +52,10 @@ func newInfoCmd() *cobra.Command { return cmd } -func (o *infoOptions) run(cmd *cobra.Command) error { +func (o *infoOptions) run(cmd *cobra.Command, api *client.API) error { infoParams := operations.NewInfoParams() infoParams.SetAccessToken(o.token) - api, err := client.ApiClient() - if err != nil { - return err - } infoResp, err := api.Operations.Info(infoParams) if err != nil { return err diff --git a/cmd/list.go b/cmd/list.go index 7b3dbcf..21beb22 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -74,7 +74,7 @@ type listOptions struct { } // newListCmd creates a new command for listing workflows and sessions. -func newListCmd() *cobra.Command { +func newListCmd(api *client.API, viper *viper.Viper) *cobra.Command { o := &listOptions{} cmd := &cobra.Command{ @@ -84,7 +84,7 @@ func newListCmd() *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { o.serverURL = viper.GetString("server-url") - return o.run(cmd) + return o.run(cmd, api) }, } @@ -149,7 +149,7 @@ In case a workflow is in progress, its duration as of now will be shown.`, return cmd } -func (o *listOptions) run(cmd *cobra.Command) error { +func (o *listOptions) run(cmd *cobra.Command, api *client.API) error { var runType string if o.listSessions { runType = "interactive" @@ -181,10 +181,6 @@ func (o *listOptions) run(cmd *cobra.Command) error { listParams.SetIncludeWorkspaceSize(&o.includeWorkspaceSize) } - api, err := client.ApiClient() - if err != nil { - return err - } listResp, err := api.Operations.GetWorkflows(listParams) if err != nil { return err diff --git a/cmd/logs.go b/cmd/logs.go index 26867ad..3dc5109 100644 --- a/cmd/logs.go +++ b/cmd/logs.go @@ -75,7 +75,7 @@ type logsOptions struct { } // newLogsCmd creates a command to get workflow logs. -func newLogsCmd() *cobra.Command { +func newLogsCmd(api *client.API) *cobra.Command { o := &logsOptions{} cmd := &cobra.Command{ @@ -84,7 +84,7 @@ func newLogsCmd() *cobra.Command { Long: logsDesc, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return o.run(cmd) + return o.run(cmd, api) }, } @@ -105,7 +105,7 @@ func newLogsCmd() *cobra.Command { return cmd } -func (o *logsOptions) run(cmd *cobra.Command) error { +func (o *logsOptions) run(cmd *cobra.Command, api *client.API) error { filters, err := parseLogsFilters(o.filters) if err != nil { return err @@ -124,10 +124,6 @@ func (o *logsOptions) run(cmd *cobra.Command) error { logsParams.SetSize(&o.size) } - api, err := client.ApiClient() - if err != nil { - return err - } logsResp, err := api.Operations.GetWorkflowLogs(logsParams) if err != nil { return err diff --git a/cmd/ls.go b/cmd/ls.go index fd9be02..2a991e3 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -71,7 +71,7 @@ type lsOptions struct { } // newLsCmd creates a command to list workspace files. -func newLsCmd() *cobra.Command { +func newLsCmd(api *client.API, viper *viper.Viper) *cobra.Command { o := &lsOptions{} cmd := &cobra.Command{ @@ -84,7 +84,7 @@ func newLsCmd() *cobra.Command { if len(args) > 0 { o.fileName = args[0] } - return o.run(cmd) + return o.run(cmd, api) }, } @@ -116,7 +116,8 @@ func newLsCmd() *cobra.Command { return cmd } -func (o *lsOptions) run(cmd *cobra.Command) error { +// Run runs the ls command with the options provided by o. +func (o *lsOptions) run(cmd *cobra.Command, api *client.API) error { header := []string{"name", "size", "last-modified"} filters, err := filterer.NewFilters(nil, header, o.filters) @@ -140,10 +141,6 @@ func (o *lsOptions) run(cmd *cobra.Command) error { lsParams.SetSize(&o.size) } - api, err := client.ApiClient() - if err != nil { - return err - } lsResp, err := api.Operations.GetFiles(lsParams) if err != nil { return err diff --git a/cmd/mv.go b/cmd/mv.go index 5a148e9..99a11e5 100644 --- a/cmd/mv.go +++ b/cmd/mv.go @@ -35,7 +35,7 @@ type mvOptions struct { } // newMvCmd creates a command to move files within workspace. -func newMvCmd() *cobra.Command { +func newMvCmd(api *client.API) *cobra.Command { o := &mvOptions{} cmd := &cobra.Command{ @@ -46,7 +46,7 @@ func newMvCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { o.source = args[0] o.target = args[1] - return o.run(cmd) + return o.run(cmd, api) }, } @@ -62,18 +62,14 @@ func newMvCmd() *cobra.Command { return cmd } -func (o *mvOptions) run(cmd *cobra.Command) error { +func (o *mvOptions) run(cmd *cobra.Command, api *client.API) error { mvParams := operations.NewMoveFilesParams() mvParams.SetAccessToken(&o.token) mvParams.SetWorkflowIDOrName(o.workflow) mvParams.SetSource(o.source) mvParams.SetTarget(o.target) - api, err := client.ApiClient() - if err != nil { - return err - } - _, err = api.Operations.MoveFiles(mvParams) + _, err := api.Operations.MoveFiles(mvParams) if err != nil { return err } diff --git a/cmd/open.go b/cmd/open.go index 372487e..1ba7551 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -48,7 +48,7 @@ type openOptions struct { } // newOpenCmd creates a command to open an interactive session inside the workspace. -func newOpenCmd() *cobra.Command { +func newOpenCmd(api *client.API, viper *viper.Viper) *cobra.Command { o := &openOptions{} cmd := &cobra.Command{ @@ -69,7 +69,7 @@ func newOpenCmd() *cobra.Command { ); err != nil { return err } - return o.run(cmd) + return o.run(cmd, api) }, } @@ -86,7 +86,7 @@ func newOpenCmd() *cobra.Command { return cmd } -func (o *openOptions) run(cmd *cobra.Command) error { +func (o *openOptions) run(cmd *cobra.Command, api *client.API) error { openParams := operations.NewOpenInteractiveSessionParams() openParams.SetAccessToken(&o.token) openParams.SetWorkflowIDOrName(o.workflow) @@ -95,10 +95,6 @@ func (o *openOptions) run(cmd *cobra.Command) error { operations.OpenInteractiveSessionBody{Image: o.image}, ) - api, err := client.ApiClient() - if err != nil { - return err - } log.Infof("Opening an interactive session on %s", o.workflow) openResp, err := api.Operations.OpenInteractiveSession(openParams) if err != nil { diff --git a/cmd/ping.go b/cmd/ping.go index b30ec2b..cb94e00 100644 --- a/cmd/ping.go +++ b/cmd/ping.go @@ -30,7 +30,7 @@ type pingOptions struct { } // newPingCmd creates a command to ping the REANA server. -func newPingCmd() *cobra.Command { +func newPingCmd(api *client.API, viper *viper.Viper) *cobra.Command { o := &pingOptions{} cmd := &cobra.Command{ @@ -40,7 +40,7 @@ func newPingCmd() *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { o.serverURL = viper.GetString("server-url") - return o.run(cmd) + return o.run(cmd, api) }, } @@ -50,14 +50,10 @@ func newPingCmd() *cobra.Command { return cmd } -func (o *pingOptions) run(cmd *cobra.Command) error { +func (o *pingOptions) run(cmd *cobra.Command, api *client.API) error { pingParams := operations.NewGetYouParams() pingParams.SetAccessToken(&o.token) - api, err := client.ApiClient() - if err != nil { - return err - } pingResp, err := api.Operations.GetYou(pingParams) if err != nil { return err diff --git a/cmd/quota_show.go b/cmd/quota_show.go index d9d1bd3..b261227 100644 --- a/cmd/quota_show.go +++ b/cmd/quota_show.go @@ -61,7 +61,7 @@ type quotaShowOptions struct { } // newQuotaShowCmd creates a command to show user quota. -func newQuotaShowCmd() *cobra.Command { +func newQuotaShowCmd(api *client.API) *cobra.Command { o := "aShowOptions{} cmd := &cobra.Command{ @@ -84,7 +84,7 @@ func newQuotaShowCmd() *cobra.Command { } else { o.unspecifiedReport = true } - return o.run(cmd) + return o.run(cmd, api) }, } @@ -106,14 +106,10 @@ func newQuotaShowCmd() *cobra.Command { return cmd } -func (o *quotaShowOptions) run(cmd *cobra.Command) error { +func (o *quotaShowOptions) run(cmd *cobra.Command, api *client.API) error { quotaParams := operations.NewGetYouParams() quotaParams.SetAccessToken(&o.token) - api, err := client.ApiClient() - if err != nil { - return err - } quotaResp, err := api.Operations.GetYou(quotaParams) if err != nil { return err diff --git a/cmd/rm.go b/cmd/rm.go index c6348fd..8393f81 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -38,7 +38,7 @@ type rmOptions struct { } // newRmCmd creates a command to delete files from workspace. -func newRmCmd() *cobra.Command { +func newRmCmd(api *client.API) *cobra.Command { o := &rmOptions{} cmd := &cobra.Command{ @@ -48,7 +48,7 @@ func newRmCmd() *cobra.Command { Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { o.fileNames = args - return o.run(cmd) + return o.run(cmd, api) }, } @@ -64,12 +64,7 @@ func newRmCmd() *cobra.Command { return cmd } -func (o *rmOptions) run(cmd *cobra.Command) error { - api, err := client.ApiClient() - if err != nil { - return err - } - +func (o *rmOptions) run(cmd *cobra.Command, api *client.API) error { hasError := false for _, fileName := range o.fileNames { rmParams := operations.NewDeleteFileParams() diff --git a/cmd/root.go b/cmd/root.go index 1add77f..93d3abe 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,7 +10,9 @@ under the terms of the MIT License; see LICENSE file for more details. package cmd import ( + "fmt" "os" + "reanahub/reana-client-go/client" "reanahub/reana-client-go/pkg/validator" "github.com/spf13/pflag" @@ -27,8 +29,17 @@ type rootOptions struct { // NewRootCmd creates a new root command, responsible for creating all the other subcommands and // setting up the logger and persistent flags. -func NewRootCmd() *cobra.Command { +func NewRootCmd() (*cobra.Command, error) { o := &rootOptions{} + viper := viper.New() + if err := setupViper(viper); err != nil { + return nil, err + } + api, err := client.NewApiClient(viper) + if err != nil { + return nil, fmt.Errorf("error while creating api client:\n%s", err.Error()) + } + cmd := &cobra.Command{ Use: "reana-client", Short: "REANA client for interacting with REANA server.", @@ -36,7 +47,7 @@ func NewRootCmd() *cobra.Command { SilenceUsage: true, SilenceErrors: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - return o.run(cmd) + return o.run(cmd, viper) }, } @@ -47,38 +58,34 @@ func NewRootCmd() *cobra.Command { // Add commands cmd.AddCommand(newVersionCmd()) - cmd.AddCommand(newPingCmd()) - cmd.AddCommand(newInfoCmd()) - cmd.AddCommand(newListCmd()) - cmd.AddCommand(newDuCmd()) - cmd.AddCommand(newOpenCmd()) - cmd.AddCommand(newCloseCmd()) - cmd.AddCommand(newLogsCmd()) - cmd.AddCommand(newStatusCmd()) - cmd.AddCommand(newLsCmd()) - cmd.AddCommand(newDiffCmd()) - cmd.AddCommand(newQuotaShowCmd()) - cmd.AddCommand(newDeleteCmd()) - cmd.AddCommand(newStartCmd()) - cmd.AddCommand(newSecretsAddCmd()) - cmd.AddCommand(newSecretsListCmd()) - cmd.AddCommand(newSecretsDeleteCmd()) - cmd.AddCommand(newRmCmd()) - cmd.AddCommand(newMvCmd()) - - return cmd + cmd.AddCommand(newPingCmd(api, viper)) + cmd.AddCommand(newInfoCmd(api)) + cmd.AddCommand(newListCmd(api, viper)) + cmd.AddCommand(newDuCmd(api)) + cmd.AddCommand(newOpenCmd(api, viper)) + cmd.AddCommand(newCloseCmd(api)) + cmd.AddCommand(newLogsCmd(api)) + cmd.AddCommand(newStatusCmd(api)) + cmd.AddCommand(newLsCmd(api, viper)) + cmd.AddCommand(newDiffCmd(api)) + cmd.AddCommand(newQuotaShowCmd(api)) + cmd.AddCommand(newDeleteCmd(api)) + cmd.AddCommand(newStartCmd(api, viper)) + cmd.AddCommand(newSecretsAddCmd(api)) + cmd.AddCommand(newSecretsListCmd(api)) + cmd.AddCommand(newSecretsDeleteCmd(api)) + cmd.AddCommand(newRmCmd(api)) + cmd.AddCommand(newMvCmd(api)) + + return cmd, nil } -func (o *rootOptions) run(cmd *cobra.Command) error { +func (o *rootOptions) run(cmd *cobra.Command, viper *viper.Viper) error { if err := setupLogger(o.logLevel); err != nil { return err } - if err := setupViper(); err != nil { - return err - } - - if err := validateFlags(cmd); err != nil { + if err := validateFlags(cmd, viper); err != nil { return err } @@ -87,13 +94,13 @@ func (o *rootOptions) run(cmd *cobra.Command) error { } // validateFlags validates access token, server URL and workflow flag values. -func validateFlags(cmd *cobra.Command) error { +func validateFlags(cmd *cobra.Command, viper *viper.Viper) error { token := cmd.Flags().Lookup("access-token") serverURL := viper.GetString("server-url") workflow := cmd.Flags().Lookup("workflow") if token != nil { - if err := bindViperToCmdFlag(token); err != nil { + if err := bindViperToCmdFlag(token, viper); err != nil { return err } tokenValue := token.Value.String() @@ -111,7 +118,7 @@ func validateFlags(cmd *cobra.Command) error { return nil } - if err := bindViperToCmdFlag(workflow); err != nil { + if err := bindViperToCmdFlag(workflow, viper); err != nil { return err } workflowValue := workflow.Value.String() @@ -123,7 +130,7 @@ func validateFlags(cmd *cobra.Command) error { } // setupViper binds environment variable values to the viper keys. -func setupViper() error { +func setupViper(viper *viper.Viper) error { if err := viper.BindEnv("server-url", "REANA_SERVER_URL"); err != nil { return err } @@ -167,7 +174,7 @@ func logCmdFlags(cmd *cobra.Command) { } // bindViperToCmdFlag applies viper config value to the flag when the flag is not set and viper has a value. -func bindViperToCmdFlag(f *pflag.Flag) error { +func bindViperToCmdFlag(f *pflag.Flag, viper *viper.Viper) error { if f != nil && !f.Changed && viper.IsSet(f.Name) { value := viper.GetString(f.Name) if err := f.Value.Set(value); err != nil { diff --git a/cmd/secrets_add.go b/cmd/secrets_add.go index 1abf0bf..af04154 100644 --- a/cmd/secrets_add.go +++ b/cmd/secrets_add.go @@ -47,7 +47,7 @@ type secretsAddOptions struct { } // newSecretsAddCmd creates a command to add secrets from literal string or from file. -func newSecretsAddCmd() *cobra.Command { +func newSecretsAddCmd(api *client.API) *cobra.Command { o := &secretsAddOptions{} cmd := &cobra.Command{ @@ -66,7 +66,7 @@ func newSecretsAddCmd() *cobra.Command { return fmt.Errorf("invalid value for '--file': %s", err.Error()) } } - return o.run(cmd) + return o.run(cmd, api) }, } @@ -80,7 +80,7 @@ e.g. PASSWORD=password123`) return cmd } -func (o *secretsAddOptions) run(cmd *cobra.Command) error { +func (o *secretsAddOptions) run(cmd *cobra.Command, api *client.API) error { secrets, secretNames, err := parseSecrets(o.envSecrets, o.fileSecrets) if err != nil { return err @@ -91,10 +91,6 @@ func (o *secretsAddOptions) run(cmd *cobra.Command) error { addSecretsParams.SetOverwrite(&o.overwrite) addSecretsParams.SetSecrets(secrets) - api, err := client.ApiClient() - if err != nil { - return err - } _, err = api.Operations.AddSecrets(addSecretsParams) if err != nil { return err diff --git a/cmd/secrets_delete.go b/cmd/secrets_delete.go index 3a2bcb8..7c03f19 100644 --- a/cmd/secrets_delete.go +++ b/cmd/secrets_delete.go @@ -32,7 +32,7 @@ type secretsDeleteOptions struct { } // newSecretsDeleteCmd creates a command to delete user secrets by name. -func newSecretsDeleteCmd() *cobra.Command { +func newSecretsDeleteCmd(api *client.API) *cobra.Command { o := &secretsDeleteOptions{} cmd := &cobra.Command{ @@ -42,7 +42,7 @@ func newSecretsDeleteCmd() *cobra.Command { Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { o.secrets = args - return o.run(cmd) + return o.run(cmd, api) }, } @@ -52,15 +52,11 @@ func newSecretsDeleteCmd() *cobra.Command { return cmd } -func (o *secretsDeleteOptions) run(cmd *cobra.Command) error { +func (o *secretsDeleteOptions) run(cmd *cobra.Command, api *client.API) error { deleteSecretsParams := operations.NewDeleteSecretsParams() deleteSecretsParams.SetAccessToken(&o.token) deleteSecretsParams.SetSecrets(o.secrets) - api, err := client.ApiClient() - if err != nil { - return err - } deleteSecretsResp, err := api.Operations.DeleteSecrets(deleteSecretsParams) if err != nil { return handleSecretsDeleteApiError(err) diff --git a/cmd/secrets_list.go b/cmd/secrets_list.go index 6f1ca21..b367031 100644 --- a/cmd/secrets_list.go +++ b/cmd/secrets_list.go @@ -29,7 +29,7 @@ type secretsListOptions struct { } // newSecretsListCmd creates a command to list user secrets. -func newSecretsListCmd() *cobra.Command { +func newSecretsListCmd(api *client.API) *cobra.Command { o := &secretsListOptions{} cmd := &cobra.Command{ @@ -38,7 +38,7 @@ func newSecretsListCmd() *cobra.Command { Long: secretsListDesc, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return o.run(cmd) + return o.run(cmd, api) }, } @@ -48,14 +48,10 @@ func newSecretsListCmd() *cobra.Command { return cmd } -func (o *secretsListOptions) run(cmd *cobra.Command) error { +func (o *secretsListOptions) run(cmd *cobra.Command, api *client.API) error { listSecretsParams := operations.NewGetSecretsParams() listSecretsParams.SetAccessToken(&o.token) - api, err := client.ApiClient() - if err != nil { - return err - } listSecretsResp, err := api.Operations.GetSecrets(listSecretsParams) if err != nil { return err diff --git a/cmd/start.go b/cmd/start.go index 15aff2f..1a8d846 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -53,7 +53,7 @@ type startOptions struct { } // newStartCmd creates a command to start previously created workflow. -func newStartCmd() *cobra.Command { +func newStartCmd(api *client.API, viper *viper.Viper) *cobra.Command { o := &startOptions{} cmd := &cobra.Command{ @@ -63,7 +63,7 @@ func newStartCmd() *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { o.serverURL = viper.GetString("server-url") - return o.run(cmd) + return o.run(cmd, api) }, } @@ -102,13 +102,9 @@ E.g. --debug (workflow engine - cwl)`, return cmd } -func (o *startOptions) run(cmd *cobra.Command) error { - api, err := client.ApiClient() - if err != nil { - return err - } - +func (o *startOptions) run(cmd *cobra.Command, api *client.API) error { if len(o.parameters) > 0 || len(o.options) > 0 { + var err error o.options, o.parameters, err = validateStartOptionsAndParams( api, o.token, o.workflow, o.options, o.parameters, @@ -144,7 +140,7 @@ func (o *startOptions) run(cmd *cobra.Command) error { ) if o.follow { - err = followWorkflowExecution(cmd, currentStatus, o.token, o.serverURL, o.workflow) + err = followWorkflowExecution(cmd, api, currentStatus, o.token, o.serverURL, o.workflow) if err != nil { return err } @@ -190,12 +186,13 @@ func validateStartOptionsAndParams( // If the workflow finishes successfully, this calls the ls command to display the workflow files' URLs. func followWorkflowExecution( cmd *cobra.Command, + api *client.API, currentStatus string, token, serverURL, workflow string, ) error { for slices.Contains([]string{"pending", "queued", "running"}, currentStatus) { time.Sleep(time.Duration(config.CheckInterval) * time.Second) - status, err := workflows.GetStatus(token, workflow) + status, err := workflows.GetStatus(api, token, workflow) if err != nil { return err } @@ -226,7 +223,7 @@ func followWorkflowExecution( displayURLs: true, page: 1, } - err = lsParams.run(cmd) + err = lsParams.run(cmd, api) if err != nil { return err } diff --git a/cmd/status.go b/cmd/status.go index 2ad27a3..087f968 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -10,6 +10,7 @@ package cmd import ( "fmt" + "reanahub/reana-client-go/client" "reanahub/reana-client-go/client/operations" "reanahub/reana-client-go/pkg/displayer" "reanahub/reana-client-go/pkg/formatter" @@ -51,7 +52,7 @@ type statusOptions struct { } // newStatusCmd creates a command to get status of a workflow. -func newStatusCmd() *cobra.Command { +func newStatusCmd(api *client.API) *cobra.Command { o := &statusOptions{} cmd := &cobra.Command{ @@ -60,7 +61,7 @@ func newStatusCmd() *cobra.Command { Long: statusDesc, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return o.run(cmd) + return o.run(cmd, api) }, } @@ -86,8 +87,8 @@ In case a workflow is in progress, its duration as of now will be shown.`, return cmd } -func (o *statusOptions) run(cmd *cobra.Command) error { - payload, err := workflows.GetStatus(o.token, o.workflow) +func (o *statusOptions) run(cmd *cobra.Command, api *client.API) error { + payload, err := workflows.GetStatus(api, o.token, o.workflow) if err != nil { return err } diff --git a/main.go b/main.go index 01d096b..cf4e7e1 100644 --- a/main.go +++ b/main.go @@ -24,8 +24,10 @@ import ( ) func main() { - rootCmd := cmd.NewRootCmd() - err := rootCmd.Execute() + rootCmd, err := cmd.NewRootCmd() + if err == nil { + err = rootCmd.Execute() + } if err != nil { log.Debug(err) diff --git a/pkg/workflows/manager.go b/pkg/workflows/manager.go index 089d9d6..046cd74 100644 --- a/pkg/workflows/manager.go +++ b/pkg/workflows/manager.go @@ -17,6 +17,7 @@ import ( // UpdateStatus updates the status of the specified workflow. func UpdateStatus( + api *client.API, token, workflow, status string, includeWorkspace, includeAllRuns bool, ) error { @@ -33,11 +34,7 @@ func UpdateStatus( Workspace: includeWorkspace, }) - api, err := client.ApiClient() - if err != nil { - return err - } - _, err = api.Operations.SetWorkflowStatus(deleteParams) + _, err := api.Operations.SetWorkflowStatus(deleteParams) if err != nil { return err } @@ -46,15 +43,11 @@ func UpdateStatus( } // GetStatus returns the status information of the specified workflow. -func GetStatus(token, workflow string) (*operations.GetWorkflowStatusOKBody, error) { +func GetStatus(api *client.API, token, workflow string) (*operations.GetWorkflowStatusOKBody, error) { getParams := operations.NewGetWorkflowStatusParams() getParams.SetAccessToken(&token) getParams.SetWorkflowIDOrName(workflow) - api, err := client.ApiClient() - if err != nil { - return nil, err - } resp, err := api.Operations.GetWorkflowStatus(getParams) if err != nil { return nil, err