Skip to content

Commit 74b0d69

Browse files
committed
notify: Remove viper interactive checks
The notify helpers accept now *run.CommandOptions and use it to check if we can interact with the user. Modify callers to pass options using cmd/flags.CommandOptions().
1 parent 4f2a8a1 commit 74b0d69

File tree

5 files changed

+25
-24
lines changed

5 files changed

+25
-24
lines changed

cmd/minikube/cmd/config/profile_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var profileListCmd = &cobra.Command{
5757
options := flags.CommandOptions()
5858
output := strings.ToLower(profileOutput)
5959
out.SetJSON(output == "json")
60-
go notify.MaybePrintUpdateTextFromGithub()
60+
go notify.MaybePrintUpdateTextFromGithub(options)
6161

6262
switch output {
6363
case "json":

cmd/minikube/cmd/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func runStart(cmd *cobra.Command, _ []string) {
170170
go download.CleanUpOlderPreloads()
171171

172172
// Avoid blocking execution on optional HTTP fetches
173-
go notify.MaybePrintUpdateTextFromGithub()
173+
go notify.MaybePrintUpdateTextFromGithub(options)
174174

175175
displayEnviron(os.Environ())
176176
if viper.GetBool(force) {

cmd/minikube/cmd/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ var statusCmd = &cobra.Command{
9292

9393
options := flags.CommandOptions()
9494
out.SetJSON(output == "json")
95-
go notify.MaybePrintUpdateTextFromGithub()
95+
go notify.MaybePrintUpdateTextFromGithub(options)
9696

9797
cname := ClusterFlagValue()
9898
api, cc := mustload.Partial(cname, options)

pkg/minikube/notify/notify.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"k8s.io/minikube/pkg/minikube/detect"
3434
"k8s.io/minikube/pkg/minikube/localpath"
3535
"k8s.io/minikube/pkg/minikube/out"
36+
"k8s.io/minikube/pkg/minikube/run"
3637
"k8s.io/minikube/pkg/minikube/style"
3738
"k8s.io/minikube/pkg/util/lock"
3839
"k8s.io/minikube/pkg/version"
@@ -44,30 +45,30 @@ var (
4445
)
4546

4647
// MaybePrintUpdateTextFromGithub prints update text if needed, from github
47-
func MaybePrintUpdateTextFromGithub() {
48-
maybePrintUpdateText(GithubMinikubeReleasesURL, GithubMinikubeBetaReleasesURL, lastUpdateCheckFilePath)
48+
func MaybePrintUpdateTextFromGithub(options *run.CommandOptions) {
49+
maybePrintUpdateText(GithubMinikubeReleasesURL, GithubMinikubeBetaReleasesURL, lastUpdateCheckFilePath, options)
4950
}
5051

5152
// MaybePrintUpdateTextFromAliyunMirror prints update text if needed, from Aliyun mirror
52-
func MaybePrintUpdateTextFromAliyunMirror() {
53-
maybePrintUpdateText(GithubMinikubeReleasesAliyunURL, GithubMinikubeBetaReleasesAliyunURL, lastUpdateCheckFilePath)
53+
func MaybePrintUpdateTextFromAliyunMirror(options *run.CommandOptions) {
54+
maybePrintUpdateText(GithubMinikubeReleasesAliyunURL, GithubMinikubeBetaReleasesAliyunURL, lastUpdateCheckFilePath, options)
5455
}
5556

56-
func maybePrintUpdateText(latestReleasesURL string, betaReleasesURL string, lastUpdatePath string) {
57+
func maybePrintUpdateText(latestReleasesURL string, betaReleasesURL string, lastUpdatePath string, options *run.CommandOptions) {
5758
latestVersion, err := latestVersionFromURL(latestReleasesURL)
5859
if err != nil {
5960
klog.Warning(err)
6061
return
6162
}
62-
if !shouldCheckURLVersion(lastUpdatePath) {
63+
if !shouldCheckURLVersion(lastUpdatePath, options) {
6364
return
6465
}
6566
localVersion, err := version.GetSemverVersion()
6667
if err != nil {
6768
klog.Warning(err)
6869
return
6970
}
70-
if maybePrintBetaUpdateText(betaReleasesURL, localVersion, latestVersion, lastUpdatePath) {
71+
if maybePrintBetaUpdateText(betaReleasesURL, localVersion, latestVersion, lastUpdatePath, options) {
7172
return
7273
}
7374
if localVersion.Compare(latestVersion) >= 0 {
@@ -77,8 +78,8 @@ func maybePrintUpdateText(latestReleasesURL string, betaReleasesURL string, last
7778
}
7879

7980
// maybePrintBetaUpdateText returns true if update text is printed
80-
func maybePrintBetaUpdateText(betaReleasesURL string, localVersion semver.Version, latestFullVersion semver.Version, lastUpdatePath string) bool {
81-
if !shouldCheckURLBetaVersion(lastUpdatePath) {
81+
func maybePrintBetaUpdateText(betaReleasesURL string, localVersion semver.Version, latestFullVersion semver.Version, lastUpdatePath string, options *run.CommandOptions) bool {
82+
if !shouldCheckURLBetaVersion(lastUpdatePath, options) {
8283
return false
8384
}
8485
latestBetaVersion, err := latestVersionFromURL(betaReleasesURL)
@@ -115,11 +116,11 @@ func printBetaUpdateText(ver semver.Version) {
115116
out.Styled(style.Tip, "To disable update notices in general, run: 'minikube config set WantUpdateNotification false'\n")
116117
}
117118

118-
func shouldCheckURLVersion(filePath string) bool {
119+
func shouldCheckURLVersion(filePath string, options *run.CommandOptions) bool {
119120
if !viper.GetBool(config.WantUpdateNotification) {
120121
return false
121122
}
122-
if !viper.GetBool("interactive") {
123+
if options.NonInteractive {
123124
return false
124125
}
125126
if out.JSON {
@@ -129,12 +130,12 @@ func shouldCheckURLVersion(filePath string) bool {
129130
return time.Since(lastUpdateTime).Hours() >= viper.GetFloat64(config.ReminderWaitPeriodInHours)
130131
}
131132

132-
func shouldCheckURLBetaVersion(filePath string) bool {
133+
func shouldCheckURLBetaVersion(filePath string, options *run.CommandOptions) bool {
133134
if !viper.GetBool(config.WantBetaUpdateNotification) {
134135
return false
135136
}
136137

137-
return shouldCheckURLVersion(filePath)
138+
return shouldCheckURLVersion(filePath, options)
138139
}
139140

140141
type operatingSystems struct {

pkg/minikube/notify/notify_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ import (
3131
"github.com/spf13/viper"
3232
"k8s.io/minikube/pkg/minikube/config"
3333
"k8s.io/minikube/pkg/minikube/out"
34+
"k8s.io/minikube/pkg/minikube/run"
3435
"k8s.io/minikube/pkg/minikube/tests"
3536
"k8s.io/minikube/pkg/version"
3637
)
3738

3839
func TestShouldCheckURLVersion(t *testing.T) {
39-
viper.Set("interactive", true)
4040
tempDir := tests.MakeTempDir(t)
4141

4242
lastUpdateCheckFilePath := filepath.Join(tempDir, "last_update_check")
4343

4444
// test that if users disable update notification in config, the URL version does not get checked
4545
viper.Set(config.WantUpdateNotification, false)
46-
if shouldCheckURLVersion(lastUpdateCheckFilePath) {
46+
if shouldCheckURLVersion(lastUpdateCheckFilePath, &run.CommandOptions{}) {
4747
t.Fatalf("shouldCheckURLVersion returned true even though config had WantUpdateNotification: false")
4848
}
4949

5050
// test that if users want update notification, the URL version does get checked
5151
viper.Set(config.WantUpdateNotification, true)
52-
if !shouldCheckURLVersion(lastUpdateCheckFilePath) {
52+
if !shouldCheckURLVersion(lastUpdateCheckFilePath, &run.CommandOptions{}) {
5353
t.Fatalf("shouldCheckURLVersion returned false even though there was no last_update_check file")
5454
}
5555

@@ -60,15 +60,15 @@ func TestShouldCheckURLVersion(t *testing.T) {
6060
if err := writeTimeToFile(lastUpdateCheckFilePath, time.Time{}); err != nil {
6161
t.Errorf("write failed: %v", err)
6262
}
63-
if !shouldCheckURLVersion(lastUpdateCheckFilePath) {
63+
if !shouldCheckURLVersion(lastUpdateCheckFilePath, &run.CommandOptions{}) {
6464
t.Fatalf("shouldCheckURLVersion returned false even though longer than 24 hours since last update")
6565
}
6666

6767
// test that update notifications do not get triggered if it has been less than 24 hours
6868
if err := writeTimeToFile(lastUpdateCheckFilePath, time.Now().UTC()); err != nil {
6969
t.Errorf("write failed: %v", err)
7070
}
71-
if shouldCheckURLVersion(lastUpdateCheckFilePath) {
71+
if shouldCheckURLVersion(lastUpdateCheckFilePath, &run.CommandOptions{}) {
7272
t.Fatalf("shouldCheckURLVersion returned true even though less than 24 hours since last update")
7373
}
7474

@@ -83,13 +83,13 @@ func TestShouldCheckURLBetaVersion(t *testing.T) {
8383

8484
// test if the user disables beta update notification in config, the URL version does not get checked
8585
viper.Set(config.WantBetaUpdateNotification, false)
86-
if shouldCheckURLBetaVersion(lastUpdateCheckFilePath) {
86+
if shouldCheckURLBetaVersion(lastUpdateCheckFilePath, &run.CommandOptions{}) {
8787
t.Fatalf("shouldCheckURLBetaVersion returned true even though config had WantBetaUpdateNotification: false")
8888
}
8989

9090
// test if the user enables beta update notification in config, the URL version does get checked
9191
viper.Set(config.WantBetaUpdateNotification, true)
92-
if !shouldCheckURLBetaVersion(lastUpdateCheckFilePath) {
92+
if !shouldCheckURLBetaVersion(lastUpdateCheckFilePath, &run.CommandOptions{}) {
9393
t.Fatalf("shouldCheckURLBetaVersion returned false even though config had WantBetaUpdateNotification: true")
9494
}
9595
}
@@ -227,7 +227,7 @@ func TestMaybePrintUpdateText(t *testing.T) {
227227
}
228228
defer os.Remove(tmpfile.Name())
229229

230-
maybePrintUpdateText(tt.latestFullVersionFromURL, tt.latestBetaVersionFromURL, tmpfile.Name())
230+
maybePrintUpdateText(tt.latestFullVersionFromURL, tt.latestBetaVersionFromURL, tmpfile.Name(), &run.CommandOptions{})
231231
got := outputBuffer.String()
232232
if (tt.want == "" && len(got) != 0) || (tt.want != "" && !strings.Contains(got, tt.want)) {
233233
t.Fatalf("Expected MaybePrintUpdateText to contain the text %q as the current version is %s and full version %s and beta version %s, but output was [%s]",

0 commit comments

Comments
 (0)