Skip to content

Commit 12456ba

Browse files
committed
Refactor
1 parent 83460ba commit 12456ba

File tree

13 files changed

+210
-750
lines changed

13 files changed

+210
-750
lines changed

internal/cli/root/atlas/builder.go

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ package atlas
1616

1717
import (
1818
"fmt"
19+
"io"
1920
"runtime"
2021
"strings"
22+
"time"
2123

2224
"github.com/mongodb/mongocli/internal/cli"
2325
"github.com/mongodb/mongocli/internal/cli/alerts"
@@ -54,15 +56,24 @@ import (
5456
"github.com/mongodb/mongocli/internal/cli/performanceadvisor"
5557
"github.com/mongodb/mongocli/internal/config"
5658
"github.com/mongodb/mongocli/internal/flag"
59+
"github.com/mongodb/mongocli/internal/homebrew"
5760
"github.com/mongodb/mongocli/internal/latestrelease"
5861
"github.com/mongodb/mongocli/internal/usage"
5962
"github.com/mongodb/mongocli/internal/validate"
6063
"github.com/mongodb/mongocli/internal/version"
64+
"github.com/spf13/afero"
6165
"github.com/spf13/cobra"
6266
)
6367

6468
const atlas = "atlas"
6569

70+
type Notifier struct {
71+
currentVersion string
72+
finder latestrelease.VersionFinder
73+
filesystem afero.Fs
74+
writer io.Writer
75+
}
76+
6677
// Builder conditionally adds children commands as needed.
6778
func Builder(profile *string) *cobra.Command {
6879
rootCmd := &cobra.Command{
@@ -103,12 +114,20 @@ func Builder(profile *string) *cobra.Command {
103114
},
104115
PersistentPostRun: func(cmd *cobra.Command, args []string) {
105116
w := cmd.ErrOrStderr()
106-
107-
if !config.SkipUpdateCheck() && cli.IsTerminal(w) {
108-
printer := latestrelease.NewPrinter(w, config.ToolName, config.BinName())
109-
checker := latestrelease.NewChecker(version.Version, config.ToolName, printer)
110-
_ = checker.CheckAvailable()
117+
fs := afero.NewOsFs()
118+
f, _ := latestrelease.NewVersionFinder(fs, version.NewReleaseVersionDescriber())
119+
120+
notifier := &Notifier{
121+
currentVersion: latestrelease.VersionFromTag(version.Version, config.ToolName),
122+
finder: f,
123+
filesystem: fs,
124+
writer: w,
111125
}
126+
127+
c, _ := homebrew.NewChecker(fs)
128+
isHb := c.IsHomebrew()
129+
130+
notifier.notifyIfApplicable(isHb)
112131
},
113132
}
114133
rootCmd.SetVersionTemplate(formattedVersion())
@@ -182,3 +201,42 @@ func formattedVersion() string {
182201
runtime.GOARCH,
183202
runtime.Compiler)
184203
}
204+
205+
func (c *Notifier) notifyIfApplicable(isHb bool) {
206+
if c.shouldCheck() {
207+
release, err := c.finder.Find()
208+
if err != nil || release == nil {
209+
return
210+
}
211+
_ = c.notify(isHb, release)
212+
}
213+
}
214+
func (c *Notifier) shouldCheck() bool {
215+
return !config.SkipUpdateCheck() && cli.IsTerminal(c.writer)
216+
}
217+
218+
func (c *Notifier) notify(isHb bool, release *latestrelease.ReleaseInformation) error {
219+
if isHb && !isAtLeast24HoursPast(release.PublishedAt) {
220+
return nil
221+
}
222+
223+
var upgradeInstructions string
224+
if isHb {
225+
upgradeInstructions = fmt.Sprintf(`To upgrade, run "brew update && brew upgrade %s".`, homebrew.FormulaName(config.ToolName))
226+
} else {
227+
upgradeInstructions = fmt.Sprintf(`To upgrade, see: https://dochub.mongodb.org/core/%s-install.`, config.ToolName)
228+
}
229+
230+
newVersionTemplate := `
231+
A new version of %s is available '%s'!
232+
%s
233+
234+
To disable this alert, run "%s config set skip_update_check true".
235+
`
236+
_, err := fmt.Fprintf(c.writer, newVersionTemplate, config.ToolName, release.Version, upgradeInstructions, config.BinName())
237+
return err
238+
}
239+
240+
func isAtLeast24HoursPast(t time.Time) bool {
241+
return !t.IsZero() && time.Since(t) >= time.Hour*24
242+
}

internal/cli/root/mongocli/builder.go

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ package mongocli
1616

1717
import (
1818
"fmt"
19+
"io"
20+
"runtime"
21+
"time"
22+
1923
"github.com/mongodb/mongocli/internal/cli"
2024
"github.com/mongodb/mongocli/internal/cli/atlas"
2125
"github.com/mongodb/mongocli/internal/cli/auth"
@@ -33,11 +37,13 @@ import (
3337
"github.com/mongodb/mongocli/internal/version"
3438
"github.com/spf13/afero"
3539
"github.com/spf13/cobra"
36-
"io"
37-
"runtime"
3840
)
3941

4042
type Notifier struct {
43+
currentVersion string
44+
finder latestrelease.VersionFinder
45+
filesystem afero.Fs
46+
writer io.Writer
4147
}
4248

4349
// Builder conditionally adds children commands as needed.
@@ -58,18 +64,20 @@ func Builder(profile *string, argsWithoutProg []string) *cobra.Command {
5864
},
5965
PersistentPostRun: func(cmd *cobra.Command, args []string) {
6066
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,
6375
}
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()
7276

77+
c, _ := homebrew.NewChecker(fs)
78+
isHb := c.IsHomebrew()
79+
80+
notifier.notifyIfApplicable(isHb)
7381
},
7482
}
7583
rootCmd.SetVersionTemplate(formattedVersion())
@@ -116,42 +124,6 @@ func Builder(profile *string, argsWithoutProg []string) *cobra.Command {
116124
return rootCmd
117125
}
118126

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-
155127
const verTemplate = `%s version: %s
156128
git version: %s
157129
Go version: %s
@@ -171,6 +143,41 @@ func formattedVersion() string {
171143
runtime.Compiler)
172144
}
173145

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
176183
}

internal/homebrew/homebrew.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import (
2727
"github.com/spf13/afero"
2828
)
2929

30-
//go:generate mockgen -destination=../mocks/mock_brew_store.go -package=mocks github.com/mongodb/mongocli/internal/homebrew PathStore
31-
3230
const (
3331
atlasFormulaName = "mongodb-atlas"
3432
brewFileSubPath = "/brew.yaml"

internal/latestrelease/checker.go

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)