Skip to content

Commit

Permalink
[feature] Support limits number of search result(-m, --max).
Browse files Browse the repository at this point in the history
  • Loading branch information
sona-tar committed Oct 21, 2015
1 parent a1eafc2 commit 06dbd3a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions ghs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

const Version string = "0.0.4"
const Version string = "0.0.5"

func main() {
args, opts := GhsOptionParser()
Expand All @@ -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)
}
5 changes: 5 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."`
}
Expand All @@ -38,6 +39,10 @@ func GhsOptionParser() ([]string, GhsOptions) {
ghsOptionError(parser)
}

if opts.Max < 1 || opts.Max > 1000 {
ghsOptionError(parser)
}

return args, opts
}

Expand Down
6 changes: 6 additions & 0 deletions release/release_pkg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <version>" && exit 1
Expand Down
43 changes: 32 additions & 11 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 06dbd3a

Please sign in to comment.