Skip to content

Commit

Permalink
cli: instantiate viper instead of using a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoRosendo committed Sep 2, 2022
1 parent e5b446c commit 4ef294a
Show file tree
Hide file tree
Showing 22 changed files with 115 additions and 173 deletions.
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 4 additions & 8 deletions cmd/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand All @@ -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
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand All @@ -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",
Expand Down
10 changes: 3 additions & 7 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand All @@ -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)
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions cmd/du.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 4 additions & 7 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
12 changes: 4 additions & 8 deletions cmd/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
},
}

Expand All @@ -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
}
Expand Down
10 changes: 3 additions & 7 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -69,7 +69,7 @@ func newOpenCmd() *cobra.Command {
); err != nil {
return err
}
return o.run(cmd)
return o.run(cmd, api)
},
}

Expand All @@ -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)
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 4ef294a

Please sign in to comment.