diff options
author | Valery Piashchynski <[email protected]> | 2022-08-04 23:17:08 +0200 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2022-08-04 23:17:08 +0200 |
commit | bfeb471b3c725102aba0da1f11316bd770fac83f (patch) | |
tree | 715488b63df786ad6eea1e1342e0ac46f887d204 /internal/cli/jobs | |
parent | 024b54348c6d3ac34f48dc17c098d66554385d83 (diff) |
feat: jobs cli commands
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'internal/cli/jobs')
-rw-r--r-- | internal/cli/jobs/command.go | 75 | ||||
-rw-r--r-- | internal/cli/jobs/command_test.go | 17 | ||||
-rw-r--r-- | internal/cli/jobs/render.go | 22 | ||||
-rw-r--r-- | internal/cli/jobs/subcommands.go | 70 |
4 files changed, 184 insertions, 0 deletions
diff --git a/internal/cli/jobs/command.go b/internal/cli/jobs/command.go new file mode 100644 index 00000000..4237839a --- /dev/null +++ b/internal/cli/jobs/command.go @@ -0,0 +1,75 @@ +package jobs + +import ( + "strings" + + internalRpc "github.com/roadrunner-server/roadrunner/v2/internal/rpc" + + "github.com/roadrunner-server/errors" + "github.com/spf13/cobra" +) + +const ( + listRPC string = "jobs.List" + pauseRPC string = "jobs.Pause" + destroyRPC string = "jobs.Destroy" + resumeRPC string = "jobs.Resume" +) + +// NewCommand creates `jobs` command. +func NewCommand(cfgFile *string, override *[]string, silent *bool) *cobra.Command { //nolint:funlen + var ( + pausePipes bool + destroyPipes bool + resumePipes bool + listPipes bool + ) + + cmd := &cobra.Command{ + Use: "jobs", + Short: "Jobs pipelines manipulation", + RunE: func(_ *cobra.Command, args []string) error { + const op = errors.Op("jobs_command") + + if cfgFile == nil { + return errors.E(op, errors.Str("no configuration file provided")) + } + + // for the commands other than list, args[1] should contain list of pipelines to pause/resume/destroy + if !listPipes && len(args[0]) == 0 { + return errors.Str("pause/resume/destroy commands should have list of the pipelines as second arg") + } + + client, err := internalRpc.NewClient(*cfgFile, *override) + if err != nil { + return err + } + + defer func() { _ = client.Close() }() + + switch { + case pausePipes: + split := strings.Split(strings.Trim(args[0], " "), ",") + return pause(client, split, silent) + case destroyPipes: + split := strings.Split(strings.Trim(args[0], " "), ",") + return destroy(client, split, silent) + case resumePipes: + split := strings.Split(strings.Trim(args[0], " "), ",") + return resume(client, split, silent) + case listPipes: + return list(client) + default: + return errors.Str("command should be in form of: `rr jobs pause pipe1,pipe2,etc`") + } + }, + } + + // commands + cmd.Flags().BoolVar(&pausePipes, "pause", false, "pause pipelines") + cmd.Flags().BoolVar(&destroyPipes, "destroy", false, "destroy pipelines") + cmd.Flags().BoolVar(&resumePipes, "resume", false, "resume pipelines") + cmd.Flags().BoolVar(&listPipes, "list", false, "list pipelines") + + return cmd +} diff --git a/internal/cli/jobs/command_test.go b/internal/cli/jobs/command_test.go new file mode 100644 index 00000000..74042479 --- /dev/null +++ b/internal/cli/jobs/command_test.go @@ -0,0 +1,17 @@ +package jobs_test + +import ( + "testing" + + "github.com/roadrunner-server/roadrunner/v2/internal/cli/jobs" + "github.com/stretchr/testify/assert" +) + +func TestCommandProperties(t *testing.T) { + path := "" + f := false + cmd := jobs.NewCommand(&path, nil, &f) + + assert.Equal(t, "jobs", cmd.Use) + assert.NotNil(t, cmd.RunE) +} diff --git a/internal/cli/jobs/render.go b/internal/cli/jobs/render.go new file mode 100644 index 00000000..c9f71e2d --- /dev/null +++ b/internal/cli/jobs/render.go @@ -0,0 +1,22 @@ +package jobs + +import ( + "io" + + "github.com/olekukonko/tablewriter" +) + +// JobsCommandsRender uses console renderer to show jobs +func renderPipelines(writer io.Writer, pipelines []string) *tablewriter.Table { + tw := tablewriter.NewWriter(writer) + tw.SetAutoWrapText(false) + tw.SetHeader([]string{"Pipeline(s)"}) + tw.SetColWidth(50) + tw.SetAlignment(tablewriter.ALIGN_LEFT) + + for i := 0; i < len(pipelines); i++ { + tw.Append([]string{pipelines[i]}) + } + + return tw +} diff --git a/internal/cli/jobs/subcommands.go b/internal/cli/jobs/subcommands.go new file mode 100644 index 00000000..fe47f33f --- /dev/null +++ b/internal/cli/jobs/subcommands.go @@ -0,0 +1,70 @@ +package jobs + +import ( + "net/rpc" + "os" + + jobsv1 "go.buf.build/protocolbuffers/go/roadrunner-server/api/proto/jobs/v1" +) + +func pause(client *rpc.Client, pause []string, silent *bool) error { + pipes := &jobsv1.Pipelines{Pipelines: pause} + er := &jobsv1.Empty{} + + err := client.Call(pauseRPC, pipes, er) + if err != nil { + return err + } + + if !*silent { + renderPipelines(os.Stdout, pause).Render() + } + + return nil +} + +func resume(client *rpc.Client, resume []string, silent *bool) error { + pipes := &jobsv1.Pipelines{Pipelines: resume} + er := &jobsv1.Empty{} + + err := client.Call(resumeRPC, pipes, er) + if err != nil { + return err + } + + if !*silent { + renderPipelines(os.Stdout, resume).Render() + } + + return nil +} + +func destroy(client *rpc.Client, destroy []string, silent *bool) error { + pipes := &jobsv1.Pipelines{Pipelines: destroy} + resp := &jobsv1.Pipelines{} + + err := client.Call(destroyRPC, pipes, resp) + if err != nil { + return err + } + + if !*silent { + renderPipelines(os.Stdout, resp.GetPipelines()).Render() + } + + return nil +} + +func list(client *rpc.Client) error { + resp := &jobsv1.Pipelines{} + er := &jobsv1.Empty{} + + err := client.Call(listRPC, er, resp) + if err != nil { + return err + } + + renderPipelines(os.Stdout, resp.GetPipelines()).Render() + + return nil +} |