Skip to content

Commit

Permalink
Merge pull request GoogleContainerTools#3012 from tejal29/add_disable…
Browse files Browse the repository at this point in the history
…_config

Add display prompt config
  • Loading branch information
balopat authored Jan 31, 2020
2 parents f2d337d + fff7154 commit 1ca7b66
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/skaffold/config/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ type GlobalConfig struct {
// ContextConfig is the context-specific config information provided in
// the global Skaffold config.
type ContextConfig struct {
Kubecontext string `yaml:"kube-context,omitempty"`
DefaultRepo string `yaml:"default-repo,omitempty"`
LocalCluster *bool `yaml:"local-cluster,omitempty"`
InsecureRegistries []string `yaml:"insecure-registries,omitempty"`
UpdateCheck *bool `yaml:"update-check,omitempty"`
Kubecontext string `yaml:"kube-context,omitempty"`
DefaultRepo string `yaml:"default-repo,omitempty"`
LocalCluster *bool `yaml:"local-cluster,omitempty"`
InsecureRegistries []string `yaml:"insecure-registries,omitempty"`
UpdateCheck *bool `yaml:"update-check,omitempty"`
Survey *SurveyConfig `yaml:"survey,omitempty"`
}

// SurveyConfig is the survey config information
type SurveyConfig struct {
DisablePrompt *bool `yaml:"disable-prompt,omitempty"`
LastTaken string `yaml:"last-taken,omitempty"`
LastPrompted string `yaml:"last-prompted,omitempty"`
}
33 changes: 33 additions & 0 deletions pkg/skaffold/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path/filepath"
"strings"
"sync"
"time"

"github.com/imdario/mergo"
"github.com/mitchellh/go-homedir"
Expand All @@ -36,6 +37,8 @@ import (
const (
defaultConfigDir = ".skaffold"
defaultConfigFile = "config"
tenDays = time.Hour * 24 * 10
threeMonths = time.Hour * 24 * 90
)

var (
Expand All @@ -51,6 +54,7 @@ var (

ReadConfigFile = readConfigFileCached
GetConfigForCurrentKubectx = getConfigForCurrentKubectx
current = time.Now
)

// readConfigFileCached reads the specified file and returns the contents
Expand Down Expand Up @@ -223,3 +227,32 @@ func IsUpdateCheckEnabled(configfile string) bool {
}
return cfg == nil || cfg.UpdateCheck == nil || *cfg.UpdateCheck
}

func ShouldDisplayPrompt(configfile string) bool {
cfg, disabled := isSurveyPromptDisabled(configfile)
return !disabled && !recentlyPromptedOrTaken(cfg)
}

func isSurveyPromptDisabled(configfile string) (*ContextConfig, bool) {
cfg, err := GetConfigForCurrentKubectx(configfile)
if err != nil {
return nil, false
}
return cfg, cfg != nil && cfg.Survey != nil && *cfg.Survey.DisablePrompt
}

func recentlyPromptedOrTaken(cfg *ContextConfig) bool {
if cfg == nil || cfg.Survey == nil {
return false
}
return lessThan(cfg.Survey.LastTaken, threeMonths) || lessThan(cfg.Survey.LastPrompted, tenDays)
}

func lessThan(date string, duration time.Duration) bool {
t, err := time.Parse(time.RFC3339, date)
if err != nil {
logrus.Debugf("could not parse data %s", date)
return false
}
return current().Sub(t) < duration
}
146 changes: 146 additions & 0 deletions pkg/skaffold/config/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -309,3 +310,148 @@ func TestIsKindCluster(t *testing.T) {
})
}
}

func TestIsSurveyPromptDisabled(t *testing.T) {
tests := []struct {
description string
cfg *ContextConfig
readErr error
expected bool
}{
{
description: "config disable-prompt is nil returns false",
cfg: &ContextConfig{},
},
{
description: "config disable-prompt is true",
cfg: &ContextConfig{Survey: &SurveyConfig{DisablePrompt: util.BoolPtr(true)}},
expected: true,
},
{
description: "config disable-prompt is false",
cfg: &ContextConfig{Survey: &SurveyConfig{DisablePrompt: util.BoolPtr(false)}},
},
{
description: "config is nil",
cfg: nil,
},
{
description: "config has err",
cfg: nil,
readErr: fmt.Errorf("error while reading"),
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&GetConfigForCurrentKubectx, func(string) (*ContextConfig, error) { return test.cfg, test.readErr })
_, actual := isSurveyPromptDisabled("dummyconfig")
t.CheckDeepEqual(test.expected, actual)
})
}
}

func TestLessThan(t *testing.T) {
tests := []struct {
description string
date string
duration time.Duration
expected bool
}{
{
description: "date is less than 10 days from 01/30/2019",
date: "2019-01-22T13:04:05Z",
duration: 10 * 24 * time.Hour,
expected: true,
},
{
description: "date is not less than 10 days from 01/30/2019",
date: "2019-01-19T13:04:05Z",
duration: 10 * 24 * time.Hour,
},
{
description: "date is not right format",
date: "01-19=20129",
expected: false,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&current, func() time.Time {
t, _ := time.Parse(time.RFC3339, "2019-01-30T12:04:05Z")
return t
})
t.CheckDeepEqual(test.expected, lessThan(test.date, test.duration))
})
}
}

func TestShouldDisplayPrompt(t *testing.T) {
tests := []struct {
description string
cfg *ContextConfig
expected bool
}{
{
description: "should not display prompt when prompt is disabled",
cfg: &ContextConfig{Survey: &SurveyConfig{DisablePrompt: util.BoolPtr(true)}},
},
{
description: "should not display prompt when last prompted is less than 2 weeks",
cfg: &ContextConfig{
Survey: &SurveyConfig{
DisablePrompt: util.BoolPtr(false),
LastPrompted: "2019-01-22T00:00:00Z",
},
},
},
{
description: "should not display prompt when last taken in less than 3 months",
cfg: &ContextConfig{
Survey: &SurveyConfig{
DisablePrompt: util.BoolPtr(false),
LastTaken: "2018-11-22T00:00:00Z",
},
},
},
{
description: "should display prompt when last prompted is before 2 weeks",
cfg: &ContextConfig{
Survey: &SurveyConfig{
DisablePrompt: util.BoolPtr(false),
LastPrompted: "2019-01-10T00:00:00Z",
},
},
expected: true,
},
{
description: "should display prompt when last taken is before than 3 months ago",
cfg: &ContextConfig{
Survey: &SurveyConfig{
DisablePrompt: util.BoolPtr(false),
LastTaken: "2017-11-10T00:00:00Z",
},
},
expected: true,
},
{
description: "should not display prompt when last taken is recent than 3 months ago",
cfg: &ContextConfig{
Survey: &SurveyConfig{
DisablePrompt: util.BoolPtr(false),
LastTaken: "2019-01-10T00:00:00Z",
LastPrompted: "2019-01-10T00:00:00Z",
},
},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&GetConfigForCurrentKubectx, func(string) (*ContextConfig, error) { return test.cfg, nil })
t.Override(&current, func() time.Time {
t, _ := time.Parse(time.RFC3339, "2019-01-30T12:04:05Z")
return t
})
t.CheckDeepEqual(test.expected, ShouldDisplayPrompt("dummyconfig"))
})
}
}

0 comments on commit 1ca7b66

Please sign in to comment.