提交 1e15baa9 编写于 作者: LinuxSuRen's avatar LinuxSuRen

Add support to search all jobs

上级 fc819728
......@@ -8,8 +8,11 @@ import (
"github.com/spf13/cobra"
)
// JobSearchOption is the options of job search command
type JobSearchOption struct {
OutputOption
PrintAll bool
Max int
RoundTripper http.RoundTripper
}
......@@ -18,19 +21,25 @@ var jobSearchOption JobSearchOption
func init() {
jobCmd.AddCommand(jobSearchCmd)
jobSearchCmd.Flags().IntVarP(&jobSearchOption.Max, "max", "", 10, "The number of limitation to print")
jobSearchCmd.Flags().BoolVarP(&jobSearchOption.PrintAll, "all", "", false, "Print all items if there's no keyword")
jobSearchCmd.Flags().StringVarP(&jobSearchOption.Format, "output", "o", "json", "Format the output")
}
var jobSearchCmd = &cobra.Command{
Use: "search <keyword>",
Use: "search [keyword]",
Short: "Print the job of your Jenkins",
Long: `Print the job of your Jenkins`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
if !jobSearchOption.PrintAll && len(args) == 0 {
cmd.Help()
return
}
if jobSearchOption.PrintAll && len(args) == 0 {
args = []string{""}
}
keyword := args[0]
jclient := &client.JobClient{
......@@ -40,7 +49,7 @@ var jobSearchCmd = &cobra.Command{
}
getCurrentJenkinsAndClient(&(jclient.JenkinsCore))
if status, err := jclient.Search(keyword); err == nil {
if status, err := jclient.Search(keyword, jobSearchOption.Max); err == nil {
var data []byte
if data, err = Format(status, queueOption.Format); err == nil {
cmd.Println(string(data))
......
......@@ -10,6 +10,7 @@ import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
)
......@@ -22,7 +23,6 @@ var _ = Describe("job search command", func() {
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
roundTripper = mhttp.NewMockRoundTripper(ctrl)
jobSearchOption.RoundTripper = roundTripper
rootCmd.SetArgs([]string{})
rootOptions.Jenkins = ""
......@@ -36,7 +36,33 @@ var _ = Describe("job search command", func() {
ctrl.Finish()
})
Context("basic cases", func() {
Context("no need roundtripper", func() {
It("search without any options", func() {
data, err := generateSampleConfig()
Expect(err).To(BeNil())
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
Expect(err).To(BeNil())
jobSearchCmd.SetHelpFunc(func(cmd *cobra.Command, _ []string) {
cmd.Print("help")
})
rootCmd.SetArgs([]string{"job", "search"})
buf := new(bytes.Buffer)
rootCmd.SetOutput(buf)
_, err = rootCmd.ExecuteC()
Expect(err).To(BeNil())
Expect(buf.String()).To(Equal("help"))
})
})
Context("basic cases, need roundtrippper", func() {
BeforeEach(func() {
roundTripper = mhttp.NewMockRoundTripper(ctrl)
jobSearchOption.RoundTripper = roundTripper
})
It("should success, search with one result item", func() {
data, err := generateSampleConfig()
Expect(err).To(BeNil())
......@@ -45,7 +71,7 @@ var _ = Describe("job search command", func() {
keyword := "fake"
request, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:8080/jenkins/search/suggest?query=%s", keyword), nil)
request, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:8080/jenkins/search/suggest?query=%s&max=10", keyword), nil)
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
response := &http.Response{
StatusCode: 200,
......@@ -70,6 +96,40 @@ var _ = Describe("job search command", func() {
}
]
}
`))
})
It("should success, search without keyword", func() {
data, err := generateSampleConfig()
Expect(err).To(BeNil())
err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
Expect(err).To(BeNil())
request, _ := http.NewRequest("GET", "http://localhost:8080/jenkins/search/suggest?query=&max=10", nil)
request.SetBasicAuth("admin", "111e3a2f0231198855dceaff96f20540a9")
response := &http.Response{
StatusCode: 200,
Proto: "HTTP/1.1",
Request: request,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"suggestions": [{"name": "fake"}]}`)),
}
roundTripper.EXPECT().
RoundTrip(request).Return(response, nil)
rootCmd.SetArgs([]string{"job", "search", "--all"})
buf := new(bytes.Buffer)
rootCmd.SetOutput(buf)
_, err = rootCmd.ExecuteC()
Expect(err).To(BeNil())
Expect(buf.String()).To(Equal(`{
"Suggestions": [
{
"Name": "fake"
}
]
}
`))
})
})
......
......@@ -19,8 +19,8 @@ type JobClient struct {
}
// Search find a set of jobs by name
func (q *JobClient) Search(keyword string) (status *SearchResult, err error) {
err = q.RequestWithData("GET", fmt.Sprintf("/search/suggest?query=%s", keyword), nil, nil, 200, &status)
func (q *JobClient) Search(keyword string, max int) (status *SearchResult, err error) {
err = q.RequestWithData("GET", fmt.Sprintf("/search/suggest?query=%s&max=%d", keyword, max), nil, nil, 200, &status)
return
}
......
......@@ -36,7 +36,7 @@ var _ = Describe("job test", func() {
It("basic case with one result item", func() {
keyword := "fake"
request, _ := http.NewRequest("GET", fmt.Sprintf("%s%s%s", jobClient.URL, "/search/suggest?query=", keyword), nil)
request, _ := http.NewRequest("GET", fmt.Sprintf("%s%s%s&max=1", jobClient.URL, "/search/suggest?query=", keyword), nil)
response := &http.Response{
StatusCode: 200,
Proto: "HTTP/1.1",
......@@ -46,7 +46,7 @@ var _ = Describe("job test", func() {
roundTripper.EXPECT().
RoundTrip(request).Return(response, nil)
result, err := jobClient.Search(keyword)
result, err := jobClient.Search(keyword, 1)
Expect(err).To(BeNil())
Expect(result).NotTo(BeNil())
Expect(len(result.Suggestions)).To(Equal(1))
......@@ -56,7 +56,7 @@ var _ = Describe("job test", func() {
It("basic case without any result items", func() {
keyword := "fake"
request, _ := http.NewRequest("GET", fmt.Sprintf("%s%s%s", jobClient.URL, "/search/suggest?query=", keyword), nil)
request, _ := http.NewRequest("GET", fmt.Sprintf("%s%s%s&max=1", jobClient.URL, "/search/suggest?query=", keyword), nil)
response := &http.Response{
StatusCode: 200,
Proto: "HTTP/1.1",
......@@ -66,7 +66,7 @@ var _ = Describe("job test", func() {
roundTripper.EXPECT().
RoundTrip(request).Return(response, nil)
result, err := jobClient.Search(keyword)
result, err := jobClient.Search(keyword, 1)
Expect(err).To(BeNil())
Expect(result).NotTo(BeNil())
Expect(len(result.Suggestions)).To(Equal(0))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册