forked from jenkins-zh/jenkins-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
job_edit.go
73 lines (63 loc) · 1.55 KB
/
job_edit.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
package cmd
import (
"fmt"
"log"
"github.com/AlecAivazis/survey"
"github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra"
)
func init() {
jobCmd.AddCommand(jobEditCmd)
}
var jobEditCmd = &cobra.Command{
Use: "edit <jobName>",
Short: "Edit the job of your Jenkins",
Long: `Edit the job of your Jenkins`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
return
}
name := args[0]
var content string
var err error
if content, err = getPipeline(name); err != nil {
log.Fatal(err)
}
prompt := &survey.Editor{
Message: "Edit your pipeline script",
FileName: "*.sh",
Default: content,
HideDefault: true,
AppendDefault: true,
}
if err = survey.AskOne(prompt, &content); err != nil {
log.Fatal(err)
}
jenkins := getCurrentJenkins()
jclient := &client.JobClient{}
jclient.URL = jenkins.URL
jclient.UserName = jenkins.UserName
jclient.Token = jenkins.Token
jclient.Proxy = jenkins.Proxy
jclient.ProxyAuth = jenkins.ProxyAuth
if err = jclient.UpdatePipeline(name, content); err != nil {
fmt.Println("update failed")
log.Fatal(err)
}
},
}
func getPipeline(name string) (script string, err error) {
jenkins := getCurrentJenkins()
jclient := &client.JobClient{}
jclient.URL = jenkins.URL
jclient.UserName = jenkins.UserName
jclient.Token = jenkins.Token
jclient.Proxy = jenkins.Proxy
jclient.ProxyAuth = jenkins.ProxyAuth
var job *client.Pipeline
if job, err = jclient.GetPipeline(name); err == nil {
script = job.Script
}
return
}