Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.

Commit 892d851

Browse files
Add runner command
1 parent 2a8e529 commit 892d851

File tree

6 files changed

+311
-8
lines changed

6 files changed

+311
-8
lines changed

commands/runner/delete.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package runner
2+
3+
import (
4+
lab "github.com/lighttiger2505/lab/gitlab"
5+
)
6+
7+
type deleteMethod struct {
8+
runnerClient lab.Runner
9+
project string
10+
id int
11+
}
12+
13+
func (m *deleteMethod) Process() (string, error) {
14+
err := m.runnerClient.RemoveRunner(m.id)
15+
if err != nil {
16+
return "", err
17+
}
18+
return "", nil
19+
}

commands/runner/detail.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package runner
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
lab "github.com/lighttiger2505/lab/gitlab"
8+
)
9+
10+
type detailMethod struct {
11+
runnerClient lab.Runner
12+
id int
13+
}
14+
15+
func (m *detailMethod) Process() (string, error) {
16+
detail, err := m.runnerClient.GetRunnerDetails(m.id)
17+
if err != nil {
18+
return "", err
19+
}
20+
template := `%d
21+
Status: %s
22+
Description: %s
23+
Token: %s
24+
Tag: %s
25+
Version :%s
26+
AccessLevel: %s
27+
MaximumTimeout: %d
28+
`
29+
res := fmt.Sprintf(
30+
template,
31+
detail.ID,
32+
detail.Status,
33+
detail.Description,
34+
detail.Token,
35+
strings.Join(detail.TagList, ", "),
36+
detail.Version,
37+
detail.AccessLevel,
38+
detail.MaximumTimeout,
39+
)
40+
return res, nil
41+
}

commands/runner/list.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package runner
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
7+
lab "github.com/lighttiger2505/lab/gitlab"
8+
"github.com/ryanuber/columnize"
9+
gitlab "github.com/xanzy/go-gitlab"
10+
)
11+
12+
type listMethod struct {
13+
runnerClient lab.Runner
14+
opt *ListOption
15+
project string
16+
}
17+
18+
func (m *listMethod) Process() (string, error) {
19+
runners, err := m.runnerClient.ListProjectRunners(
20+
m.project,
21+
makeListProjectRunnerOptions(m.opt),
22+
)
23+
if err != nil {
24+
return "", err
25+
}
26+
result := columnize.SimpleFormat(listRunnerOutput(runners))
27+
return result, nil
28+
}
29+
30+
func makeListRunnerOptions(opt *ListOption) *gitlab.ListRunnersOptions {
31+
listOption := &gitlab.ListOptions{
32+
Page: 1,
33+
PerPage: opt.Num,
34+
}
35+
listRunnersOptions := &gitlab.ListRunnersOptions{
36+
ListOptions: *listOption,
37+
}
38+
if opt.Scope != "" {
39+
listRunnersOptions.Scope = gitlab.String(opt.Scope)
40+
}
41+
return listRunnersOptions
42+
}
43+
44+
func makeListProjectRunnerOptions(opt *ListOption) *gitlab.ListProjectRunnersOptions {
45+
listOption := &gitlab.ListOptions{
46+
Page: 1,
47+
PerPage: opt.Num,
48+
}
49+
listProjectRunnersOptions := &gitlab.ListProjectRunnersOptions{
50+
ListOptions: *listOption,
51+
}
52+
if opt.Scope != "" {
53+
listProjectRunnersOptions.Scope = gitlab.String(opt.Scope)
54+
}
55+
return listProjectRunnersOptions
56+
}
57+
58+
func listRunnerOutput(runners []*gitlab.Runner) []string {
59+
var outputs []string
60+
for _, runner := range runners {
61+
output := strings.Join([]string{
62+
strconv.Itoa(runner.ID),
63+
runner.Name,
64+
runner.Description,
65+
runner.Status,
66+
}, "|")
67+
outputs = append(outputs, output)
68+
}
69+
return outputs
70+
}

