summaryrefslogtreecommitdiff
path: root/internal/cli/jobs/command.go
blob: a27772086335d77205bd246b18e0725e6fe7cb27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 {
	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
}