mirror of
https://github.com/bakito/adguardhome-sync.git
synced 2025-01-07 15:49:13 +08:00
correct interface name env var from REPLICA%s_INTERFACWENAME to REPLICA%s_INTERFACENAME (#159)
This commit is contained in:
parent
94ed2c6245
commit
cfcffab9d1
3 changed files with 22 additions and 7 deletions
19
cmd/root.go
19
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue