Skip to content

Commit

Permalink
feat: allow for GitHub extended search syntax (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanhawari authored Jan 19, 2025
1 parent 81d6e1f commit 16325c5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ stew install Stewfile.lock.json
```sh
# Search for a GitHub repo and browse its contents with a terminal UI
stew search ripgrep
stew search fzf user:junegunn language:go # Use GitHub search syntax
```

### Browse
Expand Down
19 changes: 13 additions & 6 deletions cmd/search.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package cmd

import (
"net/url"
"strings"

"github.com/marwanhawari/stew/constants"
stew "github.com/marwanhawari/stew/lib"
)

// Search is executed when you run `stew search`
func Search(cliInput string) {
func Search(cliInput []string) {
sp := constants.LoadingSpinner

err := stew.ValidateCLIInput(cliInput)
stew.CatchAndExit(err)
if len(cliInput) == 0 {
stew.CatchAndExit(stew.EmptyCLIInputError{})
}
for _, input := range cliInput {
err := stew.ValidateGithubSearchQuery(input)
stew.CatchAndExit(err)
}

err = stew.ValidateGithubSearchQuery(cliInput)
stew.CatchAndExit(err)
searchQuery := url.QueryEscape(strings.Join(cliInput, " "))

sp.Start()
githubSearch, err := stew.NewGithubSearch(cliInput)
githubSearch, err := stew.NewGithubSearch(searchQuery)
sp.Stop()
stew.CatchAndExit(err)

Expand Down
2 changes: 1 addition & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var Regex386 = `(?i)(i?386|x86_32|amd32|x32)`
var RegexGithub = `(?i)^[A-Za-z0-9\-]+\/[A-Za-z0-9\_\.\-]+(@.+)?$`

// RegexGithubSearch is a regular express for valid GitHub search queries
var RegexGithubSearch = `(?i)^[A-Za-z0-9\_\.\-\/]+$`
var RegexGithubSearch = `(?i)^[A-Za-z0-9\_\.\-\/\:]+$`

// RegexURL is a regular express for valid URLs
var RegexURL = `(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])`
6 changes: 3 additions & 3 deletions lib/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ var testGithubSearchReadJSON GithubSearch = GithubSearch{
Items: []GithubSearchResult{
{
FullName: "marwanhawari/ppath",
Stars: 7,
Stars: 8,
Language: "Go",
Description: "🌈 A command-line tool to pretty print your system's PATH environment variable.",
},
Expand All @@ -575,14 +575,14 @@ var testGithubSearch GithubSearch = GithubSearch{
Items: []GithubSearchResult{
{
FullName: "marwanhawari/ppath",
Stars: 7,
Stars: 8,
Language: "Go",
Description: "🌈 A command-line tool to pretty print your system's PATH environment variable.",
},
},
}

var testFormattedSearchResults = []string{"marwanhawari/ppath [⭐️7] 🌈 A command-line tool to pretty print your system's PATH environment variable."}
var testFormattedSearchResults = []string{"marwanhawari/ppath [⭐️8] 🌈 A command-line tool to pretty print your system's PATH environment variable."}

func Test_getGithubSearchJSON(t *testing.T) {
type args struct {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
Usage: "Search for a GitHub repo then browse the selected repo's releases and assets. [Ex: stew search ripgrep]",
Aliases: []string{"s"},
Action: func(c *cli.Context) error {
cmd.Search(c.Args().First())
cmd.Search(c.Args())
return nil
},
},
Expand Down

0 comments on commit 16325c5

Please sign in to comment.