feat: notify when a new version is available

This commit is contained in:
Mathis Maquenne
2026-07-30 11:30:08 +02:00
parent b734ad802a
commit 544fd09199
3 changed files with 68 additions and 3 deletions
+1
View File
@@ -21,6 +21,7 @@ var opts downloader.Options
var downloadCmd = &cobra.Command{ var downloadCmd = &cobra.Command{
Use: "download", Use: "download",
Short: "Download songs from Deezer", Short: "Download songs from Deezer",
Annotations: map[string]string{updateNoticeAnnotation: "true"},
} }
func init() { func init() {
+64
View File
@@ -1,11 +1,75 @@
package cmd package cmd
import ( import (
"context"
"fmt"
"os"
"slices"
"github.com/mathismqn/godeez/internal/buildinfo"
"github.com/mathismqn/godeez/internal/updater"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/term"
) )
const updateNoticeAnnotation = "godeez:update-notice"
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "godeez", Use: "godeez",
Short: "GoDeez is a tool to download music from Deezer", Short: "GoDeez is a tool to download music from Deezer",
SilenceUsage: true, SilenceUsage: true,
} }
func Execute(ctx context.Context) error {
var notice <-chan string
if wantsUpdateNotice() {
notice = updater.StartCheck(ctx)
}
err := RootCmd.ExecuteContext(ctx)
printUpdateNotice(notice)
return err
}
func wantsUpdateNotice() bool {
if !term.IsTerminal(int(os.Stderr.Fd())) {
return false
}
args := os.Args[1:]
if slices.Contains(args, "-h") || slices.Contains(args, "--help") {
return false
}
target, _, err := RootCmd.Find(args)
if err != nil || target == nil {
return false
}
if target.Run == nil && target.RunE == nil {
return false
}
for cmd := target; cmd != nil; cmd = cmd.Parent() {
if cmd.Annotations[updateNoticeAnnotation] == "true" {
return true
}
}
return false
}
func printUpdateNotice(notice <-chan string) {
select {
case latest := <-notice:
if latest == "" {
return
}
fmt.Fprintf(os.Stderr, "\n ┌ Update available: %s → %s\n └ Run `godeez update` to install\n",
buildinfo.Version(), latest)
default:
}
}
+1 -1
View File
@@ -12,7 +12,7 @@ func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop() defer stop()
if err := cmd.RootCmd.ExecuteContext(ctx); err != nil { if err := cmd.Execute(ctx); err != nil {
stop() stop()
os.Exit(1) os.Exit(1)
} }