未验证 提交 91b14fab 编写于 作者: Z Zhao Xiaojie 提交者: GitHub

Merge pull request #66 from LinuxSuRen/fix/job-history

Fix job history cannot print the real detail
......@@ -35,6 +35,10 @@ func (o *OutputOption) Output(obj interface{}) (data []byte, err error) {
return nil, fmt.Errorf("not support format %s", o.Format)
}
func (o *OutputOption) SetFlag(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.Format, "output", "o", "table", "Format the output (default 'json')")
}
func Format(obj interface{}, format string) (data []byte, err error) {
if format == JsonOutputFormat {
return json.MarshalIndent(obj, "", " ")
......
package cmd
import (
"fmt"
"log"
"github.com/linuxsuren/jenkins-cli/client"
"github.com/spf13/cobra"
)
......@@ -21,37 +17,13 @@ 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.Flags().BoolVarP(&jobOption.History, "history", "", false, "Print the build history of job")
}
var jobCmd = &cobra.Command{
Use: "job -n",
Use: "job",
Short: "Print the job of your Jenkins",
Long: `Print the job of your Jenkins`,
Run: func(cmd *cobra.Command, args []string) {
if jobOption.Name == "" {
cmd.Help()
return
}
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 jobOption.History {
if builds, err := jclient.GetHistory(jobOption.Name); err == nil {
var data []byte
if data, err = Format(builds, jobOption.Format); err == nil {
fmt.Printf("%s\n", string(data))
} else {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
cmd.Help()
},
}
package cmd
import (
"fmt"
"log"
"os"
"github.com/linuxsuren/jenkins-cli/client"
"github.com/linuxsuren/jenkins-cli/util"
"github.com/spf13/cobra"
)
type JobHistoryOption struct {
OutputOption
}
var jobHistoryOption JobHistoryOption
func init() {
jobCmd.AddCommand(jobHistoryCmd)
jobHistoryOption.SetFlag(jobHistoryCmd)
}
var jobHistoryCmd = &cobra.Command{
Use: "history <keyword>",
Short: "Print the history of job in your Jenkins",
Long: `Print the history of job in your Jenkins`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
return
}
jobName := args[0]
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 builds, err := jclient.GetHistory(jobName); err == nil {
if data, err := jobHistoryOption.Output(builds); err == nil {
if len(data) > 0 {
fmt.Println(string(data))
}
} else {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
},
}
func (o *JobHistoryOption) Output(obj interface{}) (data []byte, err error) {
if data, err = o.OutputOption.Output(obj); err != nil {
buildList := obj.([]client.JobBuild)
table := util.CreateTable(os.Stdout)
table.AddRow("number", "displayname", "building", "result")
for i, build := range buildList {
table.AddRow(fmt.Sprintf("%d", i), build.DisplayName,
fmt.Sprintf("%v", build.Building), build.Result)
}
table.Render()
err = nil
data = []byte{}
}
return
}
......@@ -16,7 +16,7 @@ var jobSearchOption JobSearchOption
func init() {
jobCmd.AddCommand(jobSearchCmd)
jobSearchCmd.PersistentFlags().StringVarP(&queueOption.Format, "output", "o", "json", "Format the output")
jobSearchCmd.Flags().StringVarP(&jobSearchOption.Format, "output", "o", "json", "Format the output")
}
var jobSearchCmd = &cobra.Command{
......
......@@ -344,6 +344,35 @@ func (q *JobClient) GetHistory(name string) (builds []JobBuild, err error) {
var job *Job
if job, err = q.GetJob(name); err == nil {
builds = job.Builds
for i, build := range builds {
api := fmt.Sprintf("%sapi/json", build.URL)
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 {
err = json.Unmarshal(data, &build)
builds[i] = build
} else {
log.Fatal(string(data))
}
} else {
log.Fatal(err)
}
}
}
return
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册