Skip to content

Commit

Permalink
fix corner case for LOG_LEVEL + --env, fix #972
Browse files Browse the repository at this point in the history
  • Loading branch information
Slach committed Aug 4, 2024
1 parent 6c04020 commit 289b4b1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v2.5.23
BUG FIXES
- fix corner case for LOG_LEVEL + --env, fix [972](https://github.com/Altinity/clickhouse-backup/issues/972)

# v2.5.22
IMPROVEMENTS
- redirect logs into stderr instead of stdout, fix [969](https://github.com/Altinity/clickhouse-backup/issues/969)
Expand Down
27 changes: 22 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,29 @@ type oldEnvValues struct {
func OverrideEnvVars(ctx *cli.Context) map[string]oldEnvValues {
env := ctx.StringSlice("env")
oldValues := map[string]oldEnvValues{}
logLevel := "info"
if os.Getenv("LOG_LEVEL") != "" {
logLevel = os.Getenv("LOG_LEVEL")
}
log_helper.SetLogLevelFromString(logLevel)
if len(env) > 0 {
for _, v := range env {
envVariable := strings.SplitN(v, "=", 2)
if len(envVariable) < 2 {
envVariable = append(envVariable, "true")
processEnvFromCli := func(process func(envVariable []string)) {
for _, v := range env {
envVariable := strings.SplitN(v, "=", 2)
if len(envVariable) < 2 {
envVariable = append(envVariable, "true")
}
process(envVariable)
}
}

processEnvFromCli(func(envVariable []string) {
if envVariable[0] == "LOG_LEVEL" {
log_helper.SetLogLevelFromString(envVariable[1])
}
})

processEnvFromCli(func(envVariable []string) {
log.Info().Msgf("override %s=%s", envVariable[0], envVariable[1])
oldValue, wasPresent := os.LookupEnv(envVariable[0])
oldValues[envVariable[0]] = oldEnvValues{
Expand All @@ -675,7 +692,7 @@ func OverrideEnvVars(ctx *cli.Context) map[string]oldEnvValues {
if err := os.Setenv(envVariable[0], envVariable[1]); err != nil {
log.Warn().Msgf("can't override %s=%s, error: %v", envVariable[0], envVariable[1], err)
}
}
})
}
return oldValues
}
Expand Down

0 comments on commit 289b4b1

Please sign in to comment.