diff --git a/cli/meta/version.go b/cli/meta/version.go index 43eb1c1..bb019ea 100644 --- a/cli/meta/version.go +++ b/cli/meta/version.go @@ -9,7 +9,9 @@ import ( ) const ( - VERSION = "v0.6.3" + VERSION = "v0.6.4" + // VERSION = "v0.0.0" // For testing zvm upgrade + ) diff --git a/cli/upgrade.go b/cli/upgrade.go index e1cb4a6..b2ba5a8 100644 --- a/cli/upgrade.go +++ b/cli/upgrade.go @@ -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 { @@ -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")