Skip to content

Commit

Permalink
Merge pull request #22 from seanmarpo/smarpo--fix-gist-url
Browse files Browse the repository at this point in the history
Fixing gist URLs
  • Loading branch information
tillson committed May 17, 2020
2 parents e5754da + 66924e3 commit d106301
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
12 changes: 4 additions & 8 deletions internal/app/keyword_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ func ScanAndPrintResult(client *http.Client, repo RepoSearchResult) {
if scannedRepos[repo.Repo] {
return
}
var base string
if repo.Source == "repo" {
base = "https://raw.githubusercontent.com"
} else if repo.Source == "gist" {
base = "https://gist.githubusercontent.com"
}
base := GetRawURLForSearchResult(repo)
data, err := DownloadRawFile(client, base, repo)
if err != nil {
log.Fatal(err)
Expand All @@ -68,7 +63,8 @@ func ScanAndPrintResult(client *http.Client, repo RepoSearchResult) {

if len(matches) > 0 {
if !GetFlags().ResultsOnly {
color.Green("[https://github.com/" + repo.Repo + "]")
resultRepoURL := GetRepoURLForSearchResult(repo)
color.Green("[" + resultRepoURL + "]")
}
for _, result := range matches {
if result.KeywordType == "apiKey" {
Expand Down Expand Up @@ -268,7 +264,7 @@ func PrintResultLink(result RepoSearchResult, match Match) {
if file == "" {
file = result.File
}
color.New(color.Faint).Println("https://github.com/" + result.Repo + "/blob/master/" + file)
color.New(color.Faint).Println(result.URL)
}
}

Expand Down
3 changes: 3 additions & 0 deletions internal/app/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RepoSearchResult struct {
Raw string
Source string
Query string
URL string
searchOptions *SearchOptions
}

Expand Down Expand Up @@ -172,6 +173,7 @@ func SearchGitHub(query string, options SearchOptions, client *http.Client, resu
Raw: element[2] + "/master/" + element[4],
Source: "repo",
Query: query,
URL: "https://github.com/" + element[2] + "/blob/" + element[3],
})
}
}
Expand Down Expand Up @@ -266,6 +268,7 @@ func SearchGist(query string, options SearchOptions, client *http.Client, result
Raw: GetRawGistPage(client, element[1]),
Source: "gist",
Query: query,
URL: "https://gist.github.com/" + element[1],
})
}
}
Expand Down
23 changes: 23 additions & 0 deletions internal/app/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,26 @@ func CheckErr(err error) {
log.Fatal(err)
}
}

// GetRepoURLForSearchResult returns the URL of the repo depending on
// RepoSearchResult source
func GetRepoURLForSearchResult(repo RepoSearchResult) string {
if repo.Source == "repo" {
return "https://github.com/" + repo.Repo
} else if repo.Source == "gist" {
return "https://gist.github.com/" + repo.Repo
}
// Left this way in case other Source values ever exist
return ""
}

// GetRawURLForSearchResult returns a raw data URL for a RepoSearchResult
func GetRawURLForSearchResult(repo RepoSearchResult) string {
if repo.Source == "repo" {
return "https://raw.githubusercontent.com"
} else if repo.Source == "gist" {
return "https://gist.githubusercontent.com"
}
// Left this way in case other Source values ever exist
return ""
}

0 comments on commit d106301

Please sign in to comment.