pms support
This commit is contained in:
@@ -1,2 +1,25 @@
|
||||
# wyst-package-manager
|
||||
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>
|
||||
```
|
||||
|
||||
|
||||
@@ -133,23 +133,23 @@ func getLatestVersion(repoU string) string {
|
||||
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))
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
out, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading body:", err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile("temp.zip", out, 0644)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing file:", err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+19
-23
@@ -1,36 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
import "fmt"
|
||||
|
||||
func showPackageInfo(packageName string) {
|
||||
url := fmt.Sprintf("https://raw.githubusercontent.com/wyst-lang/index/master/%s/index.json", packageName)
|
||||
resp, err := http.Get(url)
|
||||
repoURL, err := getRepoURL(packageName)
|
||||
if err != nil {
|
||||
fmt.Printf("Error fetching package info: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
fmt.Printf("Error: %s\n", resp.Status)
|
||||
fmt.Printf("Error getting repo URL: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
var pkgIndex PackageIndex
|
||||
if err := json.NewDecoder(resp.Body).Decode(&pkgIndex); err != nil {
|
||||
fmt.Printf("Error decoding package info: %v\n", err)
|
||||
return
|
||||
}
|
||||
// indexURL, err := findIndexURL(repoURL)
|
||||
// if err != nil {
|
||||
// fmt.Printf("Error finding index URL: %v\n", err)
|
||||
// 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("Repo: %s\n", repoURL)
|
||||
// for version, info := range pkgIndex.Versions {
|
||||
// fmt.Printf("Version: %s\n", version)
|
||||
// fmt.Printf(" Author: %s\n", info.Author)
|
||||
// fmt.Printf(" Description: %s\n", info.Description)
|
||||
// fmt.Println()
|
||||
// fmt.Printf("Version: %s\n", version)
|
||||
// fmt.Printf(" Author: %s\n", info.Author)
|
||||
// fmt.Printf(" Description: %s\n", info.Description)
|
||||
fmt.Println()
|
||||
// }
|
||||
}
|
||||
|
||||
+22
-19
@@ -1,46 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"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"`
|
||||
Description string `json:"description"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
type PackageIndex struct {
|
||||
Name string `json:"name"`
|
||||
Repo string `json:"repo"`
|
||||
}
|
||||
|
||||
func installPackage(packageName, packageVersion string) {
|
||||
var pkgidx PackageIndex
|
||||
jsonBody := []byte(fmt.Sprintf(`{"name": "%s"}`, packageName))
|
||||
resBody := sendRequest(http.MethodGet, "http://localhost:3000", jsonBody)
|
||||
err := json.Unmarshal(resBody.Body, &pkgidx)
|
||||
fmt.Printf("Fetching %s\n", packageName)
|
||||
repoURL, err := getRepoURL(packageName)
|
||||
if err != nil {
|
||||
fmt.Println("Json Error: ", err)
|
||||
fmt.Printf("Error getting repo URL: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("repo: %s\n", pkgidx.Repo)
|
||||
|
||||
repoU, err := url.Parse(pkgidx.Repo)
|
||||
repoU, err := url.Parse(repoURL)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
fmt.Printf("Error parsing repo URL: %s\n", err)
|
||||
}
|
||||
|
||||
if packageVersion == "" {
|
||||
packageVersion = getLatestVersion(repoU.Path)
|
||||
fmt.Printf("Found version %s\n", packageVersion)
|
||||
}
|
||||
fmt.Printf("Downloading %s %s\n", packageName, 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")
|
||||
entries, err := os.ReadDir("./wyst_tmp")
|
||||
if err != nil {
|
||||
@@ -61,4 +63,5 @@ func installPackage(packageName, packageVersion string) {
|
||||
if err := os.Remove("temp.zip"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s %s Installed\n", packageName, packageVersion)
|
||||
}
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
Reference in New Issue
Block a user