Skip to content

Commit 57751bb

Browse files
authored
CLOUDP-100613: When no profile file exists the version checker fails (#865)
1 parent 7b62157 commit 57751bb

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

internal/cli/root/builder.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ import (
3434
"github.com/mongodb/mongocli/internal/config"
3535
"github.com/mongodb/mongocli/internal/flag"
3636
"github.com/mongodb/mongocli/internal/search"
37-
"github.com/mongodb/mongocli/internal/store"
3837
"github.com/mongodb/mongocli/internal/usage"
3938
"github.com/mongodb/mongocli/internal/version"
4039
"github.com/spf13/cobra"
4140
)
4241

4342
type BuilderOpts struct {
44-
store store.ReleaseVersionDescriber
43+
store version.ReleaseVersionDescriber
4544
}
4645

4746
// rootBuilder conditionally adds children commands as needed.
@@ -60,17 +59,15 @@ func Builder(profile *string, argsWithoutProg []string) *cobra.Command {
6059
Annotations: map[string]string{
6160
"toc": "true",
6261
},
63-
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
62+
PersistentPostRun: func(cmd *cobra.Command, args []string) {
6463
w := cmd.ErrOrStderr()
6564
if shouldSkipPrintNewVersion(w) {
66-
return nil
65+
return
6766
}
68-
opts := &BuilderOpts{}
69-
err := opts.initStore()
70-
if err != nil {
71-
return err
67+
opts := &BuilderOpts{
68+
store: version.NewReleaseVersionDescriber(),
7269
}
73-
return opts.printNewVersionAvailable(w)
70+
_ = opts.printNewVersionAvailable(w)
7471
},
7572
}
7673
rootCmd.SetVersionTemplate(formattedVersion())
@@ -233,9 +230,3 @@ To disable this alert, run "mongocli config set skip_update_check true".
233230
}
234231
return nil
235232
}
236-
237-
func (opts *BuilderOpts) initStore() error {
238-
var err error
239-
opts.store, err = store.New(store.UnauthenticatedPreset(config.Default()))
240-
return err
241-
}

internal/cli/root/builder_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"github.com/golang/mock/gomock"
2727
"github.com/mongodb/mongocli/internal/mocks"
28-
"github.com/mongodb/mongocli/internal/store"
2928
"github.com/mongodb/mongocli/internal/version"
3029
)
3130

@@ -126,22 +125,22 @@ func TestBuilder(t *testing.T) {
126125
func TestOutputOpts_printNewVersionAvailable(t *testing.T) {
127126
tests := []struct {
128127
currentVersion string
129-
latestVersion *store.ReleaseInformation
128+
latestVersion *version.ReleaseInformation
130129
wantPrint bool
131130
}{
132131
{
133132
currentVersion: "v1.0.0",
134-
latestVersion: &store.ReleaseInformation{Version: "v2.0.0", PublishedAt: time.Now()},
133+
latestVersion: &version.ReleaseInformation{Version: "v2.0.0", PublishedAt: time.Now()},
135134
wantPrint: true,
136135
},
137136
{
138137
currentVersion: "v1.0.0",
139-
latestVersion: &store.ReleaseInformation{Version: "v1.0.0", PublishedAt: time.Now()},
138+
latestVersion: &version.ReleaseInformation{Version: "v1.0.0", PublishedAt: time.Now()},
140139
wantPrint: false,
141140
},
142141
{
143142
currentVersion: "v1.0.0-123",
144-
latestVersion: &store.ReleaseInformation{Version: "v1.0.0", PublishedAt: time.Now()},
143+
latestVersion: &version.ReleaseInformation{Version: "v1.0.0", PublishedAt: time.Now()},
145144
wantPrint: false,
146145
},
147146
}

internal/mocks/mock_release_version.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/store/release_version.go renamed to internal/version/release_version.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package store
15+
package version
1616

1717
import (
18+
"context"
1819
"time"
1920

2021
"github.com/google/go-github/v38/github"
2122
)
2223

23-
//go:generate mockgen -destination=../mocks/mock_release_version.go -package=mocks github.com/mongodb/mongocli/internal/store ReleaseVersionDescriber
24+
//go:generate mockgen -destination=../mocks/mock_release_version.go -package=mocks github.com/mongodb/mongocli/internal/version ReleaseVersionDescriber
2425

2526
type ReleaseVersionDescriber interface {
2627
LatestVersion() (*ReleaseInformation, error)
@@ -31,8 +32,16 @@ type ReleaseInformation struct {
3132
PublishedAt time.Time
3233
}
3334

35+
func NewReleaseVersionDescriber() ReleaseVersionDescriber {
36+
return &releaseVersionFetcher{ctx: context.Background()}
37+
}
38+
39+
type releaseVersionFetcher struct {
40+
ctx context.Context
41+
}
42+
3443
// LatestVersion encapsulates the logic to manage different cloud providers.
35-
func (s *Store) LatestVersion() (*ReleaseInformation, error) {
44+
func (s *releaseVersionFetcher) LatestVersion() (*ReleaseInformation, error) {
3645
client := github.NewClient(nil)
3746
release, _, err := client.Repositories.GetLatestRelease(s.ctx, "mongodb", "mongocli")
3847
if err != nil {

0 commit comments

Comments
 (0)