fixed github api limit

This commit is contained in:
2024-05-21 17:05:43 +02:00
parent 064f30fc39
commit 57dfe8a6af
4 changed files with 12 additions and 25 deletions
+5 -4
View File
@@ -23,6 +23,7 @@ type Package struct {
type PackageIndex struct { type PackageIndex struct {
Name string `json:"name"` Name string `json:"name"`
Repo string `json:"repo"` Repo string `json:"repo"`
Latest string `json:"latest"`
Message string `json:"message"` Message string `json:"message"`
} }
@@ -48,17 +49,17 @@ func sendRequest(method, url string, jsonBody []byte) Request {
return Request{Body: resBody, StatusCode: res.StatusCode} return Request{Body: resBody, StatusCode: res.StatusCode}
} }
func getRepoURL(packageName string) (string, error) { func getPackage(packageName string) (PackageIndex, error) {
var pkgidx PackageIndex var pkgidx PackageIndex
jsonBody := []byte(fmt.Sprintf(`{"name": "%s"}`, packageName)) jsonBody := []byte(fmt.Sprintf(`{"name": "%s"}`, packageName))
req := sendRequest(http.MethodGet, "http://localhost:3000", jsonBody) req := sendRequest(http.MethodGet, "http://localhost:3000", jsonBody)
err := json.Unmarshal(req.Body, &pkgidx) err := json.Unmarshal(req.Body, &pkgidx)
if err != nil { if err != nil {
fmt.Println("Json Error: ", err) fmt.Println("Json Error: ", err)
return "", err return pkgidx, err
} }
if req.StatusCode != 200 { if req.StatusCode != 200 {
return "", fmt.Errorf(pkgidx.Message) return pkgidx, fmt.Errorf(pkgidx.Message)
} }
return pkgidx.Repo, nil return pkgidx, nil
} }
-12
View File
@@ -2,7 +2,6 @@ package main
import ( import (
"archive/zip" "archive/zip"
"encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@@ -119,17 +118,6 @@ func Unzip(src, dest string) error {
return nil return nil
} }
func getLatestVersion(repoU string) (string, error) {
jsonBody := []byte("")
res := sendRequest(http.MethodGet, fmt.Sprintf("https://api.github.com/repos%s/releases/latest", repoU), jsonBody)
var release Release
err := json.Unmarshal([]byte(string(res.Body)), &release)
if err != nil || res.StatusCode != 200 {
return "", err
}
return release.TagName, nil
}
func downloadPackage(repoU, tag string) error { func downloadPackage(repoU, tag string) error {
resp, err := http.Get(fmt.Sprintf("https://github.com/%s/archive/refs/tags/%s.zip", strings.Trim(repoU, "/"), tag)) resp, err := http.Get(fmt.Sprintf("https://github.com/%s/archive/refs/tags/%s.zip", strings.Trim(repoU, "/"), tag))
if err != nil { if err != nil {
+3 -2
View File
@@ -5,7 +5,7 @@ import "fmt"
func showPackageInfo(packageName string) { func showPackageInfo(packageName string) {
progress := ProgressBar{total: 4, length: 20} progress := ProgressBar{total: 4, length: 20}
progress.change(3, "", "Fetching") progress.change(3, "", "Fetching")
repoURL, err := getRepoURL(packageName) pkgidx, err := getPackage(packageName)
if err != nil { if err != nil {
fmt.Printf("\nError getting repo URL: %v\n", err) fmt.Printf("\nError getting repo URL: %v\n", err)
return return
@@ -25,7 +25,8 @@ func showPackageInfo(packageName string) {
// } // }
fmt.Printf("Package: %s\n", packageName) fmt.Printf("Package: %s\n", packageName)
fmt.Printf("Repo: %s\n", repoURL) fmt.Printf("Repo: %s\n", pkgidx.Repo)
fmt.Printf("Version: %s\n", pkgidx.Latest)
// for version, info := range pkgIndex.Versions { // for version, info := range pkgIndex.Versions {
// fmt.Printf("Version: %s\n", version) // fmt.Printf("Version: %s\n", version)
// fmt.Printf(" Author: %s\n", info.Author) // fmt.Printf(" Author: %s\n", info.Author)
+4 -7
View File
@@ -22,21 +22,18 @@ type PackageVersion struct {
func installPackage(packageName, packageVersion string) { func installPackage(packageName, packageVersion string) {
progress := ProgressBar{total: 10, length: 20, enabled: true} progress := ProgressBar{total: 10, length: 20, enabled: true}
progress.change(1, "", "Fetching") progress.change(1, "", "Fetching")
repoURL, err := getRepoURL(packageName) pkgidx, err := getPackage(packageName)
if err != nil { if err != nil {
fmt.Printf("Error getting repo URL: %s\n", err) fmt.Printf("Error getting repo URL: %s\n", err)
return return
} }
repoU, err := url.Parse(repoURL) repoU, err := url.Parse(pkgidx.Repo)
if err != nil { if err != nil {
fmt.Printf("Error parsing repo URL: %s\n", err) fmt.Printf("Error parsing repo URL: %s\n", err)
} }
if packageVersion == "" { if packageVersion == "" {
packageVersion, err = getLatestVersion(repoU.Path) packageVersion = pkgidx.Latest
if err != nil { progress.change(3, "", fmt.Sprintf("Using version %s", packageVersion))
fmt.Printf("Error extracting version: %s\n", err)
}
progress.change(3, "", fmt.Sprintf("Found version %s", packageVersion))
} }
progress.change(6, "", "Downloading") progress.change(6, "", "Downloading")
downloadPackage(repoU.Path, packageVersion) downloadPackage(repoU.Path, packageVersion)