From f256b5ca81284aebc2e22b414cd5b243dab28883 Mon Sep 17 00:00:00 2001 From: bakito Date: Mon, 1 Nov 2021 18:38:35 +0100 Subject: [PATCH] change env var names --- README.md | 2 -- cmd/cmd_suite_test.go | 13 +++++++++ cmd/root.go | 1 - cmd/root_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++ cmd/run.go | 4 +-- pkg/types/features.go | 32 ++++++++++++++++++++--- 6 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 cmd/cmd_suite_test.go create mode 100644 cmd/root_test.go diff --git a/README.md b/README.md index 3f21be5..860d0c8 100644 --- a/README.md +++ b/README.md @@ -113,10 +113,8 @@ services: # - FEATURES_CLIENTSETTINGS=true # - FEATURES_SERVICES=true # - FEATURES_FILTERS=true - # - FEATURES_DHCP_SERVERCONFIG=true # - FEATURES_DHCP_STATICLEASES=true - # - FEATURES_DNS_SERVERCONFIG=true # - FEATURES_DNS_ACCESSLISTS=true # - FEATURES_DNS_REWRITES=true diff --git a/cmd/cmd_suite_test.go b/cmd/cmd_suite_test.go new file mode 100644 index 0000000..22d1c57 --- /dev/null +++ b/cmd/cmd_suite_test.go @@ -0,0 +1,13 @@ +package cmd_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestCmd(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Cmd Suite") +} diff --git a/cmd/root.go b/cmd/root.go index 8523568..f64e112 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,7 +16,6 @@ import ( const ( configCron = "cron" configRunOnStart = "runOnStart" - configBeta = "beta" configAPIPort = "api.port" configAPIUsername = "api.username" diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 0000000..8d828f9 --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,61 @@ +package cmd + +import ( + "github.com/bakito/adguardhome-sync/pkg/types" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "os" +) + +var envVars = []string{ + "FEATURES_GENERALSETTINGS", + "FEATURES_QUERYLOGCONFIG", + "FEATURES_STATSCONFIG", + "FEATURES_CLIENTSETTINGS", + "FEATURES_SERVICES", + "FEATURES_FILTERS", + "FEATURES_DHCP_SERVERCONFIG", + "FEATURES_DHCP_STATICLEASES", + "FEATURES_DNS_SERVERCONFIG", + "FEATURES_DNS_ACCESSLISTS", + "FEATURES_DNS_REWRITES", +} + +var _ = Describe("Run", func() { + + BeforeEach(func() { + for _, envVar := range envVars { + Ω(os.Unsetenv(envVar)).ShouldNot(HaveOccurred()) + } + initConfig() + }) + Context("getConfig", func() { + It("features should be true by default", func() { + cfg, err := getConfig() + Ω(err).ShouldNot(HaveOccurred()) + verifyFeatures(cfg, true) + }) + It("features should be false", func() { + for _, envVar := range envVars { + Ω(os.Setenv(envVar, "false")).ShouldNot(HaveOccurred()) + } + cfg, err := getConfig() + Ω(err).ShouldNot(HaveOccurred()) + verifyFeatures(cfg, false) + }) + }) +}) + +func verifyFeatures(cfg *types.Config, value bool) { + Ω(cfg.Features.GeneralSettings).Should(Equal(value)) + Ω(cfg.Features.QueryLogConfig).Should(Equal(value)) + Ω(cfg.Features.StatsConfig).Should(Equal(value)) + Ω(cfg.Features.ClientSettings).Should(Equal(value)) + Ω(cfg.Features.Services).Should(Equal(value)) + Ω(cfg.Features.Filters).Should(Equal(value)) + Ω(cfg.Features.DHCP.ServerConfig).Should(Equal(value)) + Ω(cfg.Features.DHCP.StaticLeases).Should(Equal(value)) + Ω(cfg.Features.DNS.ServerConfig).Should(Equal(value)) + Ω(cfg.Features.DNS.AccessLists).Should(Equal(value)) + Ω(cfg.Features.DNS.Rewrites).Should(Equal(value)) +} diff --git a/cmd/run.go b/cmd/run.go index 9de959b..1ef6467 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -50,6 +50,7 @@ func init() { _ = viper.BindPFlag(configFeatureDNSRewrites, doCmd.PersistentFlags().Lookup("feature-dns-rewrites")) doCmd.PersistentFlags().Bool("feature-general-settings", true, "Enable general settings feature") _ = viper.BindPFlag(configFeatureGeneralSettings, doCmd.PersistentFlags().Lookup("feature-general-settings")) + _ = viper.BindPFlag("features.generalSettings", doCmd.PersistentFlags().Lookup("feature-general-settings")) doCmd.PersistentFlags().Bool("feature-query-log-config", true, "Enable query log config feature") _ = viper.BindPFlag(configFeatureQueryLogConfig, doCmd.PersistentFlags().Lookup("feature-query-log-config")) doCmd.PersistentFlags().Bool("feature-stats-config", true, "Enable stats config feature") @@ -61,9 +62,6 @@ func init() { doCmd.PersistentFlags().Bool("feature-filters", true, "Enable filters sync feature") _ = viper.BindPFlag(configFeatureFilters, doCmd.PersistentFlags().Lookup("feature-filters")) - doCmd.PersistentFlags().String("beta", "", "Enable beta features (comma separated list)") - _ = viper.BindPFlag(configBeta, doCmd.PersistentFlags().Lookup("beta")) - doCmd.PersistentFlags().String("origin-url", "", "Origin instance url") _ = viper.BindPFlag(configOriginURL, doCmd.PersistentFlags().Lookup("origin-url")) doCmd.PersistentFlags().String("origin-api-path", "/control", "Origin instance API path") diff --git a/pkg/types/features.go b/pkg/types/features.go index aa799bf..08f9bc7 100644 --- a/pkg/types/features.go +++ b/pkg/types/features.go @@ -1,9 +1,7 @@ package types import ( - "fmt" "go.uber.org/zap" - "strings" ) // Features feature flags @@ -31,6 +29,7 @@ type DNS struct { Rewrites bool `json:"rewrites" yaml:"rewrites"` } +// LogDisabled log all disabled features func (f *Features) LogDisabled(l *zap.SugaredLogger) { var features []string if !f.DHCP.ServerConfig { @@ -39,8 +38,35 @@ func (f *Features) LogDisabled(l *zap.SugaredLogger) { if !f.DHCP.StaticLeases { features = append(features, "DHCP.StaticLeases") } + if !f.DNS.AccessLists { + features = append(features, "DHCP.AccessLists") + } + if !f.DNS.ServerConfig { + features = append(features, "DHCP.ServerConfig") + } + if !f.DNS.Rewrites { + features = append(features, "DHCP.Rewrites") + } + if !f.GeneralSettings { + features = append(features, "GeneralSettings") + } + if !f.QueryLogConfig { + features = append(features, "QueryLogConfig") + } + if !f.StatsConfig { + features = append(features, "StatsConfig") + } + if !f.ClientSettings { + features = append(features, "ClientSettings") + } + if !f.Services { + features = append(features, "Services") + } + if !f.Filters { + features = append(features, "Filters") + } if len(features) > 0 { - l.With("features", fmt.Sprintf("[%s]", strings.Join(features, ","))).Info("Disabled features") + l.With("features", features).Info("Disabled features") } }