Skip to content

Commit

Permalink
silent update notification
Browse files Browse the repository at this point in the history
  • Loading branch information
AshutoshPatole committed Aug 19, 2024
1 parent 7972101 commit 28e84d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
8 changes: 7 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package cmd

import (
"embed"
"fmt"
"os"
"path/filepath"

"github.com/TwiN/go-color"
goversion "github.com/caarlos0/go-version"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -51,6 +53,11 @@ func Execute() {
if err != nil {
os.Exit(1)
}
isUpdateAvailable, _, latestVersion := CheckForUpdates()
if isUpdateAvailable {
fmt.Println(color.InGreen("New Update Available"), color.InBold(color.InGreen(latestVersion)))
return
}
}

//go:embed .env.production
Expand Down Expand Up @@ -86,7 +93,6 @@ func init() {
return
}

CheckForUpdates()
}

// initConfig reads in config file and ENV variables if set.
Expand Down
42 changes: 26 additions & 16 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ var updateCmd = &cobra.Command{
Short: "checks for latest update",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
CheckForUpdates()
updateAvailable, latestRelease, latestVersion := CheckForUpdates()
if updateAvailable {
startUpgrade(latestRelease, latestVersion)
} else {
fmt.Println("You are already using the latest version.")
}
},
}

func init() {
rootCmd.AddCommand(updateCmd)
}

func CheckForUpdates() {
func CheckForUpdates() (bool, *GitHubRelease, *semver.Version) {
currentVersion, err := semver.NewVersion(version)
if err != nil {
logrus.Fatalf("Error parsing current version: %s\n", err)
Expand All @@ -48,18 +53,22 @@ func CheckForUpdates() {
if err != nil {
logrus.Fatalf("Error parsing latest version: %s\n", err)
}
if latestVersion.GreaterThan(currentVersion) {
fmt.Println(asciiArt)
fmt.Printf("Current version: %s\n", currentVersion)
fmt.Printf("New version available: %s\n", latestVersion)
fmt.Println("https://github.com/AshutoshPatole/ssm-v2/releases")

fmt.Print("Do you want to download the update? (y/n): ")
var answer string
fmt.Scanln(&answer)
if strings.ToLower(answer) == "y" {
downloadUpdate(latestRelease)
}

return latestVersion.GreaterThan(currentVersion), latestRelease, latestVersion
}

func startUpgrade(latestRelease *GitHubRelease, latestVersion *semver.Version) {

fmt.Println(asciiArt)
fmt.Printf("Current version: %s\n", version)
fmt.Printf("New version available: %s\n", latestVersion)
fmt.Println("https://github.com/AshutoshPatole/ssm-v2/releases")

fmt.Print("Do you want to download the update? (y/n): ")
var answer string
fmt.Scanln(&answer)
if strings.ToLower(answer) == "y" {
downloadUpdate(latestRelease)
}
}

Expand Down Expand Up @@ -162,7 +171,7 @@ func upgrade(downloadURL string, assetName string) {
if err := handleWindowsUpdate(archivePath); err != nil {
logrus.Fatalf("Error updating on Windows: %s", err)
}
return
os.Exit(0)
}

if runtime.GOOS == "linux" {
Expand All @@ -174,9 +183,10 @@ func upgrade(downloadURL string, assetName string) {
if err := installBinary(binaryPath); err != nil {
logrus.Fatalf("Error installing update: %s", err)
}
fmt.Println("Update successfully installed to /usr/local/bin")
os.Exit(0)
}

fmt.Println("Update successfully installed to /usr/local/bin")
}

func downloadFile(url, filepath string) error {
Expand Down

0 comments on commit 28e84d3

Please sign in to comment.