refactor: build cobra commands with constructors

This commit is contained in:
Mathis Maquenne
2026-08-05 11:35:52 +02:00
parent 0ab25addd5
commit 3c9da5f24a
6 changed files with 121 additions and 111 deletions
+24 -10
View File
@@ -14,26 +14,40 @@ import (
const updateNoticeAnnotation = "godeez:update-notice"
var RootCmd = &cobra.Command{
Use: "godeez",
Short: "GoDeez is a tool to download music from Deezer",
SilenceUsage: true,
}
func Execute(ctx context.Context) error {
root := NewRootCmd()
var notice <-chan string
if wantsUpdateNotice() {
if wantsUpdateNotice(root) {
notice = updater.StartCheck(ctx)
}
err := RootCmd.ExecuteContext(ctx)
err := root.ExecuteContext(ctx)
printUpdateNotice(notice)
return err
}
func wantsUpdateNotice() bool {
func NewRootCmd() *cobra.Command {
root := &cobra.Command{
Use: "godeez",
Short: "GoDeez is a tool to download music from Deezer",
SilenceUsage: true,
}
root.AddCommand(
newDownloadCmd(),
newLoginCmd(),
newLogoutCmd(),
newUpdateCmd(),
newVersionCmd(),
)
return root
}
func wantsUpdateNotice(root *cobra.Command) bool {
if !term.IsTerminal(int(os.Stderr.Fd())) {
return false
}
@@ -43,7 +57,7 @@ func wantsUpdateNotice() bool {
return false
}
target, _, err := RootCmd.Find(args)
target, _, err := root.Find(args)
if err != nil || target == nil {
return false
}