Skip to content

Commit

Permalink
Use existing executable dir for upgrade (#46)
Browse files Browse the repository at this point in the history
Signed-off-by: Prasad Ghangal <[email protected]>
  • Loading branch information
PrasadG193 authored Apr 4, 2021
1 parent 923ad2e commit b71d220
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/update/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/kbrew-dev/kbrew/pkg/version"

Expand All @@ -17,6 +18,14 @@ const (
upgradeCmd = "curl -sfL https://raw.githubusercontent.com/kbrew-dev/kbrew-release/main/install.sh | sh"
)

func getBinDir() (string, error) {
path, err := os.Executable()
if err != nil {
return "", err
}
return filepath.Dir(path), nil
}

func CheckRelease(ctx context.Context) error {
client := github.NewClient(nil)
release, _, err := client.Repositories.GetLatestRelease(ctx, releaseRepoOwner, releaseRepoName)
Expand All @@ -29,13 +38,20 @@ func CheckRelease(ctx context.Context) error {
}
// Send notification if newer version available
if version.VERSION != *release.TagName {
fmt.Printf("kbrew %s is available, upgrading...\n", version.VERSION)
fmt.Printf("kbrew %s is available, upgrading...\n", *release.TagName)
return upgradeKbrew(ctx)
}
return nil
}

func upgradeKbrew(ctx context.Context) error {
dir, err := getBinDir()
if err != nil {
fmt.Println("Failed to get executable dir.")
return err
}
os.Setenv("BINDIR", dir)
defer os.Unsetenv("BINDIR")
return execCommand(ctx, upgradeCmd)
}

Expand Down

0 comments on commit b71d220

Please sign in to comment.