3232)
3333
3434var updateNotice * update.CheckResult
35+ var updateCheckWarning string
36+ var isTerminalFn = term .IsTerminal
37+ var checkForUpdateAutoFn = update .CheckForUpdateAuto
3538
3639var rootCmd = & cobra.Command {
3740 Use : "flashduty" ,
@@ -50,27 +53,41 @@ var rootCmd = &cobra.Command{
5053 return err
5154 }
5255 }
56+ updateNotice = nil
57+ updateCheckWarning = ""
5358 if cmd .CommandPath () == "flashduty update" {
5459 return nil
5560 }
56- if ! term . IsTerminal (int (os .Stderr .Fd ())) {
61+ if ! isTerminalFn (int (os .Stderr .Fd ())) {
5762 return nil
5863 }
59- updateNotice = update .StateHasUpdate (versionStr )
6064 if update .ShouldCheck (versionStr ) {
61- go func () {
62- _ , _ = update .CheckForUpdate (versionStr )
63- }()
65+ result , err := checkForUpdateAutoFn (versionStr )
66+ if err != nil {
67+ if update .IsTimeout (err ) {
68+ updateCheckWarning = "auto update check timeout, please run 'flashduty update --check' manually"
69+ } else {
70+ updateNotice = update .StateHasUpdate (versionStr )
71+ }
72+ return nil
73+ }
74+ if result .UpdateAvailable {
75+ updateNotice = result
76+ }
77+ return nil
6478 }
79+ updateNotice = update .StateHasUpdate (versionStr )
6580 return nil
6681 },
67- PersistentPostRun : func (_ * cobra.Command , _ []string ) {
68- if updateNotice == nil {
69- return
82+ PersistentPostRun : func (cmd * cobra.Command , _ []string ) {
83+ if updateCheckWarning != "" {
84+ _ , _ = fmt .Fprintf (cmd .ErrOrStderr (), "\n %s\n " , updateCheckWarning )
85+ }
86+ if updateNotice != nil {
87+ _ , _ = fmt .Fprintf (cmd .ErrOrStderr (), "\n A new version of flashduty is available: v%s -> %s\n " ,
88+ update .StripV (updateNotice .CurrentVersion ), updateNotice .LatestVersion )
89+ _ , _ = fmt .Fprintf (cmd .ErrOrStderr (), "To update, run: flashduty update\n " )
7090 }
71- _ , _ = fmt .Fprintf (os .Stderr , "\n A new version of flashduty is available: v%s -> %s\n " ,
72- update .StripV (updateNotice .CurrentVersion ), updateNotice .LatestVersion )
73- _ , _ = fmt .Fprintf (os .Stderr , "To update, run: flashduty update\n " )
7491 },
7592}
7693
0 commit comments