change env var names

This commit is contained in:
bakito 2021-11-01 18:38:35 +01:00
parent 103d78d0ee
commit f256b5ca81
No known key found for this signature in database
GPG key ID: FAF93C1C384DD6B4
6 changed files with 104 additions and 9 deletions

View file

@ -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

13
cmd/cmd_suite_test.go Normal file
View file

@ -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")
}

View file

@ -16,7 +16,6 @@ import (
const (
configCron = "cron"
configRunOnStart = "runOnStart"
configBeta = "beta"
configAPIPort = "api.port"
configAPIUsername = "api.username"

61
cmd/root_test.go Normal file
View file

@ -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))
}

View file

@ -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")

View file

@ -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")
}
}