Skip to content

Commit

Permalink
support single bz2 files, add ddg search
Browse files Browse the repository at this point in the history
  • Loading branch information
jpillora committed Jan 1, 2024
1 parent ff60117 commit fa45bc1
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ curl https://i.jpillora.com/<user>/<repo>@<release>! | bash
```

```sh
# search Google for github repo <query>
# search web for github repo <query>
curl https://i.jpillora.com/<query>! | bash
```

*Or you can use* `wget -qO- <url> | bash`

**Path API**

* `user` Github user (defaults to @jpillora, customisable if you [host your own](#host-your-own), uses Google to pick most relevant `user` when `repo` not found)
* `user` Github user (defaults to @jpillora, customisable if you [host your own](#host-your-own), searches the web to pick most relevant `user` when `repo` not found)
* `repo` Github repository belonging to `user` (**required**)
* `release` Github release name (defaults to the **latest** release)
* `!` When provided, downloads binary directly into `/usr/local/bin/` (defaults to working directory)
Expand Down
7 changes: 5 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (

type Query struct {
User, Program, AsProgram, Release string
MoveToPath, Google, Insecure bool
MoveToPath, Search, Insecure bool
SudoMove bool // deprecated: not used, now automatically detected
}

Expand Down Expand Up @@ -127,7 +127,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if q.Program == "" {
q.Program = q.User
q.User = h.Config.User
q.Google = true
q.Search = true
}
if q.Release == "" {
q.Release = "latest"
}
// micro > nano!
if q.User == "" && q.Program == "micro" {
Expand Down
21 changes: 12 additions & 9 deletions handler/handler_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func (h *Handler) execute(q Query) (Result, error) {
ts := time.Now()
release, assets, err := h.getAssetsNoCache(q)
if err == nil {
//didn't need google
q.Google = false
} else if errors.Is(err, errNotFound) && q.Google {
//use google to auto-detect user...
user, program, gerr := searchGoogle(q.Program)
//didn't need search
q.Search = false
} else if errors.Is(err, errNotFound) && q.Search {
//use ddg/google to auto-detect user...
user, program, gerr := imFeelingLuck(q.Program)
if gerr != nil {
log.Printf("google search failed: %s", gerr)
log.Printf("web search failed: %s", gerr)
} else {
log.Printf("google search found: %s/%s", user, program)
log.Printf("web search found: %s/%s", user, program)
if program != q.Program {
log.Printf("program mismatch: got %s: expected %s", q.Program, program)
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (h *Handler) getAssetsNoCache(q Query) (string, Assets, error) {
log.Printf("fetching asset info for %s/%s@%s", user, repo, release)
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", user, repo)
ghas := ghAssets{}
if release == "" {
if release == "" || release == "latest" {
url += "/latest"
ghr := ghRelease{}
if err := h.get(url, &ghr); err != nil {
Expand Down Expand Up @@ -120,7 +120,10 @@ func (h *Handler) getAssetsNoCache(q Query) (string, Assets, error) {
if fext == "" && ga.Size > 1024*1024 {
fext = ".bin" // +1MB binary
}
if fext != ".bin" && fext != ".zip" && fext != ".gz" && fext != ".tar.gz" && fext != ".tgz" {
switch fext {
case ".bin", ".zip", ".tar.bz", ".tar.bz2", ".bz2", ".gz", ".tar.gz", ".tgz":
// valid
default:
log.Printf("fetched asset has unsupported file type: %s (ext '%s')", ga.Name, fext)
continue
}
Expand Down
30 changes: 21 additions & 9 deletions handler/search.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package handler

import (
"errors"
"fmt"
"log"
"net/http"
"net/url"
"regexp"
)

var searchGithubRe = regexp.MustCompile(`https:\/\/github\.com\/(\w+)\/(\w+)`)

//uses im feeling lucky and grabs the "Location"
//header from the 302, which contains the github repo
func searchGoogle(phrase string) (user, project string, err error) {
func imFeelingLuck(phrase string) (user, project string, err error) {
phrase += " site:github.com"
log.Printf("google search for '%s'", phrase)
// try dgg
v := url.Values{}
v.Set("q", "! " /*I'm feeling lucky*/ +phrase)
if user, project, err := captureRepoLocation(("https://html.duckduckgo.com/html?" + v.Encode())); err == nil {
return user, project, nil
}
// try google
v = url.Values{}
v.Set("btnI", "") //I'm feeling lucky
v.Set("q", phrase)
urlstr := "https://www.google.com/search?" + v.Encode()
req, err := http.NewRequest("GET", urlstr, nil)
if user, project, err := captureRepoLocation(("https://www.google.com/search?" + v.Encode())); err == nil {
return user, project, nil
}
return "", "", errors.New("not found")
}

// uses im feeling lucky and grabs the "Location"
// header from the 302, which contains the github repo
func captureRepoLocation(url string) (user, project string, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", "", err
panic(err)
}
req.Header.Set("Accept", "*/*")
//I'm a browser... :)
Expand All @@ -33,7 +45,7 @@ func searchGoogle(phrase string) (user, project string, err error) {
}
resp.Body.Close()
//assume redirection
if resp.StatusCode != 302 {
if resp.StatusCode/100 != 3 {
return "", "", fmt.Errorf("non-redirect response: %d", resp.StatusCode)
}
//extract Location header URL
Expand Down
2 changes: 1 addition & 1 deletion handler/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

var (
archRe = regexp.MustCompile(`(arm64|arm|386|amd64|x86_64|aarch64|32|64)`)
fileExtRe = regexp.MustCompile(`(\.[a-z][a-z0-9]+)+$`)
fileExtRe = regexp.MustCompile(`(\.tar)?(\.[a-z][a-z0-9]+)$`)
posixOSRe = regexp.MustCompile(`(darwin|linux|(net|free|open)bsd|mac|osx|windows|win)`)
checksumRe = regexp.MustCompile(`(checksums|sha256sums)`)
)
Expand Down
22 changes: 22 additions & 0 deletions handler/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package handler

import "testing"

func TestFilExt(t *testing.T) {
tests := []struct {
file, ext string
}{
{"my.file.tar.gz", ".tar.gz"},
{"my.file.tar.bz2", ".tar.bz2"},
{"my.file.tar.bz", ".tar.bz"},
{"my.file.bz2", ".bz2"},
{"my.file.gz", ".gz"},
{"my.file.tar.zip", ".tar.zip"}, // :(
}
for _, tc := range tests {
ext := getFileExt(tc.file)
if ext != tc.ext {
t.Fatalf("getFileExt(%s) = %s, want %s", tc.file, ext, tc.ext)
}
}
}
7 changes: 5 additions & 2 deletions scripts/install.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ function install {
echo -n " as $ASPROG"
fi
echo -n " (${OS}/${ARCH})"
{{ if .Google }}
#matched using google, give time to cancel
{{ if .Search }}
# web search, give time to cancel
echo -n " in 5 seconds"
for i in 1 2 3 4 5; do
sleep 1
Expand All @@ -116,6 +116,9 @@ function install {
if [[ $FTYPE = ".gz" ]]; then
which gzip > /dev/null || fail "gzip is not installed"
bash -c "$GET $URL" | gzip -d - > $PROG || fail "download failed"
elif [[ $FTYPE = ".bz2" ]]; then
which bzip2 > /dev/null || fail "bzip2 is not installed"
bash -c "$GET $URL" | bzip2 -d - > $PROG || fail "download failed"
elif [[ $FTYPE = ".tar.bz" ]] || [[ $FTYPE = ".tar.bz2" ]]; then
which tar > /dev/null || fail "tar is not installed"
which bzip2 > /dev/null || fail "bzip2 is not installed"
Expand Down
2 changes: 1 addition & 1 deletion scripts/install.txt.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ as: {{ .AsProgram }}{{end}}
release: {{ .Release }}
move-into-path: {{ .MoveToPath }}
sudo-move: {{ .SudoMove }}
used-google: {{ .Google }}
used-search: {{ .Search }}

release assets:
{{ range .Assets }} {{ .Key }}
Expand Down

0 comments on commit fa45bc1

Please sign in to comment.