@@ -15,55 +15,55 @@ import (
15
15
type SearchOptions struct {
16
16
Query string
17
17
Interactive bool
18
- SortBy string
19
18
SearchIn string
20
19
Limit int
21
20
Topic string
21
+ Language string
22
22
}
23
23
24
24
func rootCmd () * cobra.Command {
25
25
opts := & SearchOptions {}
26
26
cmd := & cobra.Command {
27
- Use : "gh search <repository >" ,
27
+ Use : "gh search <query >" ,
28
28
Short : "search repositories" ,
29
29
Long : `Search for GitHub repositories.
30
30
31
- Search through names, descriptions, or readme's ,
32
- sort by repository stats, and filter by topic .` ,
31
+ Filter with the --topic or --lang flag ,
32
+ or write a custom query with the -q flag .` ,
33
33
Example : `# cli repos with hacktoberfest topic
34
34
$ gh search cli --topic=hacktoberfest
35
35
36
- # 10 most starred cli repos
37
- $ gh search cli --sort=stars --limit=10 ` ,
38
- Args : cobra .ExactArgs (1 ),
36
+ # custom search with GitHub syntax
37
+ $ gh search -q="org:cli created:>2019-01-01" ` ,
38
+ Args : cobra .MaximumNArgs (1 ),
39
39
SilenceErrors : true ,
40
40
RunE : func (cmd * cobra.Command , args []string ) error {
41
- opts .Query = args [0 ]
42
-
43
- searchIn := strings .ToLower (opts .SearchIn )
44
- if searchIn != "name" && searchIn != "description" && searchIn != "readme" {
45
- return errors .New (`--in argument must be "name", "description", or "readme"` )
46
- }
47
-
48
- if cmd .Flags ().Changed ("sort" ) || cmd .Flags ().Changed ("s" ) {
49
- sortBy := strings .ToLower (opts .SortBy )
50
- if sortBy != "stars" && sortBy != "forks" && sortBy != "issues" {
51
- return errors .New (`--sort argument must be "bestmatch", "stars", "forks", or "issues"` )
41
+ if opts .Query == "" {
42
+ if len (args ) < 1 {
43
+ return errors .New ("Error: empty query" )
52
44
}
53
- }
54
-
55
- if opts .Limit <= 0 {
56
- return errors .New ("invalid limit" )
45
+ opts .Query = args [0 ]
46
+ if opts .SearchIn != "" {
47
+ searchIn := strings .ToLower (opts .SearchIn )
48
+ if searchIn != "name" && searchIn != "description" && searchIn != "readme" {
49
+ return errors .New (`--in argument must be "name", "description", or "readme"` )
50
+ }
51
+ }
52
+ if opts .Limit <= 0 {
53
+ return errors .New ("invalid limit" )
54
+ }
55
+ opts .Query = prepareQuery (opts )
57
56
}
58
57
59
58
return runSearch (opts )
60
59
},
61
60
}
62
61
63
62
cmd .Flags ().StringVarP (& opts .Topic , "topic" , "t" , "" , `Specify a topic` )
64
- cmd .Flags ().StringVarP (& opts .SearchIn , "in" , "i" , "name" , `Search in "name", "description", or "readme"` )
65
- cmd .Flags ().StringVarP (& opts .SortBy , "sort" , "s" , "" , `Sort by "stars", "forks", or "issues"` )
66
- cmd .Flags ().IntVarP (& opts .Limit , "limit" , "L" , 30 , "Max number of search results" )
63
+ cmd .Flags ().StringVarP (& opts .SearchIn , "in" , "i" , "" , `Search in "name", "description", or "readme"` )
64
+ cmd .Flags ().StringVarP (& opts .Language , "lang" , "l" , "" , "Search by programming language" )
65
+ cmd .Flags ().IntVarP (& opts .Limit , "limit" , "L" , 50 , "Max number of search results" )
66
+ cmd .Flags ().StringVarP (& opts .Query , "query" , "q" , "" , "Query in GitHub syntax" )
67
67
return cmd
68
68
}
69
69
0 commit comments