Skip to content

Commit

Permalink
feat: add disable filter flag
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Feb 12, 2022
1 parent e07a2ed commit a76a52e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
14 changes: 5 additions & 9 deletions dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
)

var (
NeedInstallError = errors.New(
ErrNeedInstall = errors.New(
"detected deb/rpm/apk package, download directly")
NoBinError = errors.New("binary file not found")
ErrNoBin = errors.New("binary file not found")
)

type GHReleaseDl struct {
Expand Down Expand Up @@ -47,10 +47,6 @@ func (dl *GHReleaseDl) DlTo(path string) (err error) {
}
defer resp.Body.Close()

if err != nil {
return err
}

tmpfile, err := os.Create(dl.BinaryName + ".tmp")
if err != nil {
return err
Expand Down Expand Up @@ -114,7 +110,7 @@ func (dl GHReleaseDl) ExtractBinary() error {
if err := os.Rename(tmpfileName, fileName); err != nil {
panic(err)
}
return NeedInstallError
return ErrNeedInstall
default:
defer os.Remove(tmpfileName)
return fmt.Errorf("unsupported file format: %v", fileExt)
Expand Down Expand Up @@ -144,7 +140,7 @@ func (dl GHReleaseDl) UnZipBinary(r *os.File) (*zip.File, error) {
return f, nil
}
}
return nil, NoBinError
return nil, ErrNoBin
}

func (GHReleaseDl) UnGzBinary(r *os.File) (*gzip.Reader, error) {
Expand Down Expand Up @@ -180,5 +176,5 @@ func (dl GHReleaseDl) UnTargzBinary(r *os.File) (*tar.Reader, error) {
return tarR, nil
}
}
return nil, NoBinError
return nil, ErrNoBin
}
10 changes: 8 additions & 2 deletions gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type APIReleaseAsset struct {
Size int `json:"size"`
}

func (gr GHRelease) GetGHReleases() (*GHReleaseDl, error) {
func (gr GHRelease) GetGHReleases(filterOff bool) (*GHReleaseDl, error) {
var tag string
if gr.TagName == "" {
tag = "latest"
Expand Down Expand Up @@ -77,7 +77,13 @@ func (gr GHRelease) GetGHReleases() (*GHReleaseDl, error) {
}

// Filter or Pick release assets
matchedAssets := filterAssets(filterAssets(releaseAssets, OS), ARCH)
matchedAssets := func() []APIReleaseAsset {
if filterOff {
return releaseAssets
} else {
return filterAssets(filterAssets(releaseAssets, OS), ARCH)
}
}()
matchedIdx := 0
if len(matchedAssets) != 1 {
var choices []string
Expand Down
15 changes: 10 additions & 5 deletions ghdl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ ghdl handles archived or compressed file as well`,
if err != nil {
panic(err)
}
filterOff, err := cmdFlags.GetBool("filter-off")
if err != nil {
panic(err)
}
repo, tag := parseArg(args[0])
ghRelease := ghdl.GHRelease{RepoPath: repo, TagName: tag}
ghReleaseDl, err := ghRelease.GetGHReleases()
ghReleaseDl, err := ghRelease.GetGHReleases(filterOff)
if err != nil {
h.Print(fmt.Sprintf("get gh releases failed: %s", err), h.PrintModeErr)
os.Exit(1)
Expand All @@ -46,10 +50,10 @@ ghdl handles archived or compressed file as well`,
}
if err := ghReleaseDl.ExtractBinary(); err != nil {
switch err {
case ghdl.NeedInstallError:
case ghdl.ErrNeedInstall:
h.Print(fmt.Sprintf("%s. You can install %s with the appropriate commands", err, ghReleaseDl.BinaryName), h.PrintModeInfo)
os.Exit(0)
case ghdl.NoBinError:
case ghdl.ErrNoBin:
h.Print(fmt.Sprintf("%s. Try to specify binary name flag", err), h.PrintModeInfo)
os.Exit(0)
default:
Expand All @@ -72,8 +76,9 @@ func main() {
}

func init() {
rootCmd.PersistentFlags().StringP("name", "n", "", "specify binary file name")
rootCmd.PersistentFlags().StringP("path", "p", ".", "save binary to `path`")
rootCmd.PersistentFlags().StringP("name", "n", "", "specify binary file name to enhance filtering and extracting accuracy")
rootCmd.PersistentFlags().StringP("path", "p", ".", "save binary to `path` and add execute permission to it")
rootCmd.PersistentFlags().BoolP("filter-off", "-F", false, "turn off auto-filtering feature")
}

// parse user/repo[#tagname] arg
Expand Down

0 comments on commit a76a52e

Please sign in to comment.