From d4b99d5b90010f51f5031b5ee8e90c23f1954bc6 Mon Sep 17 00:00:00 2001 From: OlshaMB Date: Fri, 26 Jan 2024 20:49:47 +0300 Subject: [PATCH 1/7] feat: adds a version map url setting - Renames the function fetchOfficialVersionMap to fetchVersionMap - Adds the setting for version map url and a cli option to change it - Uses this setting in fetchVersionMap - Adds a varibale to skip arguments in argument parsing loop - Adds the docs for the cli option --- cli/install.go | 3 +-- cli/ls.go | 2 +- cli/settings.go | 13 ++++++++++--- cli/version.go | 9 +++++++-- help.txt | 2 ++ main.go | 14 ++++++++++++++ 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/cli/install.go b/cli/install.go index 9a9fc91..e04339e 100644 --- a/cli/install.go +++ b/cli/install.go @@ -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 } diff --git a/cli/ls.go b/cli/ls.go index bcf8383..92932ee 100644 --- a/cli/ls.go +++ b/cli/ls.go @@ -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 } diff --git a/cli/settings.go b/cli/settings.go index 0b8c597..6a437a7 100644 --- a/cli/settings.go +++ b/cli/settings.go @@ -10,8 +10,9 @@ import ( ) type Settings struct { - basePath string - UseColor bool `json:"useColor"` + basePath string + UseColor bool `json:"useColor"` + VersionMapUrl *string `json:"versionMapUrl,omitempty"` } func (s *Settings) ToggleColor() { @@ -46,7 +47,13 @@ func (s *Settings) YesColor() { fmt.Printf("Terminal color output: %s\n", clr.Green("ON")) } - +func (s *Settings) SetVersionMapUrl(versionMapUrl string) { + s.VersionMapUrl = &versionMapUrl + if err := s.save(); err != nil { + log.Fatal(err) + } + fmt.Printf("Version map url: %s\n", clr.Blue(versionMapUrl)) +} func (s Settings) save() error { out_settings, err := json.MarshalIndent(&s, "", " ") if err != nil { diff --git a/cli/version.go b/cli/version.go index 1d67a66..dddc7b9 100644 --- a/cli/version.go +++ b/cli/version.go @@ -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 versionMapUrl == nil { + 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 } diff --git a/help.txt b/help.txt index 6cfdd87..3746ede 100644 --- a/help.txt +++ b/help.txt @@ -36,4 +36,6 @@ help, --help, -h --yescolor, --yescolour | Turns on color. +--versionmapurl | Changes the version map(version history) url + Looking for more help? https://github.com/tristanisham/zvm diff --git a/main.go b/main.go index abd63d5..6b75561 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,13 @@ func main() { lsFlagSet := flag.NewFlagSet("ls", flag.ExitOnError) lsRemote := lsFlagSet.Bool("all", false, "List all available versions of Zig to install") + plannedSkips := 0 + for i, arg := range args { + if plannedSkips > 0 { + plannedSkips -= 1 + continue + } switch arg { case "install", "i": installFlagSet.Parse(args[i+1:]) @@ -135,6 +141,14 @@ func main() { // Settings case "--nocolor", "--nocolour": zvm.Settings.NoColor() + case "--versionmapurl": + url := &args[i+1] + if url == nil { + fmt.Printf("ERROR: Next argument not provided for --versionmapurl. Please check out --help.\n") + os.Exit(1) + } + zvm.Settings.SetVersionMapUrl(*url) + plannedSkips = 1 case "--color", "--colour": zvm.Settings.ToggleColor() case "--yescolor", "--yescolour": From 620c0b54cec349c9d02a674c9b4c40dd689544a1 Mon Sep 17 00:00:00 2001 From: OlshaMB Date: Fri, 26 Jan 2024 21:00:24 +0300 Subject: [PATCH 2/7] feat: add option to README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ece2270..1c7280e 100644 --- a/README.md +++ b/README.md @@ -208,4 +208,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 ``` From f822a7a53e4cd415d4356aab0ca378910659a70d Mon Sep 17 00:00:00 2001 From: OlshaMB Date: Sun, 28 Jan 2024 00:17:42 +0300 Subject: [PATCH 3/7] fix: Fixes the bug where mach-latest wouldn't install correctly This commit fixes the bug where the cli would use an incorrect tarName(name of unpacked file) and this would create a broken symlink. This commit also rewrites the algorithm for getting a tarName --- cli/install.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cli/install.go b/cli/install.go index e04339e..1833cd9 100644 --- a/cli/install.go +++ b/cli/install.go @@ -126,13 +126,12 @@ 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 { @@ -140,8 +139,9 @@ func (z *ZVM) Install(version string) error { } } 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") } From 61e57509ae0eaf3ea2669f03f206d735804d1f06 Mon Sep 17 00:00:00 2001 From: OlshaMB Date: Sun, 28 Jan 2024 00:24:17 +0300 Subject: [PATCH 4/7] fmt: Formats the code with `go fmt .` --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 6b75561..a6a9a03 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,7 @@ 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) @@ -81,7 +81,7 @@ func main() { case "ls": lsFlagSet.Parse(args[i+1:]) - + if *lsRemote { if err := zvm.ListRemoteAvailable(); err != nil { log.Fatal(err) @@ -91,7 +91,7 @@ func main() { log.Fatal(err) } } - + return case "uninstall", "rm": if len(args) > i+1 { From 9a5e3d2716b4963590a916db59f77097c5405859 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Sun, 28 Jan 2024 16:00:11 -0500 Subject: [PATCH 5/7] updated command parsing --- README.md | 9 +-------- cli/error.go | 1 + cli/settings.go | 50 +++++++++++++++++++++++++++++++++++++++++++++---- cli/version.go | 6 +++--- help.txt | 4 ++-- main.go | 34 ++++++++++++++++----------------- 6 files changed, 70 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 1c7280e..60de1cc 100644 --- a/README.md +++ b/README.md @@ -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. @@ -194,10 +190,7 @@ Prints the version of ZVM you have installed. ```sh zvm help -# Or -zvm --help -# Or -zvm -h + ```
diff --git a/cli/error.go b/cli/error.go index f43a103..1505376 100644 --- a/cli/error.go +++ b/cli/error.go @@ -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") ) diff --git a/cli/settings.go b/cli/settings.go index 6a437a7..6c3e628 100644 --- a/cli/settings.go +++ b/cli/settings.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "net/url" "os" "github.com/tristanisham/clr" @@ -12,7 +13,7 @@ import ( type Settings struct { basePath string UseColor bool `json:"useColor"` - VersionMapUrl *string `json:"versionMapUrl,omitempty"` + VersionMapUrl string `json:"versionMapUrl,omitempty"` } func (s *Settings) ToggleColor() { @@ -30,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 { @@ -47,13 +52,50 @@ func (s *Settings) YesColor() { fmt.Printf("Terminal color output: %s\n", clr.Green("ON")) } -func (s *Settings) SetVersionMapUrl(versionMapUrl string) { - s.VersionMapUrl = &versionMapUrl + +func (s *Settings) SetColor(answer bool) { + s.UseColor = answer if err := s.save(); err != nil { log.Fatal(err) } - fmt.Printf("Version map url: %s\n", clr.Blue(versionMapUrl)) } + +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 { diff --git a/cli/version.go b/cli/version.go index dddc7b9..a993a99 100644 --- a/cli/version.go +++ b/cli/version.go @@ -13,11 +13,11 @@ func (z *ZVM) fetchVersionMap() (zigVersionMap, error) { defaultVersionMapUrl := "https://ziglang.org/download/index.json" versionMapUrl := z.Settings.VersionMapUrl - if versionMapUrl == nil { - versionMapUrl = &defaultVersionMapUrl + if len(versionMapUrl) == 0 { + versionMapUrl = defaultVersionMapUrl } - req, err := http.NewRequest("GET", *versionMapUrl, nil) + req, err := http.NewRequest("GET", versionMapUrl, nil) if err != nil { return nil, err } diff --git a/help.txt b/help.txt index 3746ede..adde785 100644 --- a/help.txt +++ b/help.txt @@ -23,10 +23,10 @@ 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 ----------------- diff --git a/main.go b/main.go index a6a9a03..7ef7fdd 100644 --- a/main.go +++ b/main.go @@ -42,14 +42,23 @@ func main() { lsFlagSet := flag.NewFlagSet("ls", flag.ExitOnError) lsRemote := lsFlagSet.Bool("all", false, "List all available versions of Zig to install") - plannedSkips := 0 + // Global config + sVersionMapUrl := flag.String("vmu", "https://ziglang.org/download/index.json", "Set ZVM's version map URL for custom Zig distribution servers") - for i, arg := range args { - if plannedSkips > 0 { - plannedSkips -= 1 - continue + flag.Parse() + + if sVersionMapUrl != nil { + if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { + log.Fatal(err) } + } + + args = flag.Args() + + for i, arg := range args { + switch arg { + case "install", "i": installFlagSet.Parse(args[i+1:]) // signal to install zls after zig @@ -130,10 +139,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() @@ -141,21 +150,12 @@ func main() { // Settings case "--nocolor", "--nocolour": zvm.Settings.NoColor() - case "--versionmapurl": - url := &args[i+1] - if url == nil { - fmt.Printf("ERROR: Next argument not provided for --versionmapurl. Please check out --help.\n") - os.Exit(1) - } - zvm.Settings.SetVersionMapUrl(*url) - plannedSkips = 1 case "--color", "--colour": zvm.Settings.ToggleColor() 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 check out help.\n", arg) } } From e1c87dd08243c998865931aefcd49a181d9fb88e Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Sun, 28 Jan 2024 16:01:07 -0500 Subject: [PATCH 6/7] updated help --- help.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help.txt b/help.txt index adde785..528646f 100644 --- a/help.txt +++ b/help.txt @@ -36,6 +36,6 @@ help --yescolor, --yescolour | Turns on color. ---versionmapurl | Changes the version map(version history) url +-vmu | Changes the version map url (Good if you host your own Zig distrobution server) Looking for more help? https://github.com/tristanisham/zvm From 37bc2273d7c361b4312e9a66de2a34d887817d7f Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Sun, 28 Jan 2024 16:13:31 -0500 Subject: [PATCH 7/7] updated so commands still work --- cli/install.go | 4 ++++ cli/meta/version.go | 2 +- help.txt | 6 +++--- main.go | 15 +++++++++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/cli/install.go b/cli/install.go index 1833cd9..e9360cf 100644 --- a/cli/install.go +++ b/cli/install.go @@ -307,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", diff --git a/cli/meta/version.go b/cli/meta/version.go index 4eb7342..d313be8 100644 --- a/cli/meta/version.go +++ b/cli/meta/version.go @@ -1,4 +1,4 @@ package meta -const VERSION = "v0.5.0" +const VERSION = "v0.5.1" diff --git a/help.txt b/help.txt index 528646f..eb0febc 100644 --- a/help.txt +++ b/help.txt @@ -31,10 +31,10 @@ help ------------- Flags ----------------- ---nocolor, --nocolour | Turns off color. ---color, --colour | Toggles color. ---yescolor, --yescolour | Turns on color. +--color= | 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) diff --git a/main.go b/main.go index 7ef7fdd..465baf5 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,7 @@ func main() { // 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 { @@ -53,6 +53,17 @@ func main() { } } + 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 { @@ -155,7 +166,7 @@ func main() { case "--yescolor", "--yescolour": zvm.Settings.YesColor() default: - log.Fatalf("invalid argument %q. Please check out help.\n", arg) + log.Fatalf("invalid argument %q. Please run `zvm help`.\n", arg) } }