diff --git a/cmd/root.go b/cmd/root.go index a232611..14ec3fe 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,6 +12,7 @@ import ( "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" + "go.uber.org/zap" ) const ( @@ -54,7 +55,9 @@ const ( envReplicasAPIPathFormat = "REPLICA%s_APIPATH" envReplicasInsecureSkipVerifyFormat = "REPLICA%s_INSECURESKIPVERIFY" envReplicasAutoSetup = "REPLICA%s_AUTOSETUP" - envReplicasInterfaceName = "REPLICA%s_INTERFACWENAME" + envReplicasInterfaceName = "REPLICA%s_INTERFACENAME" + // Deprecated: use envReplicasInterfaceName instead + envReplicasInterfaceNameDeprecated = "REPLICA%s_INTERFACWENAME" ) var ( @@ -122,20 +125,20 @@ func initConfig() { } } -func getConfig() (*types.Config, error) { +func getConfig(logger *zap.SugaredLogger) (*types.Config, error) { cfg := &types.Config{} if err := viper.Unmarshal(cfg); err != nil { return nil, err } if len(cfg.Replicas) == 0 { - cfg.Replicas = append(cfg.Replicas, collectEnvReplicas()...) + cfg.Replicas = append(cfg.Replicas, collectEnvReplicas(logger)...) } return cfg, nil } // Manually collect replicas from env. -func collectEnvReplicas() []types.AdGuardInstance { +func collectEnvReplicas(logger *zap.SugaredLogger) []types.AdGuardInstance { var replicas []types.AdGuardInstance for _, v := range os.Environ() { if envReplicasURLPattern.MatchString(v) { @@ -149,6 +152,14 @@ func collectEnvReplicas() []types.AdGuardInstance { AutoSetup: strings.EqualFold(os.Getenv(fmt.Sprintf(envReplicasAutoSetup, sm[1])), "true"), InterfaceName: os.Getenv(fmt.Sprintf(envReplicasInterfaceName, sm[1])), } + if re.InterfaceName != "" { + if in, ok := os.LookupEnv(fmt.Sprintf(envReplicasInterfaceNameDeprecated, sm[1])); ok { + logger. + With("correct", envReplicasInterfaceName, "deprecated", envReplicasInterfaceNameDeprecated). + Warn("Deprecated env variable is used, please use the correct one") + re.InterfaceName = in + } + } replicas = append(replicas, re) } } diff --git a/cmd/root_test.go b/cmd/root_test.go index 6e993f1..0a11a28 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -3,9 +3,11 @@ package cmd import ( "os" + "github.com/bakito/adguardhome-sync/pkg/log" "github.com/bakito/adguardhome-sync/pkg/types" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "go.uber.org/zap" ) var envVars = []string{ @@ -23,7 +25,9 @@ var envVars = []string{ } var _ = Describe("Run", func() { + var logger *zap.SugaredLogger BeforeEach(func() { + logger = log.GetLogger("root") for _, envVar := range envVars { Ω(os.Unsetenv(envVar)).ShouldNot(HaveOccurred()) } @@ -31,7 +35,7 @@ var _ = Describe("Run", func() { }) Context("getConfig", func() { It("features should be true by default", func() { - cfg, err := getConfig() + cfg, err := getConfig(logger) Ω(err).ShouldNot(HaveOccurred()) verifyFeatures(cfg, true) }) @@ -39,7 +43,7 @@ var _ = Describe("Run", func() { for _, envVar := range envVars { Ω(os.Setenv(envVar, "false")).ShouldNot(HaveOccurred()) } - cfg, err := getConfig() + cfg, err := getConfig(logger) Ω(err).ShouldNot(HaveOccurred()) verifyFeatures(cfg, false) }) diff --git a/cmd/run.go b/cmd/run.go index f9a20ab..d96bce5 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -14,7 +14,7 @@ var doCmd = &cobra.Command{ Long: `Synchronizes the configuration form an origin instance to a replica`, RunE: func(cmd *cobra.Command, args []string) error { logger = log.GetLogger("run") - cfg, err := getConfig() + cfg, err := getConfig(logger) if err != nil { logger.Error(err) return err