Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search from other known buckets #26

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 142 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -80,11 +84,121 @@ func main() {
wg.Wait()

// print results and exit with status code
if !printResults(matches.data) {
var hasResult bool
hasResult = printResults(matches.data, false)
if !hasResult && !githubRatelimitReached() {
hasResult = printResults(searchRemoteAll(args.query), true)
}

if !hasResult {
fmt.Println("No matches found.")
os.Exit(1)
}
}

func githubRatelimitReached() bool {
var parser fastjson.Parser

response, err := http.Get("https://api.github.com/rate_limit")
if err != nil {
if strings.Contains(err.Error(), "no such host") {
// no internet connection
return true
} else {
check(err)
}
}
defer response.Body.Close()

raw, err := ioutil.ReadAll(response.Body)
check(err)

parse, _ := parser.ParseBytes(raw)
json, _ := parse.Object()

return json.Get("resources").Get("core").GetInt("limit") == 0
}

func searchRemoteAll(term string) matchMap {
var parser fastjson.Parser

raw, err := os.ReadFile(scoopHome() + "\\apps\\scoop\\current\\buckets.json")
check(err)

result, _ := parser.ParseBytes(raw)
object, _ := result.Object()
matches := struct {
sync.Mutex
data matchMap
}{}
matches.data = make(matchMap)
var wg sync.WaitGroup
var hasReachedApiLimit bool = false
object.Visit(func(k []byte, v *fastjson.Value) {
if _, err := os.Stat(scoopHome() + "\\buckets\\" + string(k)); os.IsNotExist(err) {
if !hasReachedApiLimit {
wg.Add(1)
go func(b string, u string) {
var res []match
res, hasReachedApiLimit = searchRemote(u, term)
matches.Lock()
matches.data[b] = res
matches.Unlock()
wg.Done()
}(string(k), string(v.GetStringBytes()))
}

}
})
wg.Wait()
return matches.data
}

func searchRemote(bucketURL string, term string) (res []match, hasReachedApiLimit bool) {
var parser fastjson.Parser

bucketSplit := strings.Split(bucketURL, "/")
apiLink := "https://api.github.com/repos/" + bucketSplit[len(bucketSplit)-2] + "/" + bucketSplit[len(bucketSplit)-1] + "/git/trees/HEAD?recursive=1"

response, err := http.Get(apiLink)
check(err)
defer response.Body.Close()

requestsLeft, err := strconv.Atoi(response.Header.Get("X-RateLimit-Remaining"))
check(err)
hasReachedApiLimit = requestsLeft == 0

raw, err := ioutil.ReadAll(response.Body)
check(err)

json, _ := parser.ParseBytes(raw)
fileTree := json.GetArray("tree")

regex := regexp.MustCompile("(?i)^(?:bucket/)?(.*" + term + ".*)\\.json$")

matches := struct {
sync.Mutex
data []match
}{}
var wg sync.WaitGroup

for _, file := range fileTree {
wg.Add(1)
go func(path []byte) {
matching := regex.FindSubmatch(path)
if len(matching) > 1 {
matches.Lock()
matches.data = append(matches.data, match{name: string(matching[1]), version: "", bin: ""})
matches.Unlock()
}
wg.Done()
}(file.GetStringBytes("path"))
}
wg.Wait()
res = matches.data
return
}

func matchingManifests(path string, term string) (res []match) {
term = strings.ToLower(term)
files, err := os.ReadDir(path)
Expand Down Expand Up @@ -165,7 +279,7 @@ func matchingManifests(path string, term string) (res []match) {
return
}

func printResults(data matchMap) (anyMatches bool) {
func printResults(data matchMap, fromKnownBucket bool) (anyMatches bool) {
// sort by bucket names
entries := 0
sortedKeys := make([]string, 0, len(data))
Expand All @@ -184,15 +298,37 @@ func printResults(data matchMap) (anyMatches bool) {

if len(v) > 0 {
anyMatches = true
}
}

if fromKnownBucket && anyMatches {
fmt.Println("Results from other known buckets...")
fmt.Println("(add them using 'scoop bucket add <name>')")
fmt.Println()
}

for _, k := range sortedKeys {
v := data[k]

if len(v) > 0 {
display.WriteString("'")
display.WriteString(k)
display.WriteString("' bucket:\n")
display.WriteString("' bucket")
if fromKnownBucket {
display.WriteString(" (install using 'scoop install ")
display.WriteString(k)
display.WriteString("/<app>'):\n")
} else {
display.WriteString(":\n")
}
for _, m := range v {
display.WriteString(" ")
display.WriteString(m.name)
display.WriteString(" (")
display.WriteString(m.version)
display.WriteString(")")
if !fromKnownBucket {
display.WriteString(" (")
display.WriteString(m.version)
display.WriteString(")")
}
if m.bin != "" {
display.WriteString(" --> includes '")
display.WriteString(m.bin)
Expand All @@ -204,10 +340,6 @@ func printResults(data matchMap) (anyMatches bool) {
}
}

if !anyMatches {
display.WriteString("No matches found.")
}

os.Stdout.WriteString(display.String())
return
}