未验证 提交 0a35ba73 编写于 作者: Z Zhao Xiaojie 提交者: GitHub

Merge branch 'master' into token-cmd

......@@ -10,13 +10,20 @@ developer, administrator or just a regular user, it borns for you!
# Get started
For the mac user, you just need one command then you can own it.
We support mac, linux and windows for now.
## mac
You can use `brew` to install jcli.
```
brew tap linuxsuren/jcli
brew install jcli
```
## other OS
You can find the right version from the [release page](https://github.com/LinuxSuRen/jenkins-cli/releases). Then download the tar file, cp the uncompressed `jcli` directory into your system path.
# Contribution
It's still under very early develope time. Any contribution is welcome.
......@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/AlecAivazis/survey"
"gopkg.in/yaml.v2"
)
......@@ -12,7 +13,6 @@ type OutputOption struct {
Format string
}
// BatchOption represent the options for a batch operation
type BatchOption struct {
Batch bool
}
......@@ -47,3 +47,24 @@ func Format(obj interface{}, format string) (data []byte, err error) {
return nil, fmt.Errorf("not support format %s", format)
}
// BatchOption represent the options for a batch operation
type BatchOption struct {
Batch bool
}
// Confirm prompte user if they really want to do this
func (b *BatchOption) Confirm(message string) bool {
if !b.Batch {
confirm := false
prompt := &survey.Confirm{
Message: message,
}
survey.AskOne(prompt, &confirm)
if !confirm {
return false
}
}
return true
}
......@@ -21,7 +21,7 @@ func init() {
rootCmd.AddCommand(jobCmd)
jobCmd.PersistentFlags().StringVarP(&jobOption.Format, "output", "o", "json", "Format the output")
jobCmd.PersistentFlags().StringVarP(&jobOption.Name, "name", "n", "", "Name of the job")
jobCmd.PersistentFlags().BoolVarP(&jobOption.History, "history", "", false, "Print the build history of job")
jobCmd.Flags().BoolVarP(&jobOption.History, "history", "", false, "Print the build history of job")
}
var jobCmd = &cobra.Command{
......
package cmd
import (
"fmt"
"github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra"
)
type JobBuildOption struct {
BatchOption
}
var jobBuildOption JobBuildOption
func init() {
jobCmd.AddCommand(jobBuildCmd)
jobBuildCmd.Flags().BoolVarP(&jobBuildOption.Batch, "batch", "b", false, "Batch mode, no need confirm")
}
var jobBuildCmd = &cobra.Command{
......@@ -19,6 +28,10 @@ var jobBuildCmd = &cobra.Command{
return
}
if !jobBuildOption.Confirm(fmt.Sprintf("Are you sure to build job %s", jobOption.Name)) {
return
}
jenkins := getCurrentJenkins()
jclient := &client.JobClient{}
jclient.URL = jenkins.URL
......
package cmd
import (
"errors"
"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 -n",
Short: "Edit the job of your Jenkins",
Long: `Edit the job of your Jenkins`,
Args: func(cmd *cobra.Command, args []string) error {
if jobOption.Name == "" {
return errors.New("requires job name")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
var content string
var err error
if content, err = getPipeline(jobOption.Name); err != nil {
log.Fatal(err)
}
prompt := &survey.Editor{
Message: "Edit your pipeline script",
FileName: "*.sh",
Default: content,
AppendDefault: true,
}
fmt.Println(content)
if err = survey.AskOne(prompt, &content); err != nil {
log.Fatal(err)
}
fmt.Println(content)
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(jobOption.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
}
......@@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
......@@ -125,6 +126,94 @@ func (q *JobClient) GetJob(name string) (job *Job, err error) {
return
}
func (q *JobClient) UpdatePipeline(name, script string) (err error) {
jobItems := strings.Split(name, " ")
path := ""
for i, item := range jobItems {
if i == 0 {
path = fmt.Sprintf("job/%s", item)
} else {
path = fmt.Sprintf("%s/job/%s", path, item)
}
}
api := fmt.Sprintf("%s/%s/wfapisu/update", q.URL, path)
var (
req *http.Request
response *http.Response
)
fmt.Println(api)
formData := url.Values{"script": {script}}
payload := strings.NewReader(formData.Encode())
req, err = http.NewRequest("POST", api, payload)
if err == nil {
q.AuthHandle(req)
} else {
return
}
if err = q.CrumbHandle(req); err != nil {
log.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
// req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
client := q.GetClient()
if response, err = client.Do(req); err == nil {
code := response.StatusCode
var data []byte
data, err = ioutil.ReadAll(response.Body)
if code == 200 {
fmt.Println("updated")
} else {
fmt.Println("code", code)
log.Fatal(string(data))
}
} else {
fmt.Println("request is error")
log.Fatal(err)
}
return
}
func (q *JobClient) GetPipeline(name string) (pipeline *Pipeline, err error) {
jobItems := strings.Split(name, " ")
path := ""
for _, item := range jobItems {
path = fmt.Sprintf("%s/job/%s", path, item)
}
api := fmt.Sprintf("%s/%s/wfapisu/script", q.URL, path)
var (
req *http.Request
response *http.Response
)
req, err = http.NewRequest("GET", api, nil)
if err == nil {
q.AuthHandle(req)
} else {
return
}
client := q.GetClient()
if response, err = client.Do(req); err == nil {
code := response.StatusCode
var data []byte
data, err = ioutil.ReadAll(response.Body)
if code == 200 {
pipeline = &Pipeline{}
err = json.Unmarshal(data, pipeline)
} else {
log.Fatal(string(data))
}
} else {
log.Fatal(err)
}
return
}
func (q *JobClient) GetHistory(name string) (builds []JobBuild, err error) {
var job *Job
if job, err = q.GetJob(name); err == nil {
......@@ -213,3 +302,8 @@ type JobBuild struct {
Number int
URL string
}
type Pipeline struct {
Script string
Sandbox bool
}
module github.com/linuxsuren/jenkins-cli
replace github.com/AlecAivazis/survey v1.8.5 => gopkg.in/AlecAivazis/survey.v1 v1.8.5
go 1.12
require (
github.com/AlecAivazis/survey v1.8.5
github.com/gosuri/uilive v0.0.3 // indirect
github.com/gosuri/uiprogress v0.0.1
github.com/spf13/cobra v0.0.5
gopkg.in/AlecAivazis/survey.v1 v1.8.5 // indirect
gopkg.in/yaml.v2 v2.2.2
)
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gosuri/uilive v0.0.3 h1:kvo6aB3pez9Wbudij8srWo4iY6SFTTxTKOkb+uRCE8I=
github.com/gosuri/uilive v0.0.3/go.mod h1:qkLSc0A5EXSP6B04TrN4oQoxqFI7A8XvoXSlJi8cwk8=
github.com/gosuri/uiprogress v0.0.1/go.mod h1:C1RTYn4Sc7iEyf6j8ft5dyoZ4212h8G1ol9QQluh5+0=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180606202747-9527bec2660b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a h1:1n5lsVfiQW3yfsRGu98756EH1YthsFqr/5mxHduZW2A=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/AlecAivazis/survey.v1 v1.8.5 h1:QoEEmn/d5BbuPIL2qvXwzJdttFFhRQFkaq+tEKb7SMI=
gopkg.in/AlecAivazis/survey.v1 v1.8.5/go.mod h1:iBNOmqKz/NUbZx3bA+4hAGLRC7fSK7tgtVDT4tB22XA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册