Skip to content

Commit

Permalink
change env var names
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Nov 2, 2021
1 parent 103d78d commit f256b5c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 9 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
const (
configCron = "cron"
configRunOnStart = "runOnStart"
configBeta = "beta"

configAPIPort = "api.port"
configAPIUsername = "api.username"
Expand Down
61 changes: 61 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
4 changes: 1 addition & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
32 changes: 29 additions & 3 deletions pkg/types/features.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package types

import (
"fmt"
"go.uber.org/zap"
"strings"
)

// Features feature flags
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
}

0 comments on commit f256b5c

Please sign in to comment.