@@ -16,6 +16,10 @@ package mongocli
16
16
17
17
import (
18
18
"fmt"
19
+ "io"
20
+ "runtime"
21
+ "time"
22
+
19
23
"github.com/mongodb/mongocli/internal/cli"
20
24
"github.com/mongodb/mongocli/internal/cli/atlas"
21
25
"github.com/mongodb/mongocli/internal/cli/auth"
@@ -33,11 +37,13 @@ import (
33
37
"github.com/mongodb/mongocli/internal/version"
34
38
"github.com/spf13/afero"
35
39
"github.com/spf13/cobra"
36
- "io"
37
- "runtime"
38
40
)
39
41
40
42
type Notifier struct {
43
+ currentVersion string
44
+ finder latestrelease.VersionFinder
45
+ filesystem afero.Fs
46
+ writer io.Writer
41
47
}
42
48
43
49
// Builder conditionally adds children commands as needed.
@@ -58,18 +64,20 @@ func Builder(profile *string, argsWithoutProg []string) *cobra.Command {
58
64
},
59
65
PersistentPostRun : func (cmd * cobra.Command , args []string ) {
60
66
w := cmd .ErrOrStderr ()
61
- if shouldSkipPrintNewVersion (w ) {
62
- return
67
+ fs := afero .NewOsFs ()
68
+ f , _ := latestrelease .NewVersionFinder (fs , version .NewReleaseVersionDescriber ())
69
+
70
+ notifier := & Notifier {
71
+ currentVersion : latestrelease .VersionFromTag (version .Version , config .ToolName ),
72
+ finder : f ,
73
+ filesystem : fs ,
74
+ writer : w ,
63
75
}
64
- c , _ := homebrew .NewChecker (afero .NewOsFs ())
65
- c .IsHomebrew ()
66
- // p := NewPrinter(w, config.ToolName, config.BinName(), c.IsHomebrew())
67
- checker := latestrelease .NewChecker (version .Version , config .ToolName )
68
- // shouldCheck && isLatests{
69
- // print new vercions
70
- //}
71
- _ = checker .CheckAvailable ()
72
76
77
+ c , _ := homebrew .NewChecker (fs )
78
+ isHb := c .IsHomebrew ()
79
+
80
+ notifier .notifyIfApplicable (isHb )
73
81
},
74
82
}
75
83
rootCmd .SetVersionTemplate (formattedVersion ())
@@ -116,42 +124,6 @@ func Builder(profile *string, argsWithoutProg []string) *cobra.Command {
116
124
return rootCmd
117
125
}
118
126
119
- type Printer interface {
120
- PrintNewVersionAvailable (latestVersion , homebrewCommand string ) error
121
- }
122
-
123
- func NewPrinter (w io.Writer , t , b string ) Printer {
124
- return & printer {
125
- writer : w ,
126
- tool : t ,
127
- bin : b ,
128
- }
129
- }
130
-
131
- type printer struct {
132
- writer io.Writer
133
- tool string
134
- bin string
135
- }
136
-
137
- func (p * printer ) PrintNewVersionAvailable (latestVersion , formulaName string ) error {
138
- var upgradeInstructions string
139
- if formulaName != "" {
140
- upgradeInstructions = fmt .Sprintf (`To upgrade, run "brew update && brew upgrade %s".` , formulaName )
141
- } else {
142
- upgradeInstructions = fmt .Sprintf (`To upgrade, see: https://dochub.mongodb.org/core/%s-install.` , p .tool )
143
- }
144
-
145
- newVersionTemplate := `
146
- A new version of %s is available '%s'!
147
- %s
148
-
149
- To disable this alert, run "%s config set skip_update_check true".
150
- `
151
- _ , err := fmt .Fprintf (p .writer , newVersionTemplate , p .tool , latestVersion , upgradeInstructions , p .bin )
152
- return err
153
- }
154
-
155
127
const verTemplate = `%s version: %s
156
128
git version: %s
157
129
Go version: %s
@@ -171,6 +143,41 @@ func formattedVersion() string {
171
143
runtime .Compiler )
172
144
}
173
145
174
- func shouldSkipPrintNewVersion (w io.Writer ) bool {
175
- return config .SkipUpdateCheck () || ! cli .IsTerminal (w )
146
+ func (c * Notifier ) notifyIfApplicable (isHb bool ) {
147
+ if c .shouldCheck () {
148
+ release , err := c .finder .Find ()
149
+ if err != nil || release == nil {
150
+ return
151
+ }
152
+ _ = c .notify (isHb , release )
153
+ }
154
+ }
155
+ func (c * Notifier ) shouldCheck () bool {
156
+ return ! config .SkipUpdateCheck () && cli .IsTerminal (c .writer )
157
+ }
158
+
159
+ func (c * Notifier ) notify (isHb bool , release * latestrelease.ReleaseInformation ) error {
160
+ if isHb && ! isAtLeast24HoursPast (release .PublishedAt ) {
161
+ return nil
162
+ }
163
+
164
+ var upgradeInstructions string
165
+ if isHb {
166
+ upgradeInstructions = fmt .Sprintf (`To upgrade, run "brew update && brew upgrade %s".` , homebrew .FormulaName (config .ToolName ))
167
+ } else {
168
+ upgradeInstructions = fmt .Sprintf (`To upgrade, see: https://dochub.mongodb.org/core/%s-install.` , config .ToolName )
169
+ }
170
+
171
+ newVersionTemplate := `
172
+ A new version of %s is available '%s'!
173
+ %s
174
+
175
+ To disable this alert, run "%s config set skip_update_check true".
176
+ `
177
+ _ , err := fmt .Fprintf (c .writer , newVersionTemplate , config .ToolName , release .Version , upgradeInstructions , config .BinName ())
178
+ return err
179
+ }
180
+
181
+ func isAtLeast24HoursPast (t time.Time ) bool {
182
+ return ! t .IsZero () && time .Since (t ) >= time .Hour * 24
176
183
}
0 commit comments