Skip to content

Commit

Permalink
fix: The env should override flags
Browse files Browse the repository at this point in the history
Close #747

Signed-off-by: Cloud Tsai <cloudxxx8@gmail.com>
  • Loading branch information
cloudxxx8 committed Aug 23, 2024
1 parent 6f7163b commit fe8b0f6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
28 changes: 21 additions & 7 deletions bootstrap/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type Processor struct {
wg *sync.WaitGroup
configUpdated UpdatedStream
dic *di.Container
overwriteConfig *bool
providerHasConfig bool
commonConfigClient configuration.Client
appConfigClient configuration.Client
Expand Down Expand Up @@ -199,7 +200,7 @@ func (cp *Processor) Process(
return fmt.Errorf("failed check for Configuration Provider has private configiuration: %s", err.Error())
}

if cp.providerHasConfig && !cp.overwriteConfig() {
if cp.providerHasConfig && !cp.getOverwriteConfig() {
privateServiceConfig, err = copyConfigurationStruct(serviceConfig)
if err != nil {
return err
Expand Down Expand Up @@ -245,7 +246,7 @@ func (cp *Processor) Process(
}

// Now load the private config from a local file if any of these conditions are true
if !useProvider || !cp.providerHasConfig || cp.overwriteConfig() {
if !useProvider || !cp.providerHasConfig || cp.getOverwriteConfig() {
filePath := GetConfigFileLocation(cp.lc, cp.flags)
configMap, err := cp.loadConfigYamlFromFile(filePath)
if err != nil {
Expand All @@ -264,7 +265,7 @@ func (cp *Processor) Process(
}

if useProvider {
if err := privateConfigClient.PutConfigurationMap(configMap, cp.overwriteConfig()); err != nil {
if err := privateConfigClient.PutConfigurationMap(configMap, cp.getOverwriteConfig()); err != nil {
return fmt.Errorf("could not push private configuration into Configuration Provider: %s", err.Error())
}

Expand Down Expand Up @@ -332,8 +333,21 @@ func (cp *Processor) Process(
return err
}

func (cp *Processor) overwriteConfig() bool {
return environment.OverwriteConfig() || cp.flags.OverwriteConfig()
func (cp *Processor) getOverwriteConfig() bool {
// use the struct level pointer variable to prevent the logEnvironmentOverride log several times
if cp.overwriteConfig != nil {
return *cp.overwriteConfig
}

overwrite := cp.flags.OverwriteConfig()

if b, ok := environment.OverwriteConfig(cp.lc); ok {
overwrite = b
}

cp.overwriteConfig = &overwrite

return overwrite
}

func getLocalIP() string {
Expand Down Expand Up @@ -595,7 +609,7 @@ func (cp *Processor) LoadCustomConfigSection(updatableConfig interfaces.Updatabl
err.Error())
}

if exists && !cp.overwriteConfig() {
if exists && !cp.getOverwriteConfig() {
rawConfig, err := configClient.GetConfiguration(updatableConfig)
if err != nil {
return fmt.Errorf(
Expand Down Expand Up @@ -639,7 +653,7 @@ func (cp *Processor) LoadCustomConfigSection(updatableConfig interfaces.Updatabl
}

var overwriteMessage = ""
if exists && cp.overwriteConfig() {
if exists && cp.getOverwriteConfig() {
overwriteMessage = "(overwritten)"
}
cp.lc.Infof("Custom Config loaded from file and pushed to Configuration Provider %s", overwriteMessage)
Expand Down
17 changes: 10 additions & 7 deletions bootstrap/environment/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
envKeyConfigFile = "EDGEX_CONFIG_FILE"
envKeyFileURITimeout = "EDGEX_FILE_URI_TIMEOUT"
envKeyRemoteServiceHosts = "EDGEX_REMOTE_SERVICE_HOSTS"
envOverwriteConfig = "EDGEX_OVERWRITE_CONFIG"
envKeyOverwriteConfig = "EDGEX_OVERWRITE_CONFIG"

noConfigProviderValue = "none"

Expand Down Expand Up @@ -493,17 +493,20 @@ func GetRemoteServiceHosts(lc logger.LoggingClient, remoteHosts []string) []stri
return strings.Split(envValue, ",")
}

// OverwriteConfig returns whether the local configuration should be pushed (overwrite) into the Configuration provider
func OverwriteConfig() bool {
envValue := os.Getenv(envOverwriteConfig)
// OverwriteConfig returns whether the local configuration should be pushed (overwrite) into the Configuration provider.
// The second bool return value indicates whether the environment variable is set.
func OverwriteConfig(lc logger.LoggingClient) (bool, bool) {
envValue := os.Getenv(envKeyOverwriteConfig)
if len(envValue) == 0 {
return false
return false, false
}

logEnvironmentOverride(lc, "-o/--overwrite", envKeyOverwriteConfig, envValue)

boolValue, err := strconv.ParseBool(envValue)
if err != nil {
return false
return false, true
}

return boolValue
return boolValue, true
}

0 comments on commit fe8b0f6

Please sign in to comment.