diff --git a/app/cmd/common.go b/app/cmd/common.go index 3210c2e0b3fcff9e1ebc28c155d27ca65f0329be..7d21dbd282a254ed4f85a1f1eda4dd798603dbf6 100644 --- a/app/cmd/common.go +++ b/app/cmd/common.go @@ -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, "", " ") diff --git a/app/cmd/job.go b/app/cmd/job.go index d151a094d509e479130d6d5c47b4e0b58883f840..6e8d9b494217764f7a89535fb99b4d27ae4612cb 100644 --- a/app/cmd/job.go +++ b/app/cmd/job.go @@ -1,10 +1,6 @@ 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() }, } diff --git a/app/cmd/job_history.go b/app/cmd/job_history.go new file mode 100644 index 0000000000000000000000000000000000000000..cf70305c6c4bf033fc8c121162cfbd463b80ca4a --- /dev/null +++ b/app/cmd/job_history.go @@ -0,0 +1,72 @@ +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 ", + 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 +} diff --git a/app/cmd/job_search.go b/app/cmd/job_search.go index 89503fa9fc06ad5f5cd2e91a7622899d1fe1ac2f..8b09ea773526f83ab109ee6a66d8118e9a5ecfd7 100644 --- a/app/cmd/job_search.go +++ b/app/cmd/job_search.go @@ -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{ diff --git a/client/job.go b/client/job.go index ffd18fa709d84c1a522550e5dc386d24baec9a60..56626f7cb1c7e28a1be478c5976a61bc29a43bd4 100644 --- a/client/job.go +++ b/client/job.go @@ -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 }