提交 5b013355 编写于 作者: LinuxSuRen's avatar LinuxSuRen

Add sub-cmd to trigger a job

上级 62ed7a6e
......@@ -6,13 +6,16 @@ import (
type JobOption struct {
OutputOption
Name string
}
var jobOption JobOption
func init() {
rootCmd.AddCommand(jobCmd)
jobCmd.PersistentFlags().StringVarP(&queueOption.Format, "output", "o", "json", "Format the output")
jobCmd.PersistentFlags().StringVarP(&jobOption.Format, "output", "o", "json", "Format the output")
jobCmd.PersistentFlags().StringVarP(&jobOption.Name, "name", "n", "", "Name of the job")
}
var jobCmd = &cobra.Command{
......
package cmd
import (
"log"
"github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra"
)
func init() {
jobCmd.AddCommand(jobBuildCmd)
}
var jobBuildCmd = &cobra.Command{
Use: "build",
Short: "Build the job of your Jenkins",
Long: `Build the job of your Jenkins`,
Run: func(cmd *cobra.Command, args []string) {
if jobOption.Name == "" {
log.Fatal("need a name")
}
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
jclient.Build(jobOption.Name)
},
}
......@@ -9,14 +9,12 @@ import (
)
type JobLogOption struct {
Name string
}
var jobLogOption JobLogOption
func init() {
jobCmd.AddCommand(jobLogCmd)
jobLogCmd.PersistentFlags().StringVarP(&jobLogOption.Name, "name", "n", "", "Name of the job")
}
var jobLogCmd = &cobra.Command{
......@@ -24,7 +22,7 @@ var jobLogCmd = &cobra.Command{
Short: "Print the job of your Jenkins",
Long: `Print the job of your Jenkins`,
Run: func(cmd *cobra.Command, args []string) {
if jobLogOption.Name == "" {
if jobOption.Name == "" {
log.Fatal("need a name")
}
......@@ -36,7 +34,7 @@ var jobLogCmd = &cobra.Command{
jclient.Proxy = jenkins.Proxy
jclient.ProxyAuth = jenkins.ProxyAuth
printLog(jclient, jobLogOption.Name, 0)
printLog(jclient, jobOption.Name, 0)
},
}
......
......@@ -48,6 +48,46 @@ func (q *JobClient) Search(keyword string) (status *SearchResult, err error) {
return
}
func (q *JobClient) Build(jobName string) (err error) {
jobItems := strings.Split(jobName, " ")
path := ""
for _, item := range jobItems {
path = fmt.Sprintf("%s/job/%s", path, item)
}
api := fmt.Sprintf("%s/%s/build", q.URL, path)
var (
req *http.Request
response *http.Response
)
req, err = http.NewRequest("POST", api, nil)
if err == nil {
q.AuthHandle(req)
} else {
return
}
if err = q.CrumbHandle(req); err != nil {
log.Fatal(err)
}
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("build successfully")
} else {
log.Fatal(string(data))
}
} else {
log.Fatal(err)
}
return
}
// Log get the log of a job
func (q *JobClient) Log(jobName string, start int64) (jobLog JobLog, err error) {
jobItems := strings.Split(jobName, " ")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册