Skip to content

Commit

Permalink
Merge pull request #41 from OlshaMB/olshamb/custom-index
Browse files Browse the repository at this point in the history
feat: adds a version map url setting
  • Loading branch information
tristanisham authored Jan 28, 2024
2 parents 9cc7ec9 + 37bc227 commit 3a7770e
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 36 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ Use `clean` to remove build artifacts (Good if you're on Windows).

```sh
zvm version
# Or
zvm --version
# Or
zvm -v
```

Prints the version of ZVM you have installed.
Expand All @@ -194,10 +190,7 @@ Prints the version of ZVM you have installed.

```sh
zvm help
# Or
zvm --help
# Or
zvm -h

```

<hr>
Expand All @@ -208,4 +201,5 @@ zvm -h
--nocolor, --nocolour # Turns off ANSI color.
--color, --colour # Toggles ANSI color.
--yescolor, --yescolour # Turns on ANSI color.
--versionmapurl # Changes the version map(version history) url
```
1 change: 1 addition & 0 deletions cli/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ var (
ErrUnsupportedVersion = errors.New("unsupported Zig version")
ErrMissingInstallPathEnv = errors.New("env 'ZVM_INSTALL' is not set")
ErrFailedUpgrade = errors.New("failed to self-upgrade zvm")
ErrInvalidVersionMap = errors.New("invalid version map format")
)
23 changes: 13 additions & 10 deletions cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import (
func (z *ZVM) Install(version string) error {

os.Mkdir(z.zvmBaseDir, 0755)

rawVersionStructure, err := z.fetchOfficialVersionMap()
rawVersionStructure, err := z.fetchVersionMap()
if err != nil {
return err
}
Expand Down Expand Up @@ -127,22 +126,22 @@ func (z *ZVM) Install(version string) error {
}
var tarName string

resultUrl, err := url.Parse(tarPath)
if err != nil {
log.Error(err)
tarName = version
}
if wasZigOnl {
resultUrl, err := url.Parse(tarPath)
if err != nil {
log.Error(err)
tarName = version
}

if rel := resultUrl.Query().Get("release"); len(rel) > 0 {
tarName = strings.Replace(rel, " ", "+", 1)
} else {
tarName = version
}

} else {
tarName = strings.TrimPrefix(tarPath, "https://ziglang.org/builds/")
tarName = strings.TrimPrefix(tarName, fmt.Sprintf("https://ziglang.org/download/%s/", version))
// Maybe think of a better algorithm
urlPath := strings.Split(resultUrl.Path, "/")
tarName = urlPath[len(urlPath)-1]
tarName = strings.TrimSuffix(tarName, ".tar.xz")
tarName = strings.TrimSuffix(tarName, ".zip")
}
Expand Down Expand Up @@ -308,6 +307,10 @@ func (z *ZVM) InstallZls(version string) error {
defer tempDir.Close()
defer os.RemoveAll(tempDir.Name())

// if resp.ContentLength == 0 {
// return fmt.Errorf("invalid ZLS content length (%d bytes)", resp.ContentLength)
// }

pbar := progressbar.DefaultBytes(
int64(resp.ContentLength),
"Downloading ZLS",
Expand Down
2 changes: 1 addition & 1 deletion cli/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (z *ZVM) ListVersions() error {
}

func (z ZVM) ListRemoteAvailable() error {
versions, err := z.fetchOfficialVersionMap()
versions, err := z.fetchVersionMap()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/meta/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package meta

const VERSION = "v0.5.0"
const VERSION = "v0.5.1"

53 changes: 51 additions & 2 deletions cli/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"

"github.com/tristanisham/clr"
)

type Settings struct {
basePath string
UseColor bool `json:"useColor"`
basePath string
UseColor bool `json:"useColor"`
VersionMapUrl string `json:"versionMapUrl,omitempty"`
}

func (s *Settings) ToggleColor() {
Expand All @@ -29,6 +31,10 @@ func (s *Settings) ToggleColor() {

}

func (s *Settings) ResetVersionMap() {
s.VersionMapUrl = "https://ziglang.org/download/index.json"
}

func (s *Settings) NoColor() {
s.UseColor = false
if err := s.save(); err != nil {
Expand All @@ -47,6 +53,49 @@ func (s *Settings) YesColor() {

}

func (s *Settings) SetColor(answer bool) {
s.UseColor = answer
if err := s.save(); err != nil {
log.Fatal(err)
}
}

func (s *Settings) SetVersionMapUrl(versionMapUrl string) error {

if err := isValidWebURL(versionMapUrl); err != nil {
return fmt.Errorf("%w: %w", ErrInvalidVersionMap, err)
}

s.VersionMapUrl = versionMapUrl
if err := s.save(); err != nil {
return err
}

return nil
}

// isValidWebURL checks if the given URL string is a valid web URL.
func isValidWebURL(urlString string) error {
parsedURL, err := url.Parse(urlString)
if err != nil {
return err // URL parsing error
}

// Check for valid HTTP/HTTPS scheme
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
return fmt.Errorf("invalid URL scheme: %s", parsedURL.Scheme)
}

// Check for non-empty host (domain)
if parsedURL.Host == "" {
return fmt.Errorf("URL host (domain) is empty")
}

// Optionally, you can add more checks (like path, query params, etc.) here if needed

return nil // URL is valid
}

func (s Settings) save() error {
out_settings, err := json.MarshalIndent(&s, "", " ")
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import (
"zvm/cli/meta"
)

func (z *ZVM) fetchVersionMap() (zigVersionMap, error) {

func (z *ZVM) fetchOfficialVersionMap() (zigVersionMap, error) {
defaultVersionMapUrl := "https://ziglang.org/download/index.json"
versionMapUrl := z.Settings.VersionMapUrl
if len(versionMapUrl) == 0 {
versionMapUrl = defaultVersionMapUrl
}

req, err := http.NewRequest("GET", "https://ziglang.org/download/index.json", nil)
req, err := http.NewRequest("GET", versionMapUrl, nil)
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ clean
upgrade
Use `upgrade` to update your ZVM install

version, --version, -v
version
Prints the version of ZVM you have installed.

help, --help, -h
help
Prints this message.

------------- Flags -----------------

--nocolor, --nocolour | Turns off color.
--color, --colour | Toggles color.

--yescolor, --yescolour | Turns on color.
--color=<bool> | Turn color printing on or off for ZVM's output



-vmu | Changes the version map url (Good if you host your own Zig distrobution server)

Looking for more help? https://github.com/tristanisham/zvm
39 changes: 32 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,40 @@ func main() {

// Install flags
installFlagSet := flag.NewFlagSet("install", flag.ExitOnError)
installDeps := installFlagSet.String("D", "", "Specify additional dependencies to install with Zig")
installDeps := installFlagSet.String("D", "", "Specify additional dependencies to install with Zig")

// LS flags
lsFlagSet := flag.NewFlagSet("ls", flag.ExitOnError)
lsRemote := lsFlagSet.Bool("all", false, "List all available versions of Zig to install")

// Global config
sVersionMapUrl := flag.String("vmu", "https://ziglang.org/download/index.json", "Set ZVM's version map URL for custom Zig distribution servers")
sColorToggle := flag.Bool("color", true, "Turn on or off ZVM's color output")
flag.Parse()

if sVersionMapUrl != nil {
if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil {
log.Fatal(err)
}
}

if sColorToggle != nil {
if *sColorToggle != zvm.Settings.UseColor {
if *sColorToggle {
zvm.Settings.YesColor()
} else {
zvm.Settings.NoColor()
}
}

}

args = flag.Args()

for i, arg := range args {

switch arg {

case "install", "i":
installFlagSet.Parse(args[i+1:])
// signal to install zls after zig
Expand Down Expand Up @@ -75,7 +101,7 @@ func main() {

case "ls":
lsFlagSet.Parse(args[i+1:])

if *lsRemote {
if err := zvm.ListRemoteAvailable(); err != nil {
log.Fatal(err)
Expand All @@ -85,7 +111,7 @@ func main() {
log.Fatal(err)
}
}

return
case "uninstall", "rm":
if len(args) > i+1 {
Expand Down Expand Up @@ -124,10 +150,10 @@ func main() {
log.Fatal(err)
}

case "version", "--version", "-v":
case "version":
fmt.Println(meta.VERSION)
return
case "help", "--help", "-h":
case "help":
//zvm.Settings.UseColor
helpMsg()

Expand All @@ -140,8 +166,7 @@ func main() {
case "--yescolor", "--yescolour":
zvm.Settings.YesColor()
default:
fmt.Printf("ERROR: Invalid argument %s. Please check out --help.\n", arg)
os.Exit(1)
log.Fatalf("invalid argument %q. Please run `zvm help`.\n", arg)
}

}
Expand Down

0 comments on commit 3a7770e

Please sign in to comment.