Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into olshamb/bugfix-42
Browse files Browse the repository at this point in the history
  • Loading branch information
OlshaMB committed Jan 31, 2024
2 parents 30ef429 + e3fe7d4 commit 797bb58
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 69 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-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"
```

12 changes: 4 additions & 8 deletions cli/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
12 changes: 6 additions & 6 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ func Initialize() *ZVM {
}

zvm := &ZVM{
zvmBaseDir: zvm_path,
baseDir: 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{
UseColor: true,
UseColor: true,
VersionMapUrl: "https://ziglang.org/download/index.json",
}

Expand All @@ -57,8 +57,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
Expand Down Expand Up @@ -90,10 +90,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
Expand Down
38 changes: 19 additions & 19 deletions cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cli/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 14 additions & 8 deletions cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"io"
"net/http"
"os"

// "os/user"
"path/filepath"
"runtime"

// "syscall"
"time"
"zvm/cli/meta"
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions cli/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
18 changes: 9 additions & 9 deletions cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package cli

import (
"encoding/json"
"fmt"
// "fmt"
"io"
"net/http"
"os"
"path/filepath"
"zvm/cli/meta"

"github.com/charmbracelet/log"
"github.com/tristanisham/clr"
// "github.com/tristanisham/clr"
)

func (z *ZVM) fetchVersionMap() (zigVersionMap, error) {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
7 changes: 1 addition & 6 deletions help.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
zvm (Zig Version Manager) {{.Version}}
© 2023-present Tristan Isham
© 2022-present Tristan Isham
--------------------------------

install, i [flags] <version>
Expand Down Expand Up @@ -32,10 +32,5 @@ help
------------- Flags -----------------
-color=<bool> | 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.

Looking for more help? https://github.com/tristanisham/zvm

0 comments on commit 797bb58

Please # to comment.