From 06dbd3aab490324f3fe1971b09b7e9210907f62f Mon Sep 17 00:00:00 2001 From: sona-tar Date: Wed, 21 Oct 2015 21:41:59 +0900 Subject: [PATCH] [feature] Support limits number of search result(-m, --max). --- CHANGES | 3 +++ ghs.go | 4 ++-- option.go | 5 +++++ release/release_pkg.sh | 6 ++++++ repository.go | 43 +++++++++++++++++++++++++++++++----------- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/CHANGES b/CHANGES index 07395ab..e27320d 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,8 @@ CHANGES ======= +0.0.5 - Oct 21 2015 + * [feature] Support limits number of search result(-m, --max). + 0.0.4 - Jun 04 2015 * [feature] Support Github Enterprise Option(-e, --enterprise). @kou-m * [feature] Support go-latest. check version when ghs -h or -v diff --git a/ghs.go b/ghs.go index bfd3e77..56e18e0 100644 --- a/ghs.go +++ b/ghs.go @@ -1,6 +1,6 @@ package main -const Version string = "0.0.4" +const Version string = "0.0.5" func main() { args, opts := GhsOptionParser() @@ -20,6 +20,6 @@ func main() { query += " repo:" + opts.Repository } - repos := SearchRepository(opts.Sort, opts.Order, query, opts.Enterprise) + repos := SearchRepository(opts.Sort, opts.Order, opts.Max, opts.Enterprise, query) PrintRepository(repos) } diff --git a/option.go b/option.go index 88942a9..f83c18d 100644 --- a/option.go +++ b/option.go @@ -12,6 +12,7 @@ type GhsOptions struct { Language string `short:"l" long:"language" description:"searches repositories based on the language they’re written in."` User string `short:"u" long:"user" description:"limits searches to a specific user name."` Repository string `short:"r" long:"repo" description:"limits searches to a specific repository."` + Max int `short:"m" long:"max" description:"limits number of result. range 1-1000" default:"100"` Version bool `short:"v" long:"version" description:"print version infomation and exit."` Enterprise string `short:"e" long:"enterprise" description:"search from github enterprise."` } @@ -38,6 +39,10 @@ func GhsOptionParser() ([]string, GhsOptions) { ghsOptionError(parser) } + if opts.Max < 1 || opts.Max > 1000 { + ghsOptionError(parser) + } + return args, opts } diff --git a/release/release_pkg.sh b/release/release_pkg.sh index ffac4d5..5ae2e94 100755 --- a/release/release_pkg.sh +++ b/release/release_pkg.sh @@ -11,6 +11,12 @@ # │   ├── ghs-0.0.1-linux_amd64.tar.gz # │   ├── ghs-0.0.1-windows_386.zip # │   └── ghs-0.0.1-windows_amd64.zip +# +# Release Frow +# $ git tag -a ${VERSION} +# $ git push --tags +# $ ./release/create_pkg.sh ${VERSION} +# $ ./release/release_pkg.sh ${VERSION} XC_VERSION=$1 [ -z "${XC_VERSION}" ] && echo "usage : release_pkg.sh " && exit 1 diff --git a/repository.go b/repository.go index b19ee52..70e28ba 100644 --- a/repository.go +++ b/repository.go @@ -6,14 +6,8 @@ import ( "net/url" ) -func SearchRepository(sort string, order string, query string, enterprise string) []github.Repository { +func SearchRepository(sort string, order string, max int, enterprise string, query string) []github.Repository { client := github.NewClient(nil) - searchOpts := &github.SearchOptions{ - Sort: sort, - Order: order, - // TextMatch: true, - // ListOptions: github.ListOptions{Page: 1, PerPage: 1}, - } if enterprise != "" { baseURL, err := url.Parse(enterprise) @@ -24,12 +18,39 @@ func SearchRepository(sort string, order string, query string, enterprise string } } - searchResult, _, err := client.Search.Repositories(query, searchOpts) - if err != nil { - fmt.Printf("Repository not Found\n") + perPage := 100 + + if max < 100 { + perPage = max + } + + searchOpts := &github.SearchOptions{ + Sort: sort, + Order: order, + TextMatch: false, + ListOptions: github.ListOptions{PerPage: perPage}, + } + + var allRepos []github.Repository + i := 0 + + for { + searchResult, resp, err := client.Search.Repositories(query, searchOpts) + if err != nil { + fmt.Printf("Repository not Found\n") + } + + i++ + allRepos = append(allRepos, searchResult.Repositories...) + + if resp.NextPage == 0 || (i*perPage) >= max { + break + } + + searchOpts.ListOptions.Page = resp.NextPage } - return searchResult.Repositories + return allRepos } func PrintRepository(repos []github.Repository) {