Skip to content

Commit

Permalink
Runner subcommand respecting flags (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkowalski authored Mar 22, 2021
1 parent f36b543 commit 1ddad3c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions cmd/runner/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/CircleCI-Public/circleci-cli/api/runner"
)

func newRunnerInstanceCommand(r *runner.Runner, preRunE validator) *cobra.Command {
func newRunnerInstanceCommand(o *runnerOpts, preRunE validator) *cobra.Command {
cmd := &cobra.Command{
Use: "instance",
Short: "Operate on runner instances",
Expand All @@ -25,7 +25,7 @@ func newRunnerInstanceCommand(r *runner.Runner, preRunE validator) *cobra.Comman
Args: cobra.ExactArgs(1),
PreRunE: preRunE,
RunE: func(_ *cobra.Command, args []string) error {
runners, err := r.GetRunnerInstances(args[0])
runners, err := o.r.GetRunnerInstances(args[0])
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/runner/resource_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/CircleCI-Public/circleci-cli/api/runner"
)

func newResourceClassCommand(r *runner.Runner, preRunE validator) *cobra.Command {
func newResourceClassCommand(o *runnerOpts, preRunE validator) *cobra.Command {
cmd := &cobra.Command{
Use: "resource-class",
Short: "Operate on runner resource-classes",
Expand All @@ -21,7 +21,7 @@ func newResourceClassCommand(r *runner.Runner, preRunE validator) *cobra.Command
Args: cobra.ExactArgs(2),
PreRunE: preRunE,
RunE: func(_ *cobra.Command, args []string) error {
rc, err := r.CreateResourceClass(args[0], args[1])
rc, err := o.r.CreateResourceClass(args[0], args[1])
if err != nil {
return err
}
Expand All @@ -39,11 +39,11 @@ func newResourceClassCommand(r *runner.Runner, preRunE validator) *cobra.Command
Args: cobra.ExactArgs(1),
PreRunE: preRunE,
RunE: func(_ *cobra.Command, args []string) error {
rc, err := r.GetResourceClassByName(args[0])
rc, err := o.r.GetResourceClassByName(args[0])
if err != nil {
return err
}
return r.DeleteResourceClass(rc.ID)
return o.r.DeleteResourceClass(rc.ID)
},
})

Expand All @@ -54,7 +54,7 @@ func newResourceClassCommand(r *runner.Runner, preRunE validator) *cobra.Command
Args: cobra.ExactArgs(1),
PreRunE: preRunE,
RunE: func(_ *cobra.Command, args []string) error {
rcs, err := r.GetResourceClassesByNamespace(args[0])
rcs, err := o.r.GetResourceClassesByNamespace(args[0])
if err != nil {
return err
}
Expand Down
20 changes: 13 additions & 7 deletions cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ import (
"github.com/CircleCI-Public/circleci-cli/settings"
)

type runnerOpts struct {
r *runner.Runner
}

func NewCommand(config *settings.Config, preRunE validator) *cobra.Command {
r := runner.New(rest.New(config.Host, config.RestEndpoint, config.Token))
var opts runnerOpts
cmd := &cobra.Command{
Use: "runner",
Short: "Operate on runners",
Hidden: true,
Use: "runner",
Short: "Operate on runners",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
opts.r = runner.New(rest.New(config.Host, config.RestEndpoint, config.Token))
},
}
cmd.AddCommand(newResourceClassCommand(r, preRunE))
cmd.AddCommand(newTokenCommand(r, preRunE))
cmd.AddCommand(newRunnerInstanceCommand(r, preRunE))
cmd.AddCommand(newResourceClassCommand(&opts, preRunE))
cmd.AddCommand(newTokenCommand(&opts, preRunE))
cmd.AddCommand(newRunnerInstanceCommand(&opts, preRunE))
return cmd
}

Expand Down
10 changes: 4 additions & 6 deletions cmd/runner/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"

"github.com/CircleCI-Public/circleci-cli/api/runner"
)

func newTokenCommand(r *runner.Runner, preRunE validator) *cobra.Command {
func newTokenCommand(o *runnerOpts, preRunE validator) *cobra.Command {
cmd := &cobra.Command{
Use: "token",
Short: "Operate on runner tokens",
Expand All @@ -22,7 +20,7 @@ func newTokenCommand(r *runner.Runner, preRunE validator) *cobra.Command {
Args: cobra.ExactArgs(2),
PreRunE: preRunE,
RunE: func(_ *cobra.Command, args []string) error {
token, err := r.CreateToken(args[0], args[1])
token, err := o.r.CreateToken(args[0], args[1])
if err != nil {
return err
}
Expand All @@ -37,7 +35,7 @@ func newTokenCommand(r *runner.Runner, preRunE validator) *cobra.Command {
Args: cobra.ExactArgs(1),
PreRunE: preRunE,
RunE: func(_ *cobra.Command, args []string) error {
return r.DeleteToken(args[0])
return o.r.DeleteToken(args[0])
},
})

Expand All @@ -48,7 +46,7 @@ func newTokenCommand(r *runner.Runner, preRunE validator) *cobra.Command {
Args: cobra.ExactArgs(1),
PreRunE: preRunE,
RunE: func(_ *cobra.Command, args []string) error {
tokens, err := r.GetRunnerTokensByResourceClass(args[0])
tokens, err := o.r.GetRunnerTokensByResourceClass(args[0])
if err != nil {
return err
}
Expand Down

0 comments on commit 1ddad3c

Please sign in to comment.