fixed github api limit
This commit is contained in:
+5
-4
@@ -23,6 +23,7 @@ type Package struct {
|
||||
type PackageIndex struct {
|
||||
Name string `json:"name"`
|
||||
Repo string `json:"repo"`
|
||||
Latest string `json:"latest"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
@@ -48,17 +49,17 @@ func sendRequest(method, url string, jsonBody []byte) Request {
|
||||
return Request{Body: resBody, StatusCode: res.StatusCode}
|
||||
}
|
||||
|
||||
func getRepoURL(packageName string) (string, error) {
|
||||
func getPackage(packageName string) (PackageIndex, error) {
|
||||
var pkgidx PackageIndex
|
||||
jsonBody := []byte(fmt.Sprintf(`{"name": "%s"}`, packageName))
|
||||
req := sendRequest(http.MethodGet, "http://localhost:3000", jsonBody)
|
||||
err := json.Unmarshal(req.Body, &pkgidx)
|
||||
if err != nil {
|
||||
fmt.Println("Json Error: ", err)
|
||||
return "", err
|
||||
return pkgidx, err
|
||||
}
|
||||
if req.StatusCode != 200 {
|
||||
return "", fmt.Errorf(pkgidx.Message)
|
||||
return pkgidx, fmt.Errorf(pkgidx.Message)
|
||||
}
|
||||
return pkgidx.Repo, nil
|
||||
return pkgidx, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -119,17 +118,6 @@ func Unzip(src, dest string) error {
|
||||
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 {
|
||||
resp, err := http.Get(fmt.Sprintf("https://github.com/%s/archive/refs/tags/%s.zip", strings.Trim(repoU, "/"), tag))
|
||||
if err != nil {
|
||||
|
||||
+3
-2
@@ -5,7 +5,7 @@ import "fmt"
|
||||
func showPackageInfo(packageName string) {
|
||||
progress := ProgressBar{total: 4, length: 20}
|
||||
progress.change(3, "", "Fetching")
|
||||
repoURL, err := getRepoURL(packageName)
|
||||
pkgidx, err := getPackage(packageName)
|
||||
if err != nil {
|
||||
fmt.Printf("\nError getting repo URL: %v\n", err)
|
||||
return
|
||||
@@ -25,7 +25,8 @@ func showPackageInfo(packageName string) {
|
||||
// }
|
||||
|
||||
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 {
|
||||
// fmt.Printf("Version: %s\n", version)
|
||||
// fmt.Printf(" Author: %s\n", info.Author)
|
||||
|
||||
+4
-7
@@ -22,21 +22,18 @@ type PackageVersion struct {
|
||||
func installPackage(packageName, packageVersion string) {
|
||||
progress := ProgressBar{total: 10, length: 20, enabled: true}
|
||||
progress.change(1, "", "Fetching")
|
||||
repoURL, err := getRepoURL(packageName)
|
||||
pkgidx, err := getPackage(packageName)
|
||||
if err != nil {
|
||||
fmt.Printf("Error getting repo URL: %s\n", err)
|
||||
return
|
||||
}
|
||||
repoU, err := url.Parse(repoURL)
|
||||
repoU, err := url.Parse(pkgidx.Repo)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing repo URL: %s\n", err)
|
||||
}
|
||||
if packageVersion == "" {
|
||||
packageVersion, err = getLatestVersion(repoU.Path)
|
||||
if err != nil {
|
||||
fmt.Printf("Error extracting version: %s\n", err)
|
||||
}
|
||||
progress.change(3, "", fmt.Sprintf("Found version %s", packageVersion))
|
||||
packageVersion = pkgidx.Latest
|
||||
progress.change(3, "", fmt.Sprintf("Using version %s", packageVersion))
|
||||
}
|
||||
progress.change(6, "", "Downloading")
|
||||
downloadPackage(repoU.Path, packageVersion)
|
||||
|
||||
Reference in New Issue
Block a user