diff --git a/README.md b/README.md index ece2270..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 + ```
@@ -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 ``` 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/install.go b/cli/install.go index 9a9fc91..e9360cf 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 } @@ -127,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 { @@ -141,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") } @@ -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", 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/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/cli/settings.go b/cli/settings.go index 0b8c597..6c3e628 100644 --- a/cli/settings.go +++ b/cli/settings.go @@ -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() { @@ -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 { @@ -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 { diff --git a/cli/version.go b/cli/version.go index 1d67a66..a993a99 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 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 } diff --git a/help.txt b/help.txt index 6cfdd87..eb0febc 100644 --- a/help.txt +++ b/help.txt @@ -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= | 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 diff --git a/main.go b/main.go index abd63d5..465baf5 100644 --- a/main.go +++ b/main.go @@ -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 @@ -75,7 +101,7 @@ func main() { case "ls": lsFlagSet.Parse(args[i+1:]) - + if *lsRemote { if err := zvm.ListRemoteAvailable(); err != nil { log.Fatal(err) @@ -85,7 +111,7 @@ func main() { log.Fatal(err) } } - + return case "uninstall", "rm": if len(args) > i+1 { @@ -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() @@ -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) } }