From dfe5773a424e1d8c71538b4b855ccdfa0a26edaf Mon Sep 17 00:00:00 2001 From: Dalton Williams Date: Thu, 15 Aug 2024 19:41:16 -0500 Subject: [PATCH] Changed 'check-update' to 'version' --- cmd/aocli/aocli.go | 4 ++-- cmd/aocli/updater.go | 22 ++++++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/cmd/aocli/aocli.go b/cmd/aocli/aocli.go index ecfc2f3..263258f 100644 --- a/cmd/aocli/aocli.go +++ b/cmd/aocli/aocli.go @@ -96,8 +96,8 @@ func main() { // User dependent functions. I recognize that I used if/else above and switch statement here, oh well switch args[1] { - case "check-update": - update := CheckForUpdate() + case "version": + update := Version() if update { fmt.Printf("New version available! Run `aocli update` to get the new version (or `sudo aocli update` if your executable is in a protected location)") } diff --git a/cmd/aocli/updater.go b/cmd/aocli/updater.go index 4747e7d..34d97c0 100644 --- a/cmd/aocli/updater.go +++ b/cmd/aocli/updater.go @@ -15,7 +15,7 @@ import ( ) // Internally tracked version to compare against GitHub releases -const currentVersion = "v0.9.4" +const currentVersion = "v0.9.5" const repoURL = "https://api.github.com/repos/DaltonSW/aocGo/releases/latest" type githubRelease struct { @@ -26,9 +26,13 @@ type githubRelease struct { } `json:"assets"` } -// CheckForUpdate will get the latest release and compare the program versions. -// Associated command: `check-update` -func CheckForUpdate() bool { +// Version will print the current version of the program. +// It will also check for any updates available. +// Associated command: `version` +func Version() bool { + // TODO: Reorder the printing of this to make it "current version" focused at first + + // TODO: Some styling too latestVersion, err := getLatestRelease() if err != nil { log.Fatal("Error checking for updates!", "error", err) @@ -101,7 +105,7 @@ func Update() { } defer resp.Body.Close() - logger.Info("Successfully downloaded") + logger.Info("Request successful") curExec, err := os.Executable() if err != nil { @@ -114,7 +118,7 @@ func Update() { } defer os.Remove(tmpFile.Name()) - logger.Info("Writing downloaded content to temp file") + logger.Info("Downloading content to temp file") // Write the downloaded content to the temporary file if _, err := io.Copy(tmpFile, resp.Body); err != nil { @@ -128,6 +132,12 @@ func Update() { return } + // Make the temp file executable + if err := os.Chmod(tmpFile.Name(), 0700); err != nil { + logger.Fatal("Error changing mode to allow execution: ", err) + return + } + // Replace the current executable with the new one if err := os.Rename(tmpFile.Name(), curExec); err != nil { logger.Fatal("Error replacing the executable, maybe try sudo: ", err)