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 1, 2022
1 parent 6c1147e commit f1baa0f
Show file tree
Hide file tree
Showing 22 changed files with 114 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/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type options struct {
}

// NewCmd creates a command to close an interactive session.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API) *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Expand All @@ -47,7 +47,7 @@ func NewCmd() *cobra.Command {
Long: description,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(cmd)
return o.run(cmd, api)
},
}

Expand All @@ -63,17 +63,13 @@ func NewCmd() *cobra.Command {
return cmd
}

func (o *options) run(cmd *cobra.Command) error {
func (o *options) 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/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package delete

import (
"fmt"
"reanahub/reana-client-go/client"
"reanahub/reana-client-go/pkg/displayer"
"reanahub/reana-client-go/pkg/workflows"

Expand Down Expand Up @@ -40,7 +41,7 @@ type options struct {
}

// NewCmd creates a command to delete a workflow.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API) *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Expand All @@ -49,7 +50,7 @@ func NewCmd() *cobra.Command {
Long: description,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(cmd)
return o.run(cmd, api)
},
}

Expand All @@ -73,8 +74,9 @@ func NewCmd() *cobra.Command {
return cmd
}

func (o *options) run(cmd *cobra.Command) error {
func (o *options) 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/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type options struct {
}

// NewCmd creates a command to show diff between two workflows.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API) *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Expand All @@ -60,7 +60,7 @@ func NewCmd() *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 @@ -75,7 +75,7 @@ files in the two workspaces are shown.`)
return cmd
}

func (o *options) run(cmd *cobra.Command) error {
func (o *options) run(cmd *cobra.Command, api *client.API) error {
diffParams := operations.NewGetWorkflowDiffParams()
diffParams.SetAccessToken(&o.token)
diffParams.SetWorkflowIDOrNamea(o.workflowA)
Expand All @@ -84,10 +84,6 @@ func (o *options) 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/du.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type options struct {
}

// NewCmd creates a command to get workspace disk usage.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API) *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Expand All @@ -58,7 +58,7 @@ func NewCmd() *cobra.Command {
Long: description,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(cmd)
return o.run(cmd, api)
},
}

Expand All @@ -85,7 +85,7 @@ func NewCmd() *cobra.Command {
return cmd
}

func (o *options) run(cmd *cobra.Command) error {
func (o *options) run(cmd *cobra.Command, api *client.API) error {
filters, err := filterer.NewFilters(nil, config.DuMultiFilters, o.filter)
if err != nil {
return err
Expand All @@ -104,10 +104,6 @@ func (o *options) 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/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type options struct {
}

// NewCmd creates a command to list cluster general information.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API) *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Expand All @@ -42,7 +42,7 @@ func NewCmd() *cobra.Command {
Long: description,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(cmd)
return o.run(cmd, api)
},
}

Expand All @@ -53,14 +53,10 @@ func NewCmd() *cobra.Command {
return cmd
}

func (o *options) run(cmd *cobra.Command) error {
func (o *options) 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/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type options struct {
}

// NewCmd creates a new command for listing workflows and sessions.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API, viper *viper.Viper) *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Expand All @@ -85,7 +85,7 @@ func NewCmd() *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 @@ -150,7 +150,7 @@ In case a workflow is in progress, its duration as of now will be shown.`,
return cmd
}

func (o *options) run(cmd *cobra.Command) error {
func (o *options) run(cmd *cobra.Command, api *client.API) error {
var runType string
if o.listSessions {
runType = "interactive"
Expand Down Expand Up @@ -182,10 +182,6 @@ func (o *options) 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/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type options struct {
}

// NewCmd creates a command to get workflow logs.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API) *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Expand All @@ -85,7 +85,7 @@ func NewCmd() *cobra.Command {
Long: description,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(cmd)
return o.run(cmd, api)
},
}

Expand All @@ -106,7 +106,7 @@ func NewCmd() *cobra.Command {
return cmd
}

func (o *options) run(cmd *cobra.Command) error {
func (o *options) run(cmd *cobra.Command, api *client.API) error {
filters, err := parseFilters(o.filters)
if err != nil {
return err
Expand All @@ -125,10 +125,6 @@ func (o *options) 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
10 changes: 3 additions & 7 deletions cmd/ls/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Options struct {
}

// NewCmd creates a command to list workspace files.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API, viper *viper.Viper) *cobra.Command {
o := &Options{}

cmd := &cobra.Command{
Expand All @@ -86,7 +86,7 @@ func NewCmd() *cobra.Command {
if len(args) > 0 {
o.FileName = args[0]
}
return o.Run(cmd)
return o.Run(cmd, api)
},
}

Expand Down Expand Up @@ -119,7 +119,7 @@ func NewCmd() *cobra.Command {
}

// Run runs the ls command with the options provided by o.
func (o *Options) Run(cmd *cobra.Command) error {
func (o *Options) Run(cmd *cobra.Command, api *client.API) error {
header := []string{"name", "size", "last-modified"}

filters, err := filterer.NewFilters(nil, header, o.Filters)
Expand All @@ -143,10 +143,6 @@ func (o *Options) 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/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type options struct {
}

// NewCmd creates a command to move files within workspace.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API) *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Expand All @@ -47,7 +47,7 @@ func NewCmd() *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 @@ -63,18 +63,14 @@ func NewCmd() *cobra.Command {
return cmd
}

func (o *options) run(cmd *cobra.Command) error {
func (o *options) 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/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type options struct {
}

// NewCmd creates a command to open an interactive session inside the workspace.
func NewCmd() *cobra.Command {
func NewCmd(api *client.API, viper *viper.Viper) *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Expand All @@ -70,7 +70,7 @@ func NewCmd() *cobra.Command {
); err != nil {
return err
}
return o.run(cmd)
return o.run(cmd, api)
},
}

Expand All @@ -87,7 +87,7 @@ func NewCmd() *cobra.Command {
return cmd
}

func (o *options) run(cmd *cobra.Command) error {
func (o *options) run(cmd *cobra.Command, api *client.API) error {
openParams := operations.NewOpenInteractiveSessionParams()
openParams.SetAccessToken(&o.token)
openParams.SetWorkflowIDOrName(o.workflow)
Expand All @@ -96,10 +96,6 @@ func (o *options) 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 f1baa0f

Please sign in to comment.