commands/runner/runner.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package runner
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"strconv"
7+
8+
flags "github.com/jessevdk/go-flags"
9+
"github.com/lighttiger2505/lab/commands/internal"
10+
"github.com/lighttiger2505/lab/git"
11+
lab "github.com/lighttiger2505/lab/gitlab"
12+
"github.com/lighttiger2505/lab/ui"
13+
)
14+
15+
const (
16+
ExitCodeOK int = iota //0
17+
ExitCodeError int = iota //1
18+
ExitCodeFileError int = iota //2
19+
)
20+
21+
type Option struct {
22+
ListOption *ListOption `group:"List Options"`
23+
DeleteOption *DeleteOption `group:"Delete Options"`
24+
}
25+
26+
func newParser(opt *Option) *flags.Parser {
27+
opt.ListOption = newListRunnerOption()
28+
parser := flags.NewParser(opt, flags.HelpFlag|flags.PassDoubleDash)
29+
parser.Usage = "project [options]"
30+
return parser
31+
}
32+
33+
type ListOption struct {
34+
Num int `short:"n" long:"num" value-name:"<num>" default:"20" default-mask:"20" description:"Limit the number of runner to output."`
35+
Scope string `long:"scope" value-name:"<scope>" description:"Print only given scope. \"active\", \"paused\", \"online\" \"offline\"."`
36+
}
37+
38+
type DeleteOption struct {
39+
Delete bool `short:"D" long:"delete" description:"delete registed runner."`
40+
}
41+
42+
func newListRunnerOption() *ListOption {
43+
return &ListOption{}
44+
}
45+
46+
type RunnerCommand struct {
47+
UI ui.Ui
48+
Provider lab.Provider
49+
ClientFactory lab.APIClientFactory
50+
}
51+
52+
func (c *RunnerCommand) Synopsis() string {
53+
return "List CI/CD Runner"
54+
}
55+
56+
var opt Option
57+
var parser = newParser(&opt)
58+
59+
func (c *RunnerCommand) Help() string {
60+
buf := &bytes.Buffer{}
61+
parser.WriteHelp(buf)
62+
return buf.String()
63+
}
64+
65+
func (c *RunnerCommand) Run(args []string) int {
66+
parseArgs, err := parser.ParseArgs(args)
67+
if err != nil {
68+
c.UI.Error(err.Error())
69+
return ExitCodeError
70+
}
71+
72+
id, err := validID(parseArgs)
73+
if err != nil {
74+
c.UI.Error(err.Error())
75+
return ExitCodeError
76+
}
77+
78+
if err := c.Provider.Init(); err != nil {
79+
c.UI.Error(err.Error())
80+
return ExitCodeError
81+
}
82+
83+
gitlabRemote, err := c.Provider.GetCurrentRemote()
84+
if err != nil {
85+
c.UI.Error(err.Error())
86+
return ExitCodeError
87+
}
88+
89+
token, err := c.Provider.GetAPIToken(gitlabRemote)
90+
if err != nil {
91+
c.UI.Error(err.Error())
92+
return ExitCodeError
93+
}
94+
95+
if err := c.ClientFactory.Init(gitlabRemote.ApiUrl(), token); err != nil {
96+
c.UI.Error(err.Error())
97+
return ExitCodeError
98+
}
99+
100+
method := c.createMethod(id, opt, gitlabRemote)
101+
res, err := method.Process()
102+
if err != nil {
103+
c.UI.Error(err.Error())
104+
return ExitCodeError
105+
}
106+
107+
if res != "" {
108+
c.UI.Message(res)
109+
}
110+
111+
return ExitCodeOK
112+
}
113+
114+
func (c *RunnerCommand) createMethod(id int, opt Option, remote *git.RemoteInfo) internal.Method {
115+
if id > 0 {
116+
if opt.DeleteOption.Delete {
117+
return &deleteMethod{
118+
runnerClient: c.ClientFactory.GetRunnerClient(),
119+
project: remote.RepositoryFullName(),
120+
id: id,
121+
}
122+
}
123+
return &detailMethod{
124+
runnerClient: c.ClientFactory.GetRunnerClient(),
125+
id: id,
126+
}
127+
}
128+
129+
return &listMethod{
130+
runnerClient: c.ClientFactory.GetRunnerClient(),
131+
opt: opt.ListOption,
132+
project: remote.RepositoryFullName(),
133+
}
134+
}
135+
136+
func validID(args []string) (int, error) {
137+
if len(args) < 1 {
138+
return 0, nil
139+
}
140+
141+
id, err := strconv.Atoi(args[0])
142+
if err != nil {
143+
return 0, fmt.Errorf("Invalid args, please input runner id.")
144+
}
145+
return id, nil
146+
}

