From 51bc41832f7586713ced59902f00549234a8bd60 Mon Sep 17 00:00:00 2001 From: iamtimmy <58427647+iamtimmy@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:56:52 +0100 Subject: [PATCH 01/34] fixed basic zls install from github releases --- cli/install.go | 64 ++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/cli/install.go b/cli/install.go index 9a9fc91..45d8b91 100644 --- a/cli/install.go +++ b/cli/install.go @@ -205,8 +205,9 @@ type githubTaggedReleaseResponse struct { } type gitHubAsset struct { - Url string // download url for asset - Name string // contains platform information about binary + Url string // url for asset json object + Name string // contains platform information about binary + Browser_download_url string // download url } func (z *ZVM) InstallZls(version string) error { @@ -253,9 +254,9 @@ func (z *ZVM) InstallZls(version string) error { // some github releases use tar.gz, some tar.xz expectedArchOs := fmt.Sprintf("%v-%v", arch, osType) - zipName := "" - var taggedReleaseResponse githubTaggedReleaseResponse + // getting list of assets + var taggedReleaseResponse githubTaggedReleaseResponse if err := json.Unmarshal(releaseBuffer.Bytes(), &taggedReleaseResponse); err != nil { return err } @@ -264,8 +265,7 @@ func (z *ZVM) InstallZls(version string) error { downloadUrl := "" for _, asset := range taggedReleaseResponse.Assets { if strings.Contains(asset.Name, expectedArchOs) { - downloadUrl = asset.Url - zipName = asset.Name + downloadUrl = asset.Browser_download_url break } } @@ -275,32 +275,27 @@ func (z *ZVM) InstallZls(version string) error { return fmt.Errorf("could not find zls-%v", expectedArchOs) } - if version == "master" { - downloadUrl = fmt.Sprintf("https://zig.pm/zls/downloads/%v/bin/zls", expectedArchOs) - } - - client := &http.Client{} - - // download tarball to temp - req, err := http.NewRequest("GET", downloadUrl, nil) + zlsDownloadRequest, err := http.NewRequest("GET", downloadUrl, nil) if err != nil { return err } - req.Header.Set("Accept", "application/octet-stream") - resp, err = client.Do(req) + + zlsDownloadRequest.Header.Set("User-Agent", "zvm "+meta.VERSION) + + archiveResponse, err := http.DefaultClient.Do(zlsDownloadRequest) if err != nil { return err } - defer resp.Body.Close() + defer archiveResponse.Body.Close() - // creating file to place contents - tempName := filename - if version != "master" { - _, pathEnding, _ := strings.Cut(zipName, ".") - tempName = "*." + pathEnding + var pathEnding string + if runtime.GOOS == "windows" { + pathEnding = "*.zip" + } else { + pathEnding = "*.tar.xz" } - tempDir, err := os.CreateTemp(z.zvmBaseDir, tempName) + tempDir, err := os.CreateTemp(z.zvmBaseDir, pathEnding) if err != nil { return err } @@ -309,28 +304,25 @@ func (z *ZVM) InstallZls(version string) error { defer os.RemoveAll(tempDir.Name()) pbar := progressbar.DefaultBytes( - int64(resp.ContentLength), + int64(archiveResponse.ContentLength), "Downloading ZLS", ) - if _, err := io.Copy(io.MultiWriter(pbar, tempDir), resp.Body); err != nil { + if _, err := io.Copy(io.MultiWriter(pbar, tempDir), archiveResponse.Body); err != nil { return err } + // untar to destination fmt.Println("Extracting ZLS...") versionPath := filepath.Join(z.zvmBaseDir, version) - if version == "master" { - if err := os.Rename(tempDir.Name(), filepath.Join(versionPath, filename)); err != nil { - return err - } - } else { - if err := ExtractBundle(tempDir.Name(), filepath.Join(z.zvmBaseDir, version)); err != nil { - log.Fatal(err) - } - if err := os.Rename(filepath.Join(versionPath, "bin", filename), filepath.Join(versionPath, filename)); err != nil { - return err - } + + if err := ExtractBundle(tempDir.Name(), filepath.Join(z.zvmBaseDir, version)); err != nil { + log.Fatal(err) } + if err := os.Rename(filepath.Join(versionPath, "bin", filename), filepath.Join(versionPath, filename)); err != nil { + return err + } + if err := os.Chmod(filepath.Join(versionPath, filename), 0755); err != nil { return err } From 11f5d23534ed4b56f023bd6434e3617550e7c223 Mon Sep 17 00:00:00 2001 From: iamtimmy <58427647+iamtimmy@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:00:37 +0100 Subject: [PATCH 02/34] also fixed zls master installs for windows --- cli/install.go | 166 +++++++++++++++++++++++++++++-------------------- 1 file changed, 100 insertions(+), 66 deletions(-) diff --git a/cli/install.go b/cli/install.go index 45d8b91..1f453fd 100644 --- a/cli/install.go +++ b/cli/install.go @@ -210,12 +210,66 @@ type gitHubAsset struct { Browser_download_url string // download url } +func GetZlsDownloadUrl(version string, archDouble string) (string, error) { + if version == "master" { + var exeName = "zls" + if strings.Contains(archDouble, "windows") { + exeName = "zls.exe" + } + return fmt.Sprintf("https://zig.pm/zls/downloads/%v/bin/%v", archDouble, exeName), nil + + } else { + var releaseUrl string + if version == "master" { + releaseUrl = "https://api.github.com/repos/zigtools/zls/releases/latest" + } else { + // build url for tagged version + releaseUrl = fmt.Sprintf("https://api.github.com/repos/zigtools/zls/releases/tags/%v", version) + } + + // get release information + resp, err := http.Get(releaseUrl) + if err != nil { + return "", err + } + defer resp.Body.Close() + + var releaseBuffer bytes.Buffer + _, err = releaseBuffer.ReadFrom(resp.Body) + if err != nil { + return "", err + } + + // getting list of assets + var taggedReleaseResponse githubTaggedReleaseResponse + if err := json.Unmarshal(releaseBuffer.Bytes(), &taggedReleaseResponse); err != nil { + return "", err + } + + // getting platform information + downloadUrl := "" + for _, asset := range taggedReleaseResponse.Assets { + if strings.Contains(asset.Name, archDouble) { + downloadUrl = asset.Browser_download_url + break + } + } + + if downloadUrl == "" { + return "", errors.New("Could not find github release download url") + } + + return downloadUrl, nil + } +} + func (z *ZVM) InstallZls(version string) error { if version != "master" && strings.Count(version, ".") != 2 { return fmt.Errorf("%w: versions are SEMVER (MAJOR.MINOR.MINUSCULE)", ErrUnsupportedVersion) } fmt.Println("Finding ZLS executable...") + // make sure dir exists installDir := filepath.Join(z.zvmBaseDir, version) err := os.MkdirAll(installDir, 0755) @@ -224,6 +278,7 @@ func (z *ZVM) InstallZls(version string) error { } arch, osType := zigStyleSysInfo() + expectedArchOs := fmt.Sprintf("%v-%v", arch, osType) filename := "zls" if osType == "windows" { @@ -231,96 +286,75 @@ func (z *ZVM) InstallZls(version string) error { } // master does not need unzipping, zpm just serves full binary - var releaseUrl string + var shouldUnzip = true if version == "master" { - releaseUrl = "https://api.github.com/repos/zigtools/zls/releases/latest" - } else { - // build url for tagged version - releaseUrl = fmt.Sprintf("https://api.github.com/repos/zigtools/zls/releases/tags/%v", version) + shouldUnzip = false } - // get release information - resp, err := http.Get(releaseUrl) + downloadUrl, err := GetZlsDownloadUrl(version, expectedArchOs) if err != nil { + fmt.Println("what?") return err } - defer resp.Body.Close() - var releaseBuffer bytes.Buffer - _, err = releaseBuffer.ReadFrom(resp.Body) + request, err := http.NewRequest("GET", downloadUrl, nil) if err != nil { return err } - // some github releases use tar.gz, some tar.xz - expectedArchOs := fmt.Sprintf("%v-%v", arch, osType) - - // getting list of assets - var taggedReleaseResponse githubTaggedReleaseResponse - if err := json.Unmarshal(releaseBuffer.Bytes(), &taggedReleaseResponse); err != nil { - return err - } - - // getting platform information - downloadUrl := "" - for _, asset := range taggedReleaseResponse.Assets { - if strings.Contains(asset.Name, expectedArchOs) { - downloadUrl = asset.Browser_download_url - break - } - } - - // couldn't find the file - if downloadUrl == "" { - return fmt.Errorf("could not find zls-%v", expectedArchOs) - } + request.Header.Set("User-Agent", "zvm "+meta.VERSION) - zlsDownloadRequest, err := http.NewRequest("GET", downloadUrl, nil) + response, err := http.DefaultClient.Do(request) if err != nil { return err } + defer response.Body.Close() - zlsDownloadRequest.Header.Set("User-Agent", "zvm "+meta.VERSION) + pbar := progressbar.DefaultBytes( + int64(response.ContentLength), + "Downloading ZLS", + ) - archiveResponse, err := http.DefaultClient.Do(zlsDownloadRequest) - if err != nil { - return err - } - defer archiveResponse.Body.Close() + versionPath := filepath.Join(z.zvmBaseDir, version) + binaryLocation := filepath.Join(versionPath, filename) - var pathEnding string - if runtime.GOOS == "windows" { - pathEnding = "*.zip" - } else { - pathEnding = "*.tar.xz" - } + if shouldUnzip == false { + file, err := os.Create(binaryLocation) - tempDir, err := os.CreateTemp(z.zvmBaseDir, pathEnding) - if err != nil { - return err - } + if err != nil { + return err + } - defer tempDir.Close() - defer os.RemoveAll(tempDir.Name()) + if _, err := io.Copy(io.MultiWriter(pbar, file), response.Body); err != nil { + return err + } + } else { + var pathEnding string + if runtime.GOOS == "windows" { + pathEnding = "*.zip" + } else { + pathEnding = "*.tar.xz" + } - pbar := progressbar.DefaultBytes( - int64(archiveResponse.ContentLength), - "Downloading ZLS", - ) + tempDir, err := os.CreateTemp(z.zvmBaseDir, pathEnding) + if err != nil { + return err + } - if _, err := io.Copy(io.MultiWriter(pbar, tempDir), archiveResponse.Body); err != nil { - return err - } + defer tempDir.Close() + defer os.RemoveAll(tempDir.Name()) - // untar to destination - fmt.Println("Extracting ZLS...") - versionPath := filepath.Join(z.zvmBaseDir, version) + if _, err := io.Copy(io.MultiWriter(pbar, tempDir), response.Body); err != nil { + return err + } - if err := ExtractBundle(tempDir.Name(), filepath.Join(z.zvmBaseDir, version)); err != nil { - log.Fatal(err) - } - if err := os.Rename(filepath.Join(versionPath, "bin", filename), filepath.Join(versionPath, filename)); err != nil { - return err + fmt.Println("Extracting ZLS...") + if err := ExtractBundle(tempDir.Name(), filepath.Join(z.zvmBaseDir, version)); err != nil { + log.Fatal(err) + } + if err := os.Rename(filepath.Join(versionPath, "bin", filename), filepath.Join(versionPath, filename)); err != nil { + return err + } } if err := os.Chmod(filepath.Join(versionPath, filename), 0755); err != nil { From 799b98e953ffaf619c6c8cc5f5e20a352d84b16f Mon Sep 17 00:00:00 2001 From: iamtimmy <58427647+iamtimmy@users.noreply.github.com> Date: Thu, 25 Jan 2024 20:16:27 +0100 Subject: [PATCH 03/34] properly fixed zls master installs --- cli/install.go | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/cli/install.go b/cli/install.go index 1f453fd..2823292 100644 --- a/cli/install.go +++ b/cli/install.go @@ -210,25 +210,48 @@ type gitHubAsset struct { Browser_download_url string // download url } +type zlsCIDownloadIndexResponse struct { + Latest string // most recent ZLS version + LatestTagged string // most recent tagged ZLS version + Versions map[string]zlsCIZLSVersion +} + +type zlsCIZLSVersion struct { + ZLSVersion string + Targets []string +} + func GetZlsDownloadUrl(version string, archDouble string) (string, error) { if version == "master" { + resp, err := http.Get("https://zigtools-releases.nyc3.digitaloceanspaces.com/zls/index.json") + if err != nil { + return "", err + } + defer resp.Body.Close() + + var releaseBuffer bytes.Buffer + _, err = releaseBuffer.ReadFrom(resp.Body) + if err != nil { + return "", err + } + + var ciIndex zlsCIDownloadIndexResponse + if err := json.Unmarshal(releaseBuffer.Bytes(), &ciIndex); err != nil { + return "", err + } + var exeName = "zls" if strings.Contains(archDouble, "windows") { exeName = "zls.exe" } - return fmt.Sprintf("https://zig.pm/zls/downloads/%v/bin/%v", archDouble, exeName), nil + format_url := "https://zigtools-releases.nyc3.digitaloceanspaces.com/zls/%v/%v/%v" + return fmt.Sprintf(format_url, ciIndex.Latest, archDouble, exeName), nil } else { - var releaseUrl string - if version == "master" { - releaseUrl = "https://api.github.com/repos/zigtools/zls/releases/latest" - } else { - // build url for tagged version - releaseUrl = fmt.Sprintf("https://api.github.com/repos/zigtools/zls/releases/tags/%v", version) - } + url := fmt.Sprintf("https://api.github.com/repos/zigtools/zls/releases/tags/%v", version) // get release information - resp, err := http.Get(releaseUrl) + resp, err := http.Get(url) if err != nil { return "", err } From 4a09f44479ceaf8e61d7789296106e8e0070153c Mon Sep 17 00:00:00 2001 From: iamtimmy <58427647+iamtimmy@users.noreply.github.com> Date: Sat, 27 Jan 2024 03:14:22 +0100 Subject: [PATCH 04/34] in response to requested changes --- cli/install.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/cli/install.go b/cli/install.go index 2823292..2d27c63 100644 --- a/cli/install.go +++ b/cli/install.go @@ -205,9 +205,9 @@ type githubTaggedReleaseResponse struct { } type gitHubAsset struct { - Url string // url for asset json object - Name string // contains platform information about binary - Browser_download_url string // download url + Url string // url for asset json object + Name string // contains platform information about binary + BrowserDownloadUrl string // download url } type zlsCIDownloadIndexResponse struct { @@ -221,7 +221,7 @@ type zlsCIZLSVersion struct { Targets []string } -func GetZlsDownloadUrl(version string, archDouble string) (string, error) { +func getZLSDownloadUrl(version string, archDouble string) (string, error) { if version == "master" { resp, err := http.Get("https://zigtools-releases.nyc3.digitaloceanspaces.com/zls/index.json") if err != nil { @@ -240,7 +240,7 @@ func GetZlsDownloadUrl(version string, archDouble string) (string, error) { return "", err } - var exeName = "zls" + exeName := "zls" if strings.Contains(archDouble, "windows") { exeName = "zls.exe" } @@ -270,16 +270,16 @@ func GetZlsDownloadUrl(version string, archDouble string) (string, error) { } // getting platform information - downloadUrl := "" + var downloadUrl string for _, asset := range taggedReleaseResponse.Assets { if strings.Contains(asset.Name, archDouble) { - downloadUrl = asset.Browser_download_url + downloadUrl = asset.BrowserDownloadUrl break } } if downloadUrl == "" { - return "", errors.New("Could not find github release download url") + return "", errors.New("invalid release URl") } return downloadUrl, nil @@ -309,14 +309,13 @@ func (z *ZVM) InstallZls(version string) error { } // master does not need unzipping, zpm just serves full binary - var shouldUnzip = true + shouldUnzip := true if version == "master" { shouldUnzip = false } - downloadUrl, err := GetZlsDownloadUrl(version, expectedArchOs) + downloadUrl, err := getZLSDownloadUrl(version, expectedArchOs) if err != nil { - fmt.Println("what?") return err } From 1548e12113b591c44b1bc7cc865107b0ee6ff044 Mon Sep 17 00:00:00 2001 From: OlshaMB Date: Fri, 26 Jan 2024 20:49:47 +0300 Subject: [PATCH 05/34] 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 2d27c63..a56ccaf 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 38e70a01238d14e8e8a388ff5be29b461f3f7461 Mon Sep 17 00:00:00 2001 From: OlshaMB Date: Fri, 26 Jan 2024 21:00:24 +0300 Subject: [PATCH 06/34] 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 d25083f66df199539c02e79c7a39bb643ea46d35 Mon Sep 17 00:00:00 2001 From: OlshaMB Date: Sun, 28 Jan 2024 00:17:42 +0300 Subject: [PATCH 07/34] 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 a56ccaf..ba70771 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 08abe64a65e9d8ab53069d1933d2bd1bf37006ee Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Fri, 26 Jan 2024 23:15:32 -0500 Subject: [PATCH 08/34] contributing --- CONTRIBUTING.MD | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 CONTRIBUTING.MD diff --git a/CONTRIBUTING.MD b/CONTRIBUTING.MD new file mode 100644 index 0000000..f529d01 --- /dev/null +++ b/CONTRIBUTING.MD @@ -0,0 +1,9 @@ +# Contributing + +Thank you for contributing to ZVM. I'm just one guy, so all contributions are welcome and appreciated. However, in order to save us all some time, I've decided to write up some basic rules for contributing that I'd like all future P.R.'s to follow. + +1. Please run `go fmt .` before submitting your P.R. Go is a very opinionated language, and how you define function can affect visibilty across the whole program. Unless your function needs to be used by a different package, default to private (lowercase names). You should also use camelCase for all your variables, functions, and fields. Submissions using snake case, kebab case, or any other case will be refused until adjusted to camel case. + +2. I have a lot of obligations besides this repository so P.R.s may languish for a few days or weeks while I deal with my external obligations. It is important to me that you know that this is a very actively developed repository and all P.R.s will be reviewed within a month of submission. Probably sooner. If you would like to talk to me about your P.R. feel free to [email](mailto:tristan.isham@hey.com) me or reach out on Discor or Twitter. I check all regularly. + +I hope you feel comfortable contributing to ZVM and know that I greatly appreaciate every contribution. I'm just one person so seeing community members care enough to write code for this project warms my heart. Thank you in advance. From 9c9a9a072ce487f79d4a129c1cf536fc3c3be0cc Mon Sep 17 00:00:00 2001 From: OlshaMB Date: Sun, 28 Jan 2024 00:24:17 +0300 Subject: [PATCH 09/34] 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 9d6e0158cac8fe2fb800968fefa744121714c247 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Sun, 28 Jan 2024 16:00:11 -0500 Subject: [PATCH 10/34] 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 a1a335a4801f9b73e1d6a4f92b6f8229d3bdc521 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Sun, 28 Jan 2024 16:01:07 -0500 Subject: [PATCH 11/34] 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 981653c54a45b89626935e7a34e1e76b1be5190f Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Sun, 28 Jan 2024 16:13:31 -0500 Subject: [PATCH 12/34] 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 ba70771..b4343ac 100644 --- a/cli/install.go +++ b/cli/install.go @@ -331,6 +331,10 @@ func (z *ZVM) InstallZls(version string) error { } defer response.Body.Close() + // if resp.ContentLength == 0 { + // return fmt.Errorf("invalid ZLS content length (%d bytes)", resp.ContentLength) + // } + pbar := progressbar.DefaultBytes( int64(response.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) } } From 4e9865e6eb41a0c208ffbb2c585fb122a8ee3b76 Mon Sep 17 00:00:00 2001 From: iamtimmy <58427647+iamtimmy@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:00:37 +0100 Subject: [PATCH 13/34] also fixed zls master installs for windows --- cli/install.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cli/install.go b/cli/install.go index b4343ac..0e8c90b 100644 --- a/cli/install.go +++ b/cli/install.go @@ -292,6 +292,7 @@ func (z *ZVM) InstallZls(version string) error { fmt.Println("Finding ZLS executable...") + // make sure dir exists installDir := filepath.Join(z.zvmBaseDir, version) err := os.MkdirAll(installDir, 0755) @@ -301,6 +302,7 @@ func (z *ZVM) InstallZls(version string) error { arch, osType := zigStyleSysInfo() expectedArchOs := fmt.Sprintf("%v-%v", arch, osType) + expectedArchOs := fmt.Sprintf("%v-%v", arch, osType) filename := "zls" if osType == "windows" { @@ -315,6 +317,7 @@ func (z *ZVM) InstallZls(version string) error { downloadUrl, err := getZLSDownloadUrl(version, expectedArchOs) if err != nil { + fmt.Println("what?") return err } From 53e258de31e15cd6cb8e2a91a84a7b8040ef7c1f Mon Sep 17 00:00:00 2001 From: iamtimmy <58427647+iamtimmy@users.noreply.github.com> Date: Sun, 28 Jan 2024 23:11:00 +0100 Subject: [PATCH 14/34] final touches? in accordance with comments --- cli/install.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/cli/install.go b/cli/install.go index 0e8c90b..57329b5 100644 --- a/cli/install.go +++ b/cli/install.go @@ -292,7 +292,6 @@ func (z *ZVM) InstallZls(version string) error { fmt.Println("Finding ZLS executable...") - // make sure dir exists installDir := filepath.Join(z.zvmBaseDir, version) err := os.MkdirAll(installDir, 0755) @@ -302,7 +301,6 @@ func (z *ZVM) InstallZls(version string) error { arch, osType := zigStyleSysInfo() expectedArchOs := fmt.Sprintf("%v-%v", arch, osType) - expectedArchOs := fmt.Sprintf("%v-%v", arch, osType) filename := "zls" if osType == "windows" { @@ -317,7 +315,6 @@ func (z *ZVM) InstallZls(version string) error { downloadUrl, err := getZLSDownloadUrl(version, expectedArchOs) if err != nil { - fmt.Println("what?") return err } @@ -334,10 +331,6 @@ func (z *ZVM) InstallZls(version string) error { } defer response.Body.Close() - // if resp.ContentLength == 0 { - // return fmt.Errorf("invalid ZLS content length (%d bytes)", resp.ContentLength) - // } - pbar := progressbar.DefaultBytes( int64(response.ContentLength), "Downloading ZLS", @@ -348,10 +341,10 @@ func (z *ZVM) InstallZls(version string) error { if shouldUnzip == false { file, err := os.Create(binaryLocation) - if err != nil { return err } + defer file.Close() if _, err := io.Copy(io.MultiWriter(pbar, file), response.Body); err != nil { return err From b32dcef3e66aa468a86faf0bc2ee53349d6ad903 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Mon, 29 Jan 2024 21:21:45 -0500 Subject: [PATCH 15/34] update dependencies --- go.mod | 4 ++-- go.sum | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index d373aa7..8cc85ae 100644 --- a/go.mod +++ b/go.mod @@ -19,8 +19,8 @@ require ( github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.2 // indirect - github.com/rivo/uniseg v0.4.4 // indirect - golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect + github.com/rivo/uniseg v0.4.6 // indirect + golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/term v0.16.0 // indirect ) diff --git a/go.sum b/go.sum index 91ecf29..975831d 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg= +github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/schollz/progressbar/v3 v3.14.1 h1:VD+MJPCr4s3wdhTc7OEJ/Z3dAeBzJ7yKH/P4lC5yRTI= github.com/schollz/progressbar/v3 v3.14.1/go.mod h1:Zc9xXneTzWXF81TGoqL71u0sBPjULtEHYtj/WVgVy8E= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -39,6 +41,8 @@ github.com/tristanisham/clr v0.0.0-20221004001624-00ee60046d85 h1:zD4b2hs7jZ2sJt github.com/tristanisham/clr v0.0.0-20221004001624-00ee60046d85/go.mod h1:cKn2HV8Beq81OHjb2gja2ZiU4HAEQ6LSuxyaIT5Mg7o= golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc h1:ao2WRsKSzW6KuUY9IWPwWahcHCgR0s52IfwutMfEbdM= golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= +golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA= +golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08= golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From e690524fd879f9c20dc8317758dc316d853b5fb3 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Mon, 29 Jan 2024 22:40:06 -0500 Subject: [PATCH 16/34] one step closer to working vmu --- .gitignore | 3 ++- README.md | 10 ++++++---- cli/config.go | 5 +++-- cli/install.go | 2 +- cli/settings.go | 12 ++++++++++-- cli/version.go | 21 +++++++++++++++++++++ help.txt | 8 +++----- main.go | 19 ++++++++++--------- 8 files changed, 56 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index bd5109f..6aff5ec 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ /dist /demo zvm -testdata \ No newline at end of file +testdata +.env \ No newline at end of file diff --git a/README.md b/README.md index 60de1cc..8e5b6d3 100644 --- a/README.md +++ b/README.md @@ -198,8 +198,10 @@ zvm help ## Option flags ```sh ---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 +-color # Turn ANSI color printing on or off for ZVM's output, i.e. -color=true +-vmu # Changes the version map url (good if you host your own Zig distrobution server). + # ZVM expects properly formatted URLs with the included protocol. + # URLs that don't serve workable JSON files will break ZVM. If you ever want to reset + # your version map url, just run -vmu default. + # -vmu "https://my-cdn.local/zig.json" ``` diff --git a/cli/config.go b/cli/config.go index 4c7f7e8..c899a2e 100644 --- a/cli/config.go +++ b/cli/config.go @@ -37,6 +37,7 @@ func Initialize() *ZVM { if errors.Is(err, ErrNoSettings) { zvm.Settings = Settings{ UseColor: true, + VersionMapUrl: "https://ziglang.org/download/index.json", } out_settings, err := json.MarshalIndent(&zvm.Settings, "", " ") @@ -116,12 +117,12 @@ func (z ZVM) getVersion(version string) error { } func (z *ZVM) loadSettings() error { - set_path := filepath.Join(z.zvmBaseDir, "settings.json") + set_path := z.Settings.basePath if _, err := os.Stat(set_path); errors.Is(err, os.ErrNotExist) { return ErrNoSettings } - data, err := os.ReadFile(filepath.Join(z.zvmBaseDir, "settings.json")) + data, err := os.ReadFile(set_path) if err != nil { return err } diff --git a/cli/install.go b/cli/install.go index d1f49bf..4bd9d25 100644 --- a/cli/install.go +++ b/cli/install.go @@ -343,7 +343,7 @@ func (z *ZVM) InstallZls(version string) error { versionPath := filepath.Join(z.zvmBaseDir, version) binaryLocation := filepath.Join(versionPath, filename) - if shouldUnzip == false { + if !shouldUnzip { file, err := os.Create(binaryLocation) if err != nil { return err diff --git a/cli/settings.go b/cli/settings.go index 6c3e628..e2b4a6d 100644 --- a/cli/settings.go +++ b/cli/settings.go @@ -3,10 +3,11 @@ package cli import ( "encoding/json" "fmt" - "log" "net/url" "os" + "github.com/charmbracelet/log" + "github.com/tristanisham/clr" ) @@ -31,8 +32,13 @@ func (s *Settings) ToggleColor() { } -func (s *Settings) ResetVersionMap() { +func (s *Settings) ResetVersionMap() error { s.VersionMapUrl = "https://ziglang.org/download/index.json" + if err := s.save(); err != nil { + return err + } + + return nil } func (s *Settings) NoColor() { @@ -71,6 +77,8 @@ func (s *Settings) SetVersionMapUrl(versionMapUrl string) error { return err } + log.Debug("set version map url", "url", s.VersionMapUrl) + return nil } diff --git a/cli/version.go b/cli/version.go index a993a99..211b1f7 100644 --- a/cli/version.go +++ b/cli/version.go @@ -2,21 +2,42 @@ package cli import ( "encoding/json" + "fmt" "io" "net/http" "os" "path/filepath" "zvm/cli/meta" + + "github.com/charmbracelet/log" + "github.com/tristanisham/clr" ) func (z *ZVM) fetchVersionMap() (zigVersionMap, error) { + log.Debug("inital VMU", "url", z.Settings.VersionMapUrl) + + if err := z.loadSettings(); err != nil { + log.Warnf("could not read settings: %q", err) + log.Debug("vmu", z.Settings.VersionMapUrl) + } + defaultVersionMapUrl := "https://ziglang.org/download/index.json" + versionMapUrl := z.Settings.VersionMapUrl + + log.Debug("setting's VMU", "url", versionMapUrl) + if len(versionMapUrl) == 0 { versionMapUrl = defaultVersionMapUrl } + // Limited warning until I get to properly test this code. + if versionMapUrl != defaultVersionMapUrl { + fmt.Println("This command is currently in beta and may break your install.") + fmt.Printf("To reset your version map, run %s", clr.Green("zvm -vmu default")) + } + req, err := http.NewRequest("GET", versionMapUrl, nil) if err != nil { return nil, err diff --git a/help.txt b/help.txt index eb0febc..2b82f07 100644 --- a/help.txt +++ b/help.txt @@ -30,12 +30,10 @@ help Prints this message. ------------- Flags ----------------- - - ---color= | Turn color printing on or off for ZVM's output - - +-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) + URLs that don't serve workable JSON files will break ZVM. If you ever want to reset + your version map url, just run -vmu default. Looking for more help? https://github.com/tristanisham/zvm diff --git a/main.go b/main.go index 465baf5..e77f413 100644 --- a/main.go +++ b/main.go @@ -48,9 +48,16 @@ func main() { flag.Parse() if sVersionMapUrl != nil { - if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { - log.Fatal(err) + if *sVersionMapUrl == "default" { + if err := zvm.Settings.ResetVersionMap(); err != nil { + log.Fatal(err) + } + } else { + if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { + log.Fatal(err) + } } + } if sColorToggle != nil { @@ -76,7 +83,7 @@ func main() { req := cli.ExtractInstall(args[len(args)-1]) req.Version = strings.TrimPrefix(req.Version, "v") - log.Debug(req, "deps", *installDeps) + // log.Debug(req, "deps", *installDeps) if err := zvm.Install(req.Package); err != nil { log.Fatal(err) @@ -159,12 +166,6 @@ func main() { return // Settings - case "--nocolor", "--nocolour": - zvm.Settings.NoColor() - case "--color", "--colour": - zvm.Settings.ToggleColor() - case "--yescolor", "--yescolour": - zvm.Settings.YesColor() default: log.Fatalf("invalid argument %q. Please run `zvm help`.\n", arg) } From 8c41b872fd7752a168e5bf5e4cacf5e87acf82b8 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Mon, 29 Jan 2024 22:41:55 -0500 Subject: [PATCH 17/34] added warning to docs --- README.md | 3 +++ help.txt | 2 ++ main.go | 1 + 3 files changed, 6 insertions(+) diff --git a/README.md b/README.md index 8e5b6d3..ab08389 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,9 @@ zvm help ```sh -color # Turn ANSI color printing on or off for ZVM's output, i.e. -color=true +``` +### BETA FLAGS: (Might not work as expected) +```sh -vmu # Changes the version map url (good if you host your own Zig distrobution server). # ZVM expects properly formatted URLs with the included protocol. # URLs that don't serve workable JSON files will break ZVM. If you ever want to reset diff --git a/help.txt b/help.txt index 2b82f07..d90cf21 100644 --- a/help.txt +++ b/help.txt @@ -32,6 +32,8 @@ help ------------- Flags ----------------- -color= | Turn color printing on or off for ZVM's output +BETA FLAGS: (Might not work as expected) + -vmu | Changes the version map url (Good if you host your own Zig distrobution server) URLs that don't serve workable JSON files will break ZVM. If you ever want to reset your version map url, just run -vmu default. diff --git a/main.go b/main.go index e77f413..ecea4ba 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,7 @@ func main() { flag.Parse() if sVersionMapUrl != nil { + log.Warn("this is a beta flag, and may not behave as expected.") if *sVersionMapUrl == "default" { if err := zvm.Settings.ResetVersionMap(); err != nil { log.Fatal(err) From 3c9ee2f111f00f27efb0a21ca290e7ad53b14160 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Mon, 29 Jan 2024 22:42:34 -0500 Subject: [PATCH 18/34] removed useless file --- .gitmodules | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index e69de29..0000000 From 8843986f7fa63966a90c23a4fa94ae89e1142e0e Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Tue, 30 Jan 2024 18:35:33 -0500 Subject: [PATCH 19/34] unstable-vmu --- README.md | 4 ++-- cli/version.go | 16 ++++++++-------- help.txt | 4 ++-- main.go | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ab08389..5266957 100644 --- a/README.md +++ b/README.md @@ -202,9 +202,9 @@ zvm help ``` ### BETA FLAGS: (Might not work as expected) ```sh --vmu # Changes the version map url (good if you host your own Zig distrobution server). +-unstable-vmu # Changes the version map url (good if you host your own Zig distrobution server). # ZVM expects properly formatted URLs with the included protocol. # URLs that don't serve workable JSON files will break ZVM. If you ever want to reset - # your version map url, just run -vmu default. + # your version map url, just run -unstable-vmu default. # -vmu "https://my-cdn.local/zig.json" ``` diff --git a/cli/version.go b/cli/version.go index 211b1f7..625c5ce 100644 --- a/cli/version.go +++ b/cli/version.go @@ -2,7 +2,7 @@ package cli import ( "encoding/json" - "fmt" + // "fmt" "io" "net/http" "os" @@ -10,7 +10,7 @@ import ( "zvm/cli/meta" "github.com/charmbracelet/log" - "github.com/tristanisham/clr" + // "github.com/tristanisham/clr" ) func (z *ZVM) fetchVersionMap() (zigVersionMap, error) { @@ -25,18 +25,18 @@ func (z *ZVM) fetchVersionMap() (zigVersionMap, error) { defaultVersionMapUrl := "https://ziglang.org/download/index.json" versionMapUrl := z.Settings.VersionMapUrl - + log.Debug("setting's VMU", "url", versionMapUrl) if len(versionMapUrl) == 0 { versionMapUrl = defaultVersionMapUrl } - // Limited warning until I get to properly test this code. - if versionMapUrl != defaultVersionMapUrl { - fmt.Println("This command is currently in beta and may break your install.") - fmt.Printf("To reset your version map, run %s", clr.Green("zvm -vmu default")) - } + // // Limited warning until I get to properly test this code. + // if versionMapUrl != defaultVersionMapUrl { + // fmt.Println("This command is currently in beta and may break your install.") + // fmt.Printf("To reset your version map, run %s", clr.Green("zvm -unstable-vmu default")) + // } req, err := http.NewRequest("GET", versionMapUrl, nil) if err != nil { diff --git a/help.txt b/help.txt index d90cf21..0439bcc 100644 --- a/help.txt +++ b/help.txt @@ -34,8 +34,8 @@ help BETA FLAGS: (Might not work as expected) --vmu | Changes the version map url (Good if you host your own Zig distrobution server) +-unstable-vmu | Changes the version map url (Good if you host your own Zig distrobution server) URLs that don't serve workable JSON files will break ZVM. If you ever want to reset - your version map url, just run -vmu default. + your version map url, just run -unstable-vmu default. Looking for more help? https://github.com/tristanisham/zvm diff --git a/main.go b/main.go index ecea4ba..424fc0e 100644 --- a/main.go +++ b/main.go @@ -43,12 +43,12 @@ func main() { 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") + sVersionMapUrl := flag.String("unstable-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 { - log.Warn("this is a beta flag, and may not behave as expected.") + log.Warn("this is a beta flag, and may not behave as expected.\nRun --unstable-vmu default to reset your version map.") if *sVersionMapUrl == "default" { if err := zvm.Settings.ResetVersionMap(); err != nil { log.Fatal(err) From 82e94824765970a39bdbcd02562d93c2fc7263e2 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Tue, 30 Jan 2024 18:47:29 -0500 Subject: [PATCH 20/34] updated clean --- cli/clean.go | 12 ++++-------- cli/config.go | 12 ++++++------ cli/install.go | 38 +++++++++++++++++++------------------- cli/ls.go | 2 +- cli/uninstall.go | 2 +- cli/upgrade.go | 22 ++++++++++++++-------- cli/use.go | 6 +++--- cli/version.go | 2 +- main.go | 5 +++-- 9 files changed, 52 insertions(+), 49 deletions(-) diff --git a/cli/clean.go b/cli/clean.go index e677a52..3519f11 100644 --- a/cli/clean.go +++ b/cli/clean.go @@ -3,22 +3,18 @@ package cli import ( "os" "path/filepath" - "strings" ) func (z *ZVM) Clean() error { - dir, err := os.ReadDir(z.zvmBaseDir) + dir, err := os.ReadDir(z.baseDir) if err != nil { return err } for _, entry := range dir { - if strings.Contains(entry.Name(), "bin") { - continue - } - - if filepath.Ext(entry.Name()) == ".zip" || filepath.Ext(entry.Name()) == ".xz" { - if err := os.Remove(filepath.Join(z.zvmBaseDir, entry.Name())); err != nil { + + if filepath.Ext(entry.Name()) == ".zip" || filepath.Ext(entry.Name()) == ".xz" || filepath.Ext(entry.Name()) == ".tar" { + if err := os.Remove(filepath.Join(z.baseDir, entry.Name())); err != nil { return err } } diff --git a/cli/config.go b/cli/config.go index c899a2e..4ac4824 100644 --- a/cli/config.go +++ b/cli/config.go @@ -30,13 +30,13 @@ func Initialize() *ZVM { } zvm := &ZVM{ - zvmBaseDir: zvm_path, + baseDir: zvm_path, } if err := zvm.loadSettings(); err != nil { if errors.Is(err, ErrNoSettings) { zvm.Settings = Settings{ - UseColor: true, + UseColor: true, VersionMapUrl: "https://ziglang.org/download/index.json", } @@ -56,8 +56,8 @@ func Initialize() *ZVM { } type ZVM struct { - zvmBaseDir string - Settings Settings + baseDir string + Settings Settings } // A representaiton of the offical json schema for Zig versions @@ -89,10 +89,10 @@ type ZigOnlVersion = map[string][]map[string]string // } func (z ZVM) getVersion(version string) error { - if _, err := os.Stat(filepath.Join(z.zvmBaseDir, version)); err != nil { + if _, err := os.Stat(filepath.Join(z.baseDir, version)); err != nil { return err } - targetZig := strings.TrimSpace(filepath.Join(z.zvmBaseDir, version, "zig")) + targetZig := strings.TrimSpace(filepath.Join(z.baseDir, version, "zig")) cmd := exec.Command(targetZig, "version") var zigVersion strings.Builder cmd.Stdout = &zigVersion diff --git a/cli/install.go b/cli/install.go index 4bd9d25..8bc8c61 100644 --- a/cli/install.go +++ b/cli/install.go @@ -26,7 +26,7 @@ import ( func (z *ZVM) Install(version string) error { - os.Mkdir(z.zvmBaseDir, 0755) + os.Mkdir(z.baseDir, 0755) rawVersionStructure, err := z.fetchVersionMap() if err != nil { return err @@ -68,7 +68,7 @@ func (z *ZVM) Install(version string) error { pathEnding = "*.tar.xz" } - tempDir, err := os.CreateTemp(z.zvmBaseDir, pathEnding) + tempDir, err := os.CreateTemp(z.baseDir, pathEnding) if err != nil { return err } @@ -121,7 +121,7 @@ func (z *ZVM) Install(version string) error { // installedVersionPath := filepath.Join(z.zvmBaseDir, version) fmt.Println("Extracting bundle...") - if err := ExtractBundle(tempDir.Name(), z.zvmBaseDir); err != nil { + if err := ExtractBundle(tempDir.Name(), z.baseDir); err != nil { log.Fatal(err) } var tarName string @@ -148,8 +148,8 @@ func (z *ZVM) Install(version string) error { if wasZigOnl { - untarredPath := filepath.Join(z.zvmBaseDir, fmt.Sprintf("zig-%s-%s-%s", zigOS, zigArch, version)) - newPath := filepath.Join(z.zvmBaseDir, tarName) + untarredPath := filepath.Join(z.baseDir, fmt.Sprintf("zig-%s-%s-%s", zigOS, zigArch, version)) + newPath := filepath.Join(z.baseDir, tarName) if _, err := os.Stat(untarredPath); err == nil { if os.Stat(newPath); err == nil { @@ -167,15 +167,15 @@ func (z *ZVM) Install(version string) error { } } else { - if err := os.Rename(filepath.Join(z.zvmBaseDir, tarName), filepath.Join(z.zvmBaseDir, version)); err != nil { - if _, err := os.Stat(filepath.Join(z.zvmBaseDir, version)); err == nil { + if err := os.Rename(filepath.Join(z.baseDir, tarName), filepath.Join(z.baseDir, version)); err != nil { + if _, err := os.Stat(filepath.Join(z.baseDir, version)); err == nil { // Room here to make the backup file. - log.Debug("removing", "path", filepath.Join(z.zvmBaseDir, version)) - if err := os.RemoveAll(filepath.Join(z.zvmBaseDir, version)); err != nil { + log.Debug("removing", "path", filepath.Join(z.baseDir, version)) + if err := os.RemoveAll(filepath.Join(z.baseDir, version)); err != nil { log.Fatal(err) } else { - oldName := filepath.Join(z.zvmBaseDir, tarName) - newName := filepath.Join(z.zvmBaseDir, version) + oldName := filepath.Join(z.baseDir, tarName) + newName := filepath.Join(z.baseDir, version) log.Debug("renaming", "old", oldName, "new", newName, "identical", oldName == newName) if oldName != newName { if err := os.Rename(oldName, newName); err != nil { @@ -189,7 +189,7 @@ func (z *ZVM) Install(version string) error { } // This removes the extra download - if err := os.RemoveAll(filepath.Join(z.zvmBaseDir, tarName)); err != nil { + if err := os.RemoveAll(filepath.Join(z.baseDir, tarName)); err != nil { log.Warn(err) } } @@ -293,7 +293,7 @@ func (z *ZVM) InstallZls(version string) error { fmt.Println("Finding ZLS executable...") // make sure dir exists - installDir := filepath.Join(z.zvmBaseDir, version) + installDir := filepath.Join(z.baseDir, version) err := os.MkdirAll(installDir, 0755) if err != nil { return err @@ -340,7 +340,7 @@ func (z *ZVM) InstallZls(version string) error { "Downloading ZLS", ) - versionPath := filepath.Join(z.zvmBaseDir, version) + versionPath := filepath.Join(z.baseDir, version) binaryLocation := filepath.Join(versionPath, filename) if !shouldUnzip { @@ -361,7 +361,7 @@ func (z *ZVM) InstallZls(version string) error { pathEnding = "*.tar.xz" } - tempDir, err := os.CreateTemp(z.zvmBaseDir, pathEnding) + tempDir, err := os.CreateTemp(z.baseDir, pathEnding) if err != nil { return err } @@ -374,7 +374,7 @@ func (z *ZVM) InstallZls(version string) error { } fmt.Println("Extracting ZLS...") - if err := ExtractBundle(tempDir.Name(), filepath.Join(z.zvmBaseDir, version)); err != nil { + if err := ExtractBundle(tempDir.Name(), filepath.Join(z.baseDir, version)); err != nil { log.Fatal(err) } if err := os.Rename(filepath.Join(versionPath, "bin", filename), filepath.Join(versionPath, filename)); err != nil { @@ -392,15 +392,15 @@ func (z *ZVM) InstallZls(version string) error { } func (z *ZVM) createSymlink(version string) { - if _, err := os.Lstat(filepath.Join(z.zvmBaseDir, "bin")); err == nil { + if _, err := os.Lstat(filepath.Join(z.baseDir, "bin")); err == nil { fmt.Println("Removing old symlink") - if err := os.RemoveAll(filepath.Join(z.zvmBaseDir, "bin")); err != nil { + if err := os.RemoveAll(filepath.Join(z.baseDir, "bin")); err != nil { log.Fatal("could not remove bin", err) } } - if err := os.Symlink(filepath.Join(z.zvmBaseDir, version), filepath.Join(z.zvmBaseDir, "bin")); err != nil { + if err := os.Symlink(filepath.Join(z.baseDir, version), filepath.Join(z.baseDir, "bin")); err != nil { log.Fatal(err) } } diff --git a/cli/ls.go b/cli/ls.go index 92932ee..c4570e4 100644 --- a/cli/ls.go +++ b/cli/ls.go @@ -26,7 +26,7 @@ func (z *ZVM) ListVersions() error { } version := zigVersion.String() - dir, err := os.ReadDir(z.zvmBaseDir) + dir, err := os.ReadDir(z.baseDir) if err != nil { return err } diff --git a/cli/uninstall.go b/cli/uninstall.go index a0fe891..aad1092 100644 --- a/cli/uninstall.go +++ b/cli/uninstall.go @@ -7,7 +7,7 @@ import ( ) func (z *ZVM) Uninstall(ver string) error { - version := filepath.Join(z.zvmBaseDir, ver) + version := filepath.Join(z.baseDir, ver) if _, err := os.Stat(version); err == nil { if err := os.RemoveAll(version); err != nil { diff --git a/cli/upgrade.go b/cli/upgrade.go index 1c3ab82..d1409a2 100644 --- a/cli/upgrade.go +++ b/cli/upgrade.go @@ -7,9 +7,11 @@ import ( "io" "net/http" "os" + // "os/user" "path/filepath" "runtime" + // "syscall" "time" "zvm/cli/meta" @@ -60,11 +62,13 @@ func (z *ZVM) Upgrade() error { } defer resp.Body.Close() - tempDownload, err := os.CreateTemp("", fmt.Sprintf("*.%s", archive)) + tempDownload, err := os.CreateTemp(z.baseDir, fmt.Sprintf("*.%s", archive)) if err != nil { return err } defer tempDownload.Close() + // log.Debug("temp name", "path", tempDownload.Name()) + defer os.Remove(tempDownload.Name()) log.Debug("tempDir", "name", tempDownload.Name()) pbar := progressbar.DefaultBytes( @@ -89,10 +93,11 @@ func (z *ZVM) Upgrade() error { log.Debug("zvmPath", "path", zvmPath) - newTemp, err := os.MkdirTemp("", "zvm-upgrade-*") + newTemp, err := os.MkdirTemp(z.baseDir, "zvm-upgrade-*") if err != nil { return errors.Join(ErrFailedUpgrade, err) } + defer os.RemoveAll(newTemp) if err := untar(tempDownload.Name(), newTemp); err != nil { log.Error(err) @@ -107,6 +112,10 @@ func (z *ZVM) Upgrade() error { return errors.Join(ErrFailedUpgrade, err) } + if err := z.Clean(); err != nil { + log.Warn("ZVM failed to clean up after itself.") + } + return nil } @@ -115,19 +124,19 @@ func (z ZVM) getInstallDir() (string, error) { if !ok { this, err := os.Executable() if err != nil { - return filepath.Join(z.zvmBaseDir, "self"), nil + return filepath.Join(z.baseDir, "self"), nil } itIsASymlink, err := isSymlink(this) if err != nil { - return filepath.Join(z.zvmBaseDir, "self"), nil + return filepath.Join(z.baseDir, "self"), nil } var finalPath string if !itIsASymlink { finalPath, err = resolveSymlink(this) if err != nil { - return filepath.Join(z.zvmBaseDir, "self"), nil + return filepath.Join(z.baseDir, "self"), nil } } else { finalPath = this @@ -214,9 +223,6 @@ func isSymlink(path string) (bool, error) { return fileInfo.Mode()&os.ModeSymlink != 0, nil } - - - func CanIUpgrade() (bool, string, error) { release, err := getLatestGitHubRelease("tristanisham", "zvm") if err != nil { diff --git a/cli/use.go b/cli/use.go index 4c36b4d..f71e9a3 100644 --- a/cli/use.go +++ b/cli/use.go @@ -30,12 +30,12 @@ func (z *ZVM) Use(ver string) error { } func (z *ZVM) setBin(ver string) error { - version_path := filepath.Join(z.zvmBaseDir, ver) - if err := os.Remove(filepath.Join(z.zvmBaseDir, "bin")); err != nil { + version_path := filepath.Join(z.baseDir, ver) + if err := os.Remove(filepath.Join(z.baseDir, "bin")); err != nil { log.Warn(err) } - if err := os.Symlink(version_path, filepath.Join(z.zvmBaseDir, "bin")); err != nil { + if err := os.Symlink(version_path, filepath.Join(z.baseDir, "bin")); err != nil { return err } diff --git a/cli/version.go b/cli/version.go index 625c5ce..6b6cea3 100644 --- a/cli/version.go +++ b/cli/version.go @@ -56,7 +56,7 @@ func (z *ZVM) fetchVersionMap() (zigVersionMap, error) { return nil, err } - if err := os.WriteFile(filepath.Join(z.zvmBaseDir, "versions.json"), versions, 0755); err != nil { + if err := os.WriteFile(filepath.Join(z.baseDir, "versions.json"), versions, 0755); err != nil { return nil, err } diff --git a/main.go b/main.go index 424fc0e..09a5925 100644 --- a/main.go +++ b/main.go @@ -43,17 +43,18 @@ func main() { lsRemote := lsFlagSet.Bool("all", false, "List all available versions of Zig to install") // Global config - sVersionMapUrl := flag.String("unstable-vmu", "https://ziglang.org/download/index.json", "Set ZVM's version map URL for custom Zig distribution servers") + sVersionMapUrl := flag.String("unstable-vmu", "default", "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 { - log.Warn("this is a beta flag, and may not behave as expected.\nRun --unstable-vmu default to reset your version map.") if *sVersionMapUrl == "default" { if err := zvm.Settings.ResetVersionMap(); err != nil { log.Fatal(err) } } else { + log.Warn("this is a beta flag, and may not behave as expected.\nRun --unstable-vmu default to reset your version map.") + if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { log.Fatal(err) } From b232f59d387883e3f9ac7870d740cefe4fd734e6 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Tue, 30 Jan 2024 22:49:13 -0500 Subject: [PATCH 21/34] hid vmu for release --- README.md | 9 +-------- help.txt | 5 ----- main.go | 26 +++++++++++++------------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 5266957..c38ff61 100644 --- a/README.md +++ b/README.md @@ -200,11 +200,4 @@ zvm help ```sh -color # Turn ANSI color printing on or off for ZVM's output, i.e. -color=true ``` -### BETA FLAGS: (Might not work as expected) -```sh --unstable-vmu # Changes the version map url (good if you host your own Zig distrobution server). - # ZVM expects properly formatted URLs with the included protocol. - # URLs that don't serve workable JSON files will break ZVM. If you ever want to reset - # your version map url, just run -unstable-vmu default. - # -vmu "https://my-cdn.local/zig.json" -``` + diff --git a/help.txt b/help.txt index 0439bcc..ad23235 100644 --- a/help.txt +++ b/help.txt @@ -32,10 +32,5 @@ help ------------- Flags ----------------- -color= | Turn color printing on or off for ZVM's output -BETA FLAGS: (Might not work as expected) - --unstable-vmu | Changes the version map url (Good if you host your own Zig distrobution server) - URLs that don't serve workable JSON files will break ZVM. If you ever want to reset - your version map url, just run -unstable-vmu default. Looking for more help? https://github.com/tristanisham/zvm diff --git a/main.go b/main.go index 09a5925..22a8366 100644 --- a/main.go +++ b/main.go @@ -43,24 +43,24 @@ func main() { lsRemote := lsFlagSet.Bool("all", false, "List all available versions of Zig to install") // Global config - sVersionMapUrl := flag.String("unstable-vmu", "default", "Set ZVM's version map URL for custom Zig distribution servers") + // sVersionMapUrl := flag.String("unstable-vmu", "default", "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 *sVersionMapUrl == "default" { - if err := zvm.Settings.ResetVersionMap(); err != nil { - log.Fatal(err) - } - } else { - log.Warn("this is a beta flag, and may not behave as expected.\nRun --unstable-vmu default to reset your version map.") + // if sVersionMapUrl != nil { + // if *sVersionMapUrl == "default" { + // if err := zvm.Settings.ResetVersionMap(); err != nil { + // log.Fatal(err) + // } + // } else { + // log.Warn("this is a beta flag, and may not behave as expected.\nRun --unstable-vmu default to reset your version map.") - if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { - log.Fatal(err) - } - } + // if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { + // log.Fatal(err) + // } + // } - } + // } if sColorToggle != nil { if *sColorToggle != zvm.Settings.UseColor { From e3fe7d470564a54260e8dca0214a511339c822b3 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Wed, 31 Jan 2024 00:59:36 -0500 Subject: [PATCH 22/34] accurate copyright --- help.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help.txt b/help.txt index ad23235..06ec689 100644 --- a/help.txt +++ b/help.txt @@ -1,5 +1,5 @@ zvm (Zig Version Manager) {{.Version}} -© 2023-present Tristan Isham +© 2022-present Tristan Isham -------------------------------- install, i [flags] From 30ef429c3caaff8ef71a521befe92863f171ff55 Mon Sep 17 00:00:00 2001 From: OlshaMB Date: Wed, 31 Jan 2024 09:00:30 +0300 Subject: [PATCH 23/34] fix: Fixes the #42 bug --- cli/config.go | 3 ++- main.go | 15 ++++----------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/cli/config.go b/cli/config.go index c899a2e..9606473 100644 --- a/cli/config.go +++ b/cli/config.go @@ -33,6 +33,8 @@ func Initialize() *ZVM { zvmBaseDir: zvm_path, } + zvm.Settings.basePath = filepath.Join(zvm_path, "settings.json") + if err := zvm.loadSettings(); err != nil { if errors.Is(err, ErrNoSettings) { zvm.Settings = Settings{ @@ -51,7 +53,6 @@ func Initialize() *ZVM { } } - zvm.Settings.basePath = filepath.Join(zvm_path, "settings.json") return zvm } diff --git a/main.go b/main.go index ecea4ba..c188ac2 100644 --- a/main.go +++ b/main.go @@ -43,22 +43,15 @@ func main() { 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") + sVersionMapUrl := flag.String("vmu", "default", "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 *sVersionMapUrl != "default" { log.Warn("this is a beta flag, and may not behave as expected.") - if *sVersionMapUrl == "default" { - if err := zvm.Settings.ResetVersionMap(); err != nil { - log.Fatal(err) - } - } else { - if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { - log.Fatal(err) - } + if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { + log.Fatal(err) } - } if sColorToggle != nil { From fd1f06c07b04f1f38c8b9a59b327475a71602fd8 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Wed, 31 Jan 2024 22:45:29 -0500 Subject: [PATCH 24/34] updated to allow reset and ignore regular runs --- cli/version.go | 6 ------ main.go | 19 ++++++++++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/cli/version.go b/cli/version.go index 6b6cea3..92f442b 100644 --- a/cli/version.go +++ b/cli/version.go @@ -32,12 +32,6 @@ func (z *ZVM) fetchVersionMap() (zigVersionMap, error) { versionMapUrl = defaultVersionMapUrl } - // // Limited warning until I get to properly test this code. - // if versionMapUrl != defaultVersionMapUrl { - // fmt.Println("This command is currently in beta and may break your install.") - // fmt.Printf("To reset your version map, run %s", clr.Green("zvm -unstable-vmu default")) - // } - req, err := http.NewRequest("GET", versionMapUrl, nil) if err != nil { return nil, err diff --git a/main.go b/main.go index c188ac2..bdb74b1 100644 --- a/main.go +++ b/main.go @@ -43,15 +43,24 @@ func main() { lsRemote := lsFlagSet.Bool("all", false, "List all available versions of Zig to install") // Global config - sVersionMapUrl := flag.String("vmu", "default", "Set ZVM's version map URL for custom Zig distribution servers") + sVersionMapUrl := flag.String("vmu", "", "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 != "default" { - log.Warn("this is a beta flag, and may not behave as expected.") - if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { - log.Fatal(err) + if sVersionMapUrl != nil && len(*sVersionMapUrl) != 0 { + log.Debug("user passed vmu", "url", *sVersionMapUrl) + if *sVersionMapUrl == "default" { + if err := zvm.Settings.ResetVersionMap(); err != nil { + log.Fatal(err) + } + } else { + log.Warn("this is a beta flag, and may not behave as expected.\nRun `-vmu default` to reset your version map.") + + if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { + log.Fatal(err) + } } + } if sColorToggle != nil { From 7fbedccbcf598e4fdd235c9ac4c057bbfff2694c Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Wed, 31 Jan 2024 23:04:58 -0500 Subject: [PATCH 25/34] handle invalid version map marshaling --- cli/version.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cli/version.go b/cli/version.go index 92f442b..544c2db 100644 --- a/cli/version.go +++ b/cli/version.go @@ -2,6 +2,9 @@ package cli import ( "encoding/json" + "errors" + "fmt" + // "fmt" "io" "net/http" @@ -50,12 +53,17 @@ func (z *ZVM) fetchVersionMap() (zigVersionMap, error) { return nil, err } - if err := os.WriteFile(filepath.Join(z.baseDir, "versions.json"), versions, 0755); err != nil { + rawVersionStructure := make(zigVersionMap) + if err := json.Unmarshal(versions, &rawVersionStructure); err != nil { + var syntaxErr *json.SyntaxError + if errors.As(err, &syntaxErr) { + return nil, fmt.Errorf("%w: %w", ErrInvalidVersionMap, err) + } + return nil, err } - rawVersionStructure := make(zigVersionMap) - if err := json.Unmarshal(versions, &rawVersionStructure); err != nil { + if err := os.WriteFile(filepath.Join(z.baseDir, "versions.json"), versions, 0755); err != nil { return nil, err } From f0b34ba38910be697ce04e9e200014d0ee33ea2e Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Wed, 31 Jan 2024 23:07:02 -0500 Subject: [PATCH 26/34] upodated docs --- README.md | 2 ++ help.txt | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c38ff61..0e07d81 100644 --- a/README.md +++ b/README.md @@ -199,5 +199,7 @@ zvm help ```sh -color # Turn ANSI color printing on or off for ZVM's output, i.e. -color=true +-vmu="https://validurl.local/vmu.json" # Change the source ZVM pulls Zig release information from. Good for self-hosted Zig CDNs. + # ZVM only supports schemas that match the offical version map schema. ``` diff --git a/help.txt b/help.txt index 06ec689..d0dfcac 100644 --- a/help.txt +++ b/help.txt @@ -31,6 +31,6 @@ help ------------- Flags ----------------- -color= | Turn color printing on or off for ZVM's output - +-vmu="https://validurl.local/vmu.json" | Change the source ZVM pulls Zig release information from. Good for self-hosted Zig CDNs. Looking for more help? https://github.com/tristanisham/zvm From 4802d1758cc2225192cb335c955c469e6ca6308f Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Wed, 31 Jan 2024 23:07:35 -0500 Subject: [PATCH 27/34] v0.5.2 --- cli/meta/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/meta/version.go b/cli/meta/version.go index d313be8..e577256 100644 --- a/cli/meta/version.go +++ b/cli/meta/version.go @@ -1,4 +1,4 @@ package meta -const VERSION = "v0.5.1" +const VERSION = "v0.5.2" From 219b7b52619a3a0fc839c7be3645816eccdc1542 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Wed, 31 Jan 2024 23:10:41 -0500 Subject: [PATCH 28/34] docs --- README.md | 1 + help.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 0e07d81..1ea3eae 100644 --- a/README.md +++ b/README.md @@ -201,5 +201,6 @@ zvm help -color # Turn ANSI color printing on or off for ZVM's output, i.e. -color=true -vmu="https://validurl.local/vmu.json" # Change the source ZVM pulls Zig release information from. Good for self-hosted Zig CDNs. # ZVM only supports schemas that match the offical version map schema. + # Run `-vmu=default` to reset your version map. ``` diff --git a/help.txt b/help.txt index d0dfcac..6ab41ea 100644 --- a/help.txt +++ b/help.txt @@ -32,5 +32,6 @@ help ------------- Flags ----------------- -color= | Turn color printing on or off for ZVM's output -vmu="https://validurl.local/vmu.json" | Change the source ZVM pulls Zig release information from. Good for self-hosted Zig CDNs. + | Run `-vmu=default` to reset your version map. Looking for more help? https://github.com/tristanisham/zvm From 9c39e67dd994d6c5bb10d58d47ea66b0f5b6a6ea Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Thu, 1 Feb 2024 18:50:45 -0500 Subject: [PATCH 29/34] added mach alias --- cli/meta/version.go | 2 +- cli/settings.go | 1 + main.go | 11 ++++++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cli/meta/version.go b/cli/meta/version.go index e577256..0880874 100644 --- a/cli/meta/version.go +++ b/cli/meta/version.go @@ -1,4 +1,4 @@ package meta -const VERSION = "v0.5.2" +const VERSION = "v0.5.3" diff --git a/cli/settings.go b/cli/settings.go index e2b4a6d..43a2b4a 100644 --- a/cli/settings.go +++ b/cli/settings.go @@ -67,6 +67,7 @@ func (s *Settings) SetColor(answer bool) { } func (s *Settings) SetVersionMapUrl(versionMapUrl string) error { + log.Info("Run `-vmu default` to reset your version map.") if err := isValidWebURL(versionMapUrl); err != nil { return fmt.Errorf("%w: %w", ErrInvalidVersionMap, err) diff --git a/main.go b/main.go index bdb74b1..62e4e39 100644 --- a/main.go +++ b/main.go @@ -49,12 +49,17 @@ func main() { if sVersionMapUrl != nil && len(*sVersionMapUrl) != 0 { log.Debug("user passed vmu", "url", *sVersionMapUrl) - if *sVersionMapUrl == "default" { + switch *sVersionMapUrl { + case "default": if err := zvm.Settings.ResetVersionMap(); err != nil { log.Fatal(err) } - } else { - log.Warn("this is a beta flag, and may not behave as expected.\nRun `-vmu default` to reset your version map.") + case "mach": + if err := zvm.Settings.SetVersionMapUrl("https://machengine.org/zig/index.json"); err != nil { + log.Fatal(err) + } + + default: if err := zvm.Settings.SetVersionMapUrl(*sVersionMapUrl); err != nil { log.Fatal(err) From dc1c6c755fa222daad607d1cd409423697a4fb80 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Thu, 1 Feb 2024 19:18:56 -0500 Subject: [PATCH 30/34] update docs --- README.md | 9 +++++++++ help.txt | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ea3eae..9d38a88 100644 --- a/README.md +++ b/README.md @@ -197,10 +197,19 @@ zvm help ## Option flags +### Color Toggle ```sh -color # Turn ANSI color printing on or off for ZVM's output, i.e. -color=true + +``` + +### Version Map Source +```sh -vmu="https://validurl.local/vmu.json" # Change the source ZVM pulls Zig release information from. Good for self-hosted Zig CDNs. # ZVM only supports schemas that match the offical version map schema. # Run `-vmu=default` to reset your version map. + +-vmu default # Resets back to default Zig releases. +-vmu mach # Sets ZVM to pull from Mach nominated Zig. ``` diff --git a/help.txt b/help.txt index 6ab41ea..886f318 100644 --- a/help.txt +++ b/help.txt @@ -33,5 +33,7 @@ help -color= | Turn color printing on or off for ZVM's output -vmu="https://validurl.local/vmu.json" | Change the source ZVM pulls Zig release information from. Good for self-hosted Zig CDNs. | Run `-vmu=default` to reset your version map. - + + -vmu default | Resets back to default Zig releases. + -vmu mach | Sets ZVM to pull from Mach nominated Zig. Looking for more help? https://github.com/tristanisham/zvm From f4ad45e9ea895047c85ddec0dc95998f9262cb14 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Thu, 1 Feb 2024 20:34:45 -0500 Subject: [PATCH 31/34] Update README.md Removed Discord --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 9d38a88..52fadbc 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ ## Join our Community -- [Discord](https://discord.gg/NhaNhCMYX8) - [Twitch](https://twitch.tv/atalocke)
From a9d688060e4d0cbaa2657461d7f206546daf9b3d Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Thu, 1 Feb 2024 20:35:07 -0500 Subject: [PATCH 32/34] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 52fadbc..4057219 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ ## Join our Community - [Twitch](https://twitch.tv/atalocke) +- [Twitter|X](https://twitter.com/atalocke)
From 67f1481ca0092c33300b4167d7acb9ec9def539f Mon Sep 17 00:00:00 2001 From: Nithin K Joy Date: Tue, 6 Feb 2024 23:15:38 +0530 Subject: [PATCH 33/34] Updated deprecated Deno.run() API to Deno.Command() API --- build.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/build.ts b/build.ts index 6bc2abc..904280f 100755 --- a/build.ts +++ b/build.ts @@ -32,18 +32,18 @@ for (const os of GOOS) { Deno.env.set("GOARCH", ar); const zvm_str = `zvm-${os}-${ar}`; console.time(`Build zvm: ${zvm_str}`); - // deno-lint-ignore no-deprecated-deno-api - const build_cmd = Deno.run({ - cmd: [ - "go", + + const build_cmd = new Deno.Command("go", { + args: [ "build", "-o", - `build/${zvm_str}/zvm${(os == "windows" ? ".exe" : "")}`, - "-ldflags=-w -s", "-trimpath", + `build/${zvm_str}/zvm${os === "windows" ? ".exe" : ""}`, + "-ldflags=-w -s", + "-trimpath", ], }); - const { code } = await build_cmd.status(); + const { code } = await build_cmd.output(); if (code !== 0) { console.error("Something went wrong"); Deno.exit(1); From a30c6a0b275079b8f5f3ff71afb0ea898bffc733 Mon Sep 17 00:00:00 2001 From: Tristan Isham Date: Sun, 11 Feb 2024 12:58:29 -0500 Subject: [PATCH 34/34] Updated and tested build.ts --- .gitignore | 14 +++++++------- build.ts | 12 +++++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 6aff5ec..83d25c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ -/zig-cache -/zig-out -/build -/dist -/demo +zig-cache/ +zig-out/ +build/ +dist/ +demo/ zvm -testdata -.env \ No newline at end of file +testdata/ +.env* \ No newline at end of file diff --git a/build.ts b/build.ts index 904280f..36ca615 100755 --- a/build.ts +++ b/build.ts @@ -61,19 +61,21 @@ for (const os of GOOS) { } const zvm_str = `zvm-${os}-${ar}`; - if (os == "windows") { - console.time(`Compress zvm: ${zvm_str}`); + if (os === "windows") { + console.time(`Compress zvm (zip): ${zvm_str}`); const zip = new Deno.Command(`zip`, { args: [`${zvm_str}.zip`, `${zvm_str}/zvm.exe`], stdin: "piped", stdout: "piped", }); + zip.spawn(); - console.timeEnd(`Compress zvm: ${zvm_str}`); + + console.timeEnd(`Compress zvm (zip): ${zvm_str}`); continue; } const tar = new Tar(); - console.time(`Compress zvm: ${zvm_str}`); + console.time(`Compress zvm (tar): ${zvm_str}`); await tar.append("zvm", { filePath: `${zvm_str}/zvm`, }); @@ -83,7 +85,7 @@ for (const os of GOOS) { }); await copy(tar.getReader(), writer); writer.close(); - console.timeEnd(`Compress zvm: ${zvm_str}`); + console.timeEnd(`Compress zvm (tar): ${zvm_str}`); } }