From 0a71912398c605ef8176870582b618fc7d24d3e0 Mon Sep 17 00:00:00 2001 From: gek64 <10346089+gek64@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:38:17 +0800 Subject: [PATCH] v2.0.1 --- .gitattributes | 6 +- ...yml => Go Build and Release and Clean.yml} | 2 +- README.md | 41 +++++++++- go.mod | 8 +- go.sum | 14 ++-- internal/download.go | 35 -------- internal/github.go | 28 +++++++ internal/gitlab.go | 28 +++++++ internal/sourceForge.go | 23 ++++++ main.go | 80 ++++++++++++++++--- 10 files changed, 203 insertions(+), 62 deletions(-) rename .github/workflows/{go.yml => Go Build and Release and Clean.yml} (97%) delete mode 100644 internal/download.go create mode 100644 internal/github.go create mode 100644 internal/gitlab.go create mode 100644 internal/sourceForge.go diff --git a/.gitattributes b/.gitattributes index 07764a7..1a18efb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,5 @@ -* text eol=lf \ No newline at end of file +* text=auto +*.bat text eol=crlf +*.ps1 text eol=crlf +*.sh text eol=lf +*.go text eol=lf diff --git a/.github/workflows/go.yml b/.github/workflows/Go Build and Release and Clean.yml similarity index 97% rename from .github/workflows/go.yml rename to .github/workflows/Go Build and Release and Clean.yml index e7bd912..1e4c0cc 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/Go Build and Release and Clean.yml @@ -40,7 +40,7 @@ jobs: go-version: 'stable' - name: Remove old Releases - uses: dev-drprasad/delete-older-releases@v0.3.2 + uses: dev-drprasad/delete-older-releases@v0.3.4 with: keep_latest: 0 delete_tags: true diff --git a/README.md b/README.md index e286ca5..5613e48 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,48 @@ # Release Downloader -- Download Release from GitHub and Sourceforge +- Download release file with a specified name from GitHub, Gitlab, and Sourceforge +- Automatically select the latest version or select by tag name +- Get the release file download link and use it with other download tools, such as curl, wget, aria2, etc. ## Usage +### Github + +```sh +# download latest release +redl -gh "obsproject/obs-studio" -p "OBS-Studio" -p ".zip" -ep "PDBs.zip" + +# download release by tag +redl -gh "obsproject/obs-studio" -t "30.2.2" -p "OBS-Studio" -p ".zip" -ep "PDBs.zip" +``` + +### Gitlab + +```sh +# download latest release +redl -gl "36189" -p ".apk" -ep ".asc" + +# download release by tag +redl -gl "36189" -t "1.18.0" -p ".apk" -ep ".asc" ``` -# download MPV windows from sourceforge + +### SourceForge + +```sh +# download release by rss url redl -sf "https://sourceforge.net/projects/mpv-player-windows/rss?path=/64bit" -p "x86_64" -p ".7z" +``` + +### Use with other download tools + +```sh +# output download link to stdout without download file +redl -gl "36189" -p ".asc" -nd -# download OBS windows from github -redl -gh "obsproject/obs-studio" -p "OBS-Studio" -p ".zip" -ep "pdbs" +# downlaod use other download tool(aria2 curl wget etc) +aria2c "$(redl -gl "36189" -p ".asc" -nd)" +curl -LOJ "$(redl -gl "36189" -p ".asc" -nd)" +wget --content-disposition "$(redl -gl "36189" -p ".asc" -nd)" ``` ## Install diff --git a/go.mod b/go.mod index 015dd3c..d19f1e1 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,14 @@ module redl -go 1.21.4 +go 1.23.2 require ( - github.com/gek64/gek v1.1.3-0.20240627090940-b552e88adf02 - github.com/urfave/cli/v2 v2.27.2 + github.com/gek64/gek v1.1.4 + github.com/urfave/cli/v2 v2.27.5 ) require ( - github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect ) diff --git a/go.sum b/go.sum index 02124e5..b010edb 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,10 @@ -github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= -github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/gek64/gek v1.1.2 h1:ogLhQNbQAXkdNQ5wLTvW+Zgbn30fjqSwy2x+8jDap+E= -github.com/gek64/gek v1.1.2/go.mod h1:yPhb4t8/dTQ57T+5XJ1vUlf0MEsSev2oaIYrBos+GsQ= -github.com/gek64/gek v1.1.3-0.20240627090940-b552e88adf02 h1:UT+46z6eFtGzN7imveZ0zHe39en/mXBkOLe+J1ra9Z0= -github.com/gek64/gek v1.1.3-0.20240627090940-b552e88adf02/go.mod h1:yPhb4t8/dTQ57T+5XJ1vUlf0MEsSev2oaIYrBos+GsQ= +github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc= +github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/gek64/gek v1.1.4 h1:GMBPBGoJmfcNcou2THqy7p9FeXH0StAN+tFsczhxJw4= +github.com/gek64/gek v1.1.4/go.mod h1:1y6E5g/LqY/WYPFsP8cSTFWYbcUzlx4qCS7XPgQO63c= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI= -github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM= +github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w= +github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= diff --git a/internal/download.go b/internal/download.go deleted file mode 100644 index 8596e10..0000000 --- a/internal/download.go +++ /dev/null @@ -1,35 +0,0 @@ -package internal - -import ( - "fmt" - "github.com/gek64/gek/gGithub" - "github.com/gek64/gek/gSourceForge" -) - -func GetSourceForgeDownloadLink(releaseURL string, includes []string, excludes []string) (downloadLink string, err error) { - r, err := gSourceForge.NewRelease(releaseURL) - if err != nil { - return "", err - } - - release := r.SearchRelease(includes, excludes) - - if len(release) <= 0 { - return "", fmt.Errorf("can not find the release") - } - return release[0].Link, nil -} - -func GetGithubDownloadLink(repo string, includes []string, excludes []string) (downloadLink string, err error) { - api, err := gGithub.NewReleaseLatest(repo) - if err != nil { - return "", err - } - - release := api.SearchRelease(includes, excludes) - - if len(release) <= 0 { - return "", fmt.Errorf("can not find the release") - } - return release[0].BrowserDownloadURL, nil -} diff --git a/internal/github.go b/internal/github.go new file mode 100644 index 0000000..e41dfd2 --- /dev/null +++ b/internal/github.go @@ -0,0 +1,28 @@ +package internal + +import ( + "fmt" + "github.com/gek64/gek/gRelease/github" +) + +type GithubAPI struct { + r *github.Release +} + +func GetGithubApiLatest(repo string) (api *GithubAPI, err error) { + r, err := github.GetReleaseLatest(repo) + return &GithubAPI{r}, err +} + +func GetGithubApiByTagName(repo string, tagName string) (api *GithubAPI, err error) { + r, err := github.GetReleaseByTagName(repo, tagName) + return &GithubAPI{r}, err +} + +func (a *GithubAPI) GetDownloadLink(includes []string, excludes []string) (downloadLink string, err error) { + release := a.r.GetAssets(includes, excludes) + if len(release) <= 0 { + return "", fmt.Errorf("can not find the release") + } + return release[0].BrowserDownloadURL, nil +} diff --git a/internal/gitlab.go b/internal/gitlab.go new file mode 100644 index 0000000..b991c33 --- /dev/null +++ b/internal/gitlab.go @@ -0,0 +1,28 @@ +package internal + +import ( + "fmt" + "github.com/gek64/gek/gRelease/gitlab" +) + +type GitlabAPI struct { + r *gitlab.Release +} + +func GetGitlabApiLatest(repo string) (api *GitlabAPI, err error) { + r, err := gitlab.GetReleaseLatest(repo) + return &GitlabAPI{r}, err +} + +func GetGitlabApiByTagName(repo string, tagName string) (api *GitlabAPI, err error) { + r, err := gitlab.GetReleaseByTagName(repo, tagName) + return &GitlabAPI{r}, err +} + +func (a *GitlabAPI) GetDownloadLink(includes []string, excludes []string) (downloadLink string, err error) { + release := a.r.GetAssets(includes, excludes) + if len(release) <= 0 { + return "", fmt.Errorf("can not find the release") + } + return release[0].URL, nil +} diff --git a/internal/sourceForge.go b/internal/sourceForge.go new file mode 100644 index 0000000..760a130 --- /dev/null +++ b/internal/sourceForge.go @@ -0,0 +1,23 @@ +package internal + +import ( + "fmt" + "github.com/gek64/gek/gRelease/sourceForge" +) + +type SourceForgeAPI struct { + r *sourceForge.Release +} + +func GetSourceForgeByRss(rssUrl string) (api *SourceForgeAPI, err error) { + r, err := sourceForge.GetReleaseByRss(rssUrl) + return &SourceForgeAPI{r}, err +} + +func (a *SourceForgeAPI) GetDownloadLink(includes []string, excludes []string) (downloadLink string, err error) { + release := a.r.GetAssets(includes, excludes) + if len(release) <= 0 { + return "", fmt.Errorf("can not find the release") + } + return release[0].Link, nil +} diff --git a/main.go b/main.go index cbe19c8..743481b 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "github.com/gek64/gek/gDownloader" "github.com/gek64/gek/gToolbox" @@ -14,8 +15,10 @@ func main() { var github string var gitlab string var sourceforge string + var tagName string var included_parts cli.StringSlice var excluded_parts cli.StringSlice + var no_download bool var output string flags := []cli.Flag{ @@ -37,6 +40,12 @@ func main() { Usage: "set sourceforge repo url", Destination: &sourceforge, }, + &cli.StringFlag{ + Name: "tag", + Aliases: []string{"t"}, + Usage: "set tag name", + Destination: &tagName, + }, &cli.StringSliceFlag{ Name: "included_parts", Aliases: []string{"p"}, @@ -49,6 +58,12 @@ func main() { Usage: "set release file name excluded parts", Destination: &excluded_parts, }, + &cli.BoolFlag{ + Name: "no_download", + Aliases: []string{"nd"}, + Usage: "output the download link without starting to download the file", + Destination: &no_download, + }, &cli.StringFlag{ Name: "output", Aliases: []string{"o"}, @@ -64,29 +79,76 @@ func main() { app := &cli.App{ Usage: "Release Download Tool", - Version: "v2.00", + Version: "v2.01", Flags: flags, Action: func(ctx *cli.Context) (err error) { var downloadLink string - // 获取下载链接 + // 获取下载地址 if github != "" { - downloadLink, err = internal.GetGithubDownloadLink(github, included_parts.Value(), excluded_parts.Value()) + var a *internal.GithubAPI + if tagName != "" { + a, err = internal.GetGithubApiByTagName(github, tagName) + if err != nil { + return err + } + } else { + a, err = internal.GetGithubApiLatest(github) + if err != nil { + return err + } + } + + downloadLink, err = a.GetDownloadLink(included_parts.Value(), excluded_parts.Value()) + if err != nil { + return err + } + } + if gitlab != "" { + var a *internal.GitlabAPI + if tagName != "" { + a, err = internal.GetGitlabApiByTagName(gitlab, tagName) + if err != nil { + return err + } + } else { + a, err = internal.GetGitlabApiLatest(gitlab) + if err != nil { + return err + } + } + + downloadLink, err = a.GetDownloadLink(included_parts.Value(), excluded_parts.Value()) if err != nil { return err } - } else if gitlab != "" { - fmt.Println("download release from gitlab is under development") - } else if sourceforge != "" { - downloadLink, err = internal.GetSourceForgeDownloadLink(sourceforge, included_parts.Value(), excluded_parts.Value()) + } + if sourceforge != "" { + var a *internal.SourceForgeAPI + if tagName != "" { + return errors.New("sourceforge does not support searching by tag name") + } else { + a, err = internal.GetSourceForgeByRss(sourceforge) + if err != nil { + return err + } + } + + downloadLink, err = a.GetDownloadLink(included_parts.Value(), excluded_parts.Value()) if err != nil { return err } } - // 下载 + // 不进行下载文件的情况 + if no_download { + fmt.Print(downloadLink) + return nil + } + + // 进行下载文件的情况 if downloadLink != "" { - err = gToolbox.CheckToolbox([]string{"curl"}) + _, err = gToolbox.CheckToolbox([]string{"curl"}) if err != nil { err := gDownloader.Download(downloadLink, output, "") if err != nil {