gitlab/runner.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
)
88

99
type Runner interface {
10-
ListRunners(opt *gitlab.ListRunnersOptions) ([]*gitlab.Runner, error)
10+
ListAllRunners(opt *gitlab.ListRunnersOptions) ([]*gitlab.Runner, error)
11+
ListProjectRunners(pid string, opt *gitlab.ListProjectRunnersOptions) ([]*gitlab.Runner, error)
12+
GetRunnerDetails(id int) (*gitlab.RunnerDetails, error)
13+
RemoveRunner(iid int) error
1114
}
1215

1316
type RunnerClient struct {
@@ -18,18 +21,34 @@ func NewRunnerClient(client *gitlab.Client) Runner {
1821
return &RunnerClient{Client: client}
1922
}
2023

21-
func (c *RunnerClient) ListRunners(opt *gitlab.ListRunnersOptions) ([]*gitlab.Runner, error) {
22-
res, _, err := c.Client.Runners.ListRunners(opt)
24+
func (c *RunnerClient) ListAllRunners(opt *gitlab.ListRunnersOptions) ([]*gitlab.Runner, error) {
25+
res, _, err := c.Client.Runners.ListAllRunners(opt)
2326
if err != nil {
24-
return nil, fmt.Errorf("failed list tree. %s", err.Error())
27+
return nil, fmt.Errorf("failed list runners. %s", err.Error())
2528
}
2629
return res, nil
2730
}
2831

29-
type MockRunnerClient struct {
30-
MockListRunners func(opt *gitlab.ListRunnersOptions) ([]*gitlab.Runner, error)
32+
func (c *RunnerClient) ListProjectRunners(pid string, opt *gitlab.ListProjectRunnersOptions) ([]*gitlab.Runner, error) {
33+
res, _, err := c.Client.Runners.ListProjectRunners(pid, opt)
34+
if err != nil {
35+
return nil, fmt.Errorf("failed list project runners. %s", err.Error())
36+
}
37+
return res, nil
38+
}
39+
40+
func (c *RunnerClient) RemoveRunner(iid int) error {
41+
_, err := c.Client.Runners.RemoveRunner(iid)
42+
if err != nil {
43+
return fmt.Errorf("failed delete runner. %s", err.Error())
44+
}
45+
return nil
3146
}
3247

33-
func (m *MockRunnerClient) ListRunners(opt *gitlab.ListRunnersOptions) ([]*gitlab.Runner, error) {
34-
return m.MockListRunners(opt)
48+
func (c *RunnerClient) GetRunnerDetails(id int) (*gitlab.RunnerDetails, error) {
49+
res, _, err := c.Client.Runners.GetRunnerDetails(id)
50+
if err != nil {
51+
return nil, fmt.Errorf("failed delete runner. %s", err.Error())
52+
}
53+
return res, nil
3554
}

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/lighttiger2505/lab/commands/issue"
1313
"github.com/lighttiger2505/lab/commands/mr"
1414
"github.com/lighttiger2505/lab/commands/pipeline"
15+
"github.com/lighttiger2505/lab/commands/runner"
1516
"github.com/lighttiger2505/lab/config"
1617
"github.com/lighttiger2505/lab/git"
1718
lab "github.com/lighttiger2505/lab/gitlab"
@@ -140,6 +141,13 @@ func realMain(writer io.Writer, ver, rev string) int {
140141
ClientFactory: &lab.GitlabClientFactory{},
141142
}, nil
142143
},
144+
"runner": func() (cli.Command, error) {
145+
return &runner.RunnerCommand{
146+
UI: ui,
147+
Provider: provider,
148+
ClientFactory: &lab.GitlabClientFactory{},
149+
}, nil
150+
},
143151
}
144152

145153
exitStatus, err := c.Run()

0 commit comments

Comments
 (0)