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

[CLI] Add ability to self update binary #247

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add prompt to auto update the binary
jhnstn committed Mar 8, 2024
commit 3cfc253de4e31c83bd30c0ea3182d31e5fb69bba
13 changes: 2 additions & 11 deletions gbm-cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import (
"github.com/spf13/cobra"
"github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/cmd/release"
"github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/cmd/render"
"github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/cmd/utils"
"github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/console"
"github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/gh"
)

const Version = "v1.5.0"
@@ -20,20 +20,11 @@ var rootCmd = &cobra.Command{
func Execute() {
err := rootCmd.Execute()
console.ExitIfError(err)

}

func init() {
// Add the render command
rootCmd.AddCommand(render.RenderCmd)
rootCmd.AddCommand(release.ReleaseCmd)

// Check to see if the user is running the latest version
// of the CLI. If not, let them know.
latestRelease, err := gh.GetLatestRelease("release-toolkit-gutenberg-mobile")
console.ExitIfError(err)

if latestRelease.TagName != Version {
console.Warn("You are running an older version of the CLI. Please update to %s", latestRelease.TagName)
}
utils.CheckExeVersion(Version)
}
76 changes: 76 additions & 0 deletions gbm-cli/cmd/utils/utils.go
Original file line number Diff line number Diff line change
@@ -2,9 +2,15 @@ package utils

import (
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/inconshreveable/go-update"
"github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/console"
"github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/gh"
"github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/semver"
)

@@ -35,3 +41,73 @@ func Exit(code int, deferred ...func()) {
return code
}())
}

// Checks if running from a temp directory (go build)
// Useful for checking if running via `go run main.go`
// We ignore errors since this only relevant to local development
func CheckIfTempRun() bool {
ex, _ := os.Executable()
dir := filepath.Dir(ex)
return strings.Contains(dir, "go-build")
}

// Updates the currently running executable.
func UpdateExe(url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if err := update.Apply(resp.Body, update.Options{}); err != nil {
return err
}

return nil
}

// Gets the download url for the gbm-cli executable
func exeDownloadUrl(release gh.Release) string {
for _, asset := range release.Assets {
if strings.Contains(asset.Name, "gbm-cli") {
return asset.DownloadUrl
}
}
return ""
}

// Checks if the currently running executable is the latest version
// If not, prompts the user to update.
// If update is confirmed, the executable is updated and the process is restarted
func CheckExeVersion(version string) {
latestRelease, err := gh.GetLatestRelease("release-toolkit-gutenberg-mobile")
console.ExitIfError(err)

if latestRelease.TagName != version {
if console.Confirm("You are running an older version of the CLI. Would you like to update?") {

if url := exeDownloadUrl(latestRelease); url != "" {
if err := UpdateExe(url); err != nil {
console.ExitError("Could not update the CLI: %v", err)
} else {
console.Info("CLI updated successfully")
reStart()
}
} else {
console.ExitError("Could not find download url for latest release")
}
}
}
}

// Restarts the process
func reStart() {
args := os.Args
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
os.Exit(1)
}
os.Exit(0)
}