-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
job_create.go
85 lines (71 loc) · 1.87 KB
/
job_create.go
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
79
80
81
82
83
84
85
package cmd
import (
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"net/http"
"github.com/AlecAivazis/survey/v2"
"github.com/jenkins-zh/jenkins-cli/client"
"github.com/spf13/cobra"
)
// JobCreateOption is the job create option
type JobCreateOption struct {
Copy string
Type string
RoundTripper http.RoundTripper
}
var jobCreateOption JobCreateOption
func init() {
jobCmd.AddCommand(jobCreateCmd)
jobCreateCmd.Flags().StringVarP(&jobCreateOption.Copy, "copy", "", "", "Copy an exists job")
jobCreateCmd.Flags().StringVarP(&jobCreateOption.Type, "type", "", "", "Which type do you want to create")
}
var jobCreateCmd = &cobra.Command{
Use: "create <jobName>",
Short: i18n.T("Create a job in your Jenkins"),
Long: i18n.T(`Create a job in your Jenkins`),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
jobName := args[0]
jclient := &client.JobClient{
JenkinsCore: client.JenkinsCore{
RoundTripper: jobCreateOption.RoundTripper,
},
}
getCurrentJenkinsAndClientOrDie(&(jclient.JenkinsCore))
var createMode string
if createMode, err = jobCreateOption.getCreateMode(jclient); err == nil {
payload := client.CreateJobPayload{
Name: jobName,
Mode: createMode,
From: jobCreateOption.Copy,
}
if jobCreateOption.Copy != "" {
payload.Mode = "copy"
}
err = jclient.Create(payload)
}
return
},
}
func (j *JobCreateOption) getCreateMode(jclient *client.JobClient) (mode string, err error) {
mode = j.Type
if j.Copy != "" || mode != "" {
return
}
var types []string
var typeMap map[string]string
typeMap, types, err = GetCategories(jclient)
if err != nil {
return
}
var jobType string
prompt := &survey.Select{
Message: "Choose a job type:",
Options: types,
Default: jobType,
}
if err = survey.AskOne(prompt, &jobType); err != nil {
return
}
mode = typeMap[jobType]
return
}