Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix a bug that update-aqua fails on Windows #2871

Merged
merged 2 commits into from
May 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion pkg/installpackage/aqua.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/aquaproj/aqua/v2/pkg/config"
"github.com/aquaproj/aqua/v2/pkg/config/aqua"
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/osfile"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

func (is *Installer) InstallAqua(ctx context.Context, logE *logrus.Entry, version string) error { //nolint:funlen
Expand Down Expand Up @@ -133,7 +135,7 @@ func (is *Installer) InstallAqua(ctx context.Context, logE *logrus.Entry, versio
}

if is.runtime.GOOS == "windows" {
return is.Copy(filepath.Join(is.rootDir, "bin", "aqua.exe"), exePath)
return is.copyAquaOnWindows(exePath)
}

// create a symbolic link
Expand All @@ -144,3 +146,23 @@ func (is *Installer) InstallAqua(ctx context.Context, logE *logrus.Entry, versio

return is.createLink(filepath.Join(is.rootDir, "bin", "aqua"), a, logE)
}

func (is *Installer) copyAquaOnWindows(exePath string) error {
// https://github.com/orgs/aquaproj/discussions/2510
// https://stackoverflow.com/questions/1211948/best-method-for-implementing-self-updating-software
dest := filepath.Join(is.rootDir, "bin", "aqua.exe")
if f, err := afero.Exists(is.fs, dest); err != nil {
return fmt.Errorf("check if aqua.exe exists: %w", err)
} else if f {
// afero.Tempfile can't be used
// > The system cannot move the file to a different disk drive
tempDir := filepath.Join(is.rootDir, "temp")
if err := osfile.MkdirAll(is.fs, tempDir); err != nil {
return fmt.Errorf("create a temporal directory: %w", err)
}
if err := is.fs.Rename(dest, filepath.Join(tempDir, "aqua.exe")); err != nil {
return fmt.Errorf("rename aqua.exe to update: %w", err)
}
}
return is.Copy(dest, exePath)
}
Loading