mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Fix update executable path
This commit is contained in:
parent
2f08aeac1a
commit
d4bef511cb
1 changed files with 24 additions and 8 deletions
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
@ -90,8 +91,8 @@ func update() error {
|
||||||
var downloadFile string
|
var downloadFile string
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "linux":
|
case "linux":
|
||||||
yayCommand := exec.Command("pacman -Q crunchy-cli")
|
pacmanCommand := exec.Command("pacman -Q crunchy-cli")
|
||||||
if yayCommand.Run() == nil && yayCommand.ProcessState.Success() {
|
if pacmanCommand.Run() == nil && pacmanCommand.ProcessState.Success() {
|
||||||
utils.Log.Info("crunchy-cli was probably installed via an Arch Linux AUR helper (like yay). Updating via this AUR helper is recommended")
|
utils.Log.Info("crunchy-cli was probably installed via an Arch Linux AUR helper (like yay). Updating via this AUR helper is recommended")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -105,14 +106,29 @@ func update() error {
|
||||||
"You have to update manually (https://github.com/crunchy-labs/crunchy-cli", runtime.GOOS)
|
"You have to update manually (https://github.com/crunchy-labs/crunchy-cli", runtime.GOOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.Log.SetProcess("Updating executable %s", os.Args[0])
|
executePath := os.Args[0]
|
||||||
|
var perms os.FileInfo
|
||||||
perms, err := os.Stat(os.Args[0])
|
// check if the path is relative, absolute or non (if so, the executable must be in PATH)
|
||||||
|
if strings.HasPrefix(executePath, "."+string(os.PathSeparator)) || path.IsAbs(executePath) {
|
||||||
|
if perms, err = os.Stat(os.Args[0]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
executePath, err = exec.LookPath(os.Args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
os.Remove(os.Args[0])
|
if perms, err = os.Stat(executePath); err != nil {
|
||||||
executeFile, err := os.OpenFile(os.Args[0], os.O_CREATE|os.O_WRONLY, perms.Mode())
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.Log.SetProcess("Updating executable %s", executePath)
|
||||||
|
|
||||||
|
if err = os.Remove(executePath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
executeFile, err := os.OpenFile(executePath, os.O_CREATE|os.O_WRONLY, perms.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -128,7 +144,7 @@ func update() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.Log.StopProcess("Updated executable %s", os.Args[0])
|
utils.Log.StopProcess("Updated executable %s", executePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue