Skip to content

Commit

Permalink
Changed 'check-update' to 'version'
Browse files Browse the repository at this point in the history
  • Loading branch information
DaltonSW committed Aug 16, 2024
1 parent f618b37 commit dfe5773
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/aocli/aocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
Expand Down
22 changes: 16 additions & 6 deletions cmd/aocli/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit dfe5773

Please sign in to comment.