pms support

This commit is contained in:
2024-05-21 15:25:40 +02:00
5 changed files with 69 additions and 83 deletions
+23
View File
@@ -1,2 +1,25 @@
# wyst-package-manager # wyst-package-manager
A package manager for the programming language Wyst A package manager for the programming language Wyst
## Usage:
### Installing packages (version is optional)
```console
wpm install <package>[:version]
```
### Version
```console
wpm version
```
### Help
```console
wpm help
```
### Obtaining information from a package
```console
wpm info <package>
```
+5 -5
View File
@@ -133,23 +133,23 @@ func getLatestVersion(repoU string) string {
return latest return latest
} }
func downloadPackage(repoU, tag string) { 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 {
fmt.Println("Error:", err) return err
return
} }
defer resp.Body.Close() defer resp.Body.Close()
out, err := ioutil.ReadAll(resp.Body) out, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
fmt.Println("Error reading body:", err) fmt.Println("Error reading body:", err)
return return err
} }
err = ioutil.WriteFile("temp.zip", out, 0644) err = ioutil.WriteFile("temp.zip", out, 0644)
if err != nil { if err != nil {
fmt.Println("Error writing file:", err) fmt.Println("Error writing file:", err)
return return err
} }
return nil
} }
+19 -23
View File
@@ -1,36 +1,32 @@
package main package main
import ( import "fmt"
"encoding/json"
"fmt"
"net/http"
)
func showPackageInfo(packageName string) { func showPackageInfo(packageName string) {
url := fmt.Sprintf("https://raw.githubusercontent.com/wyst-lang/index/master/%s/index.json", packageName) repoURL, err := getRepoURL(packageName)
resp, err := http.Get(url)
if err != nil { if err != nil {
fmt.Printf("Error fetching package info: %v\n", err) fmt.Printf("Error getting repo URL: %v\n", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Error: %s\n", resp.Status)
return return
} }
var pkgIndex PackageIndex // indexURL, err := findIndexURL(repoURL)
if err := json.NewDecoder(resp.Body).Decode(&pkgIndex); err != nil { // if err != nil {
fmt.Printf("Error decoding package info: %v\n", err) // fmt.Printf("Error finding index URL: %v\n", err)
return // return
} // }
// pkgIndex, err := fetchPackageIndex(indexURL)
// if err != nil {
// fmt.Printf("Error fetching package index: %v\n", err)
// return
// }
fmt.Printf("Package: %s\n", packageName) fmt.Printf("Package: %s\n", packageName)
fmt.Printf("Repo: %s\n", repoURL)
// 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)
// fmt.Printf(" Description: %s\n", info.Description) // fmt.Printf(" Description: %s\n", info.Description)
// fmt.Println() fmt.Println()
// } // }
} }
+22 -19
View File
@@ -1,46 +1,48 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"os" "os"
"strings" "strings"
) )
type Package struct { type RepoResponse struct {
Message string `json:"message"`
Name string `json:"name"`
Repo string `json:"repo"`
}
type PackageVersion struct {
Author string `json:"author"` Author string `json:"author"`
Description string `json:"description"` Description string `json:"description"`
URL string `json:"url"` URL string `json:"url"`
} }
type PackageIndex struct {
Name string `json:"name"`
Repo string `json:"repo"`
}
func installPackage(packageName, packageVersion string) { func installPackage(packageName, packageVersion string) {
var pkgidx PackageIndex fmt.Printf("Fetching %s\n", packageName)
jsonBody := []byte(fmt.Sprintf(`{"name": "%s"}`, packageName)) repoURL, err := getRepoURL(packageName)
resBody := sendRequest(http.MethodGet, "http://localhost:3000", jsonBody)
err := json.Unmarshal(resBody.Body, &pkgidx)
if err != nil { if err != nil {
fmt.Println("Json Error: ", err) fmt.Printf("Error getting repo URL: %s\n", err)
return return
} }
repoU, err := url.Parse(repoURL)
fmt.Printf("repo: %s\n", pkgidx.Repo)
repoU, err := url.Parse(pkgidx.Repo)
if err != nil { if err != nil {
panic(err) fmt.Printf("Error parsing repo URL: %s\n", err)
} }
if packageVersion == "" { if packageVersion == "" {
packageVersion = getLatestVersion(repoU.Path) packageVersion = getLatestVersion(repoU.Path)
fmt.Printf("Found version %s\n", packageVersion)
} }
fmt.Printf("Downloading %s %s\n", packageName, packageVersion)
downloadPackage(repoU.Path, packageVersion) downloadPackage(repoU.Path, packageVersion)
if err := os.Mkdir("wyst_tmp", os.ModePerm); err != nil {
ERR := fmt.Sprintf("%s", err)
if !strings.Contains(strings.ToLower(ERR), "file exists") {
panic(ERR)
}
}
fmt.Printf("Extracting %s %s\n", packageName, packageVersion)
Unzip("temp.zip", "wyst_tmp") Unzip("temp.zip", "wyst_tmp")
entries, err := os.ReadDir("./wyst_tmp") entries, err := os.ReadDir("./wyst_tmp")
if err != nil { if err != nil {
@@ -61,4 +63,5 @@ func installPackage(packageName, packageVersion string) {
if err := os.Remove("temp.zip"); err != nil { if err := os.Remove("temp.zip"); err != nil {
panic(err) panic(err)
} }
fmt.Printf("%s %s Installed\n", packageName, packageVersion)
} }
-36
View File
@@ -1,36 +0,0 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
)
type Request struct {
Body []byte
StatusCode int
}
func sendRequest(method, url string, jsonBody []byte) Request {
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(method, url, bodyReader)
if err != nil {
fmt.Printf("Error fetching package info: %v\n", err)
return Request{Body: []byte(""), StatusCode: 402}
}
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("client: error making http request: %s\n", err)
os.Exit(1)
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("client: could not read response body: %s\n", err)
os.Exit(1)
}
return Request{Body: resBody, StatusCode: res.StatusCode}
}