Skip to content

Commit

Permalink
Fixed self-upgrade on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanisham committed Apr 11, 2024
1 parent 7f315c3 commit c5843e4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cli/meta/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
)

const (
VERSION = "v0.6.3"
VERSION = "v0.6.4"
// VERSION = "v0.0.0" // For testing zvm upgrade

)


Expand Down
49 changes: 41 additions & 8 deletions cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ func (z *ZVM) Upgrade() error {
return err
}

secondaryZVM := fmt.Sprintf("%s2", zvmPath)
secondaryZVM := fmt.Sprintf("%s.old", zvmPath)
log.Debug("SecondaryZVM", "path", secondaryZVM)

newDownload := filepath.Join(newTemp, fmt.Sprintf("zvm-%s-%s", runtime.GOOS, runtime.GOARCH), zvmBinaryName)
if err := os.Rename(newDownload, secondaryZVM); err != nil {
log.Debugf("Failed to rename %s to %s", filepath.Join(newTemp, fmt.Sprintf("zvm-%s-%s", runtime.GOOS, runtime.GOARCH), zvmBinaryName), secondaryZVM)
return errors.Join(ErrFailedUpgrade, err)
}

fmt.Println("Run the following to complete your upgrade on Windows.")
fmt.Printf("- Command Prompt:\n\tmove /Y '%s' '%s'\n", secondaryZVM, zvmPath)
fmt.Printf("- Powershell:\n\tMove-Item -Path '%s' -Destination '%s' -Force\n", secondaryZVM, zvmPath)
if err := replaceExe(newDownload, zvmPath); err != nil {
log.Warn("This command might break if ZVM is installed outside of ~/.zvm/self/")
return fmt.Errorf("upgrade error: %q", err)
}
// fmt.Println("Run the following to complete your upgrade on Windows.")
// fmt.Printf("- Command Prompt:\n\tmove /Y '%s' '%s'\n", secondaryZVM, zvmPath)
// fmt.Printf("- Powershell:\n\tMove-Item -Path '%s' -Destination '%s' -Force\n", secondaryZVM, zvmPath)

} else {
if err := untar(tempDownload.Name(), newTemp); err != nil {
Expand All @@ -152,6 +152,39 @@ func (z *ZVM) Upgrade() error {
return nil
}

func replaceExe(from, to string) error {
if runtime.GOOS == "windows" {
if err := os.Rename(to, fmt.Sprintf("%s.old", to)); err != nil {
return err
}
} else {
if err := os.Remove(to); err != nil {
return err
}
}

if err := os.Rename(from, to); err != nil {
from_io, err := os.Open(from)
if err != nil {
return err
}
defer from_io.Close()

to_io, err := os.Create(to)
if err != nil {
return err
}
defer to_io.Close()


if _, err := io.Copy(from_io, to_io); err != nil {
return nil
}
}

return nil
}

// getInstallDir finds the directory this executabile is in.
func (z ZVM) getInstallDir() (string, error) {
zvmInstallDirENV, ok := os.LookupEnv("ZVM_INSTALL")
Expand Down

0 comments on commit c5843e4

Please sign in to comment.