Skip to content

Commit f36fe60

Browse files
Log detected chia environment variables before applying them to config (#132)
When FillValuesFromEnvironment fails, the error message gives no indication that environment variables are being read or which one caused the issue. This is especially confusing when the user is using --set flags and has a stale or malformed chia-prefixed env var set. Add logging that lists each detected chia./chia__ env var before applying them, and update the error message to direct the user to check the listed variables.
1 parent 03cb8c8 commit f36fe60

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

cmd/config/config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package config
22

33
import (
4+
"os"
5+
"strings"
6+
7+
"github.com/chia-network/go-modules/pkg/slogs"
48
"github.com/spf13/cobra"
59
"github.com/spf13/viper"
610

@@ -12,6 +16,21 @@ var (
1216
retries uint
1317
)
1418

19+
// logDetectedChiaEnvVars scans the process environment for variables prefixed
20+
// with "chia." or "chia__" and logs each one so the user knows which env vars
21+
// will be applied to the config before --set flags are processed.
22+
func logDetectedChiaEnvVars() {
23+
for _, env := range os.Environ() {
24+
for _, prefix := range []string{"chia.", "chia__"} {
25+
if strings.HasPrefix(env, prefix) {
26+
name, _, _ := strings.Cut(env, "=")
27+
slogs.Logr.Info("Detected chia environment variable that will be applied to config", "env_var", name)
28+
break
29+
}
30+
}
31+
}
32+
}
33+
1534
// configCmd represents the config command
1635
var configCmd = &cobra.Command{
1736
Use: "config",

cmd/config/edit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ chia-tools config edit --set full_node.port=58444 --dry-run`,
4646
cfg.SetIndependentLogging()
4747
}
4848

49+
logDetectedChiaEnvVars()
4950
err = cfg.FillValuesFromEnvironment()
5051
if err != nil {
51-
slogs.Logr.Fatal("error filling values from environment", "error", err)
52+
slogs.Logr.Fatal("error applying chia environment variables to config. Check the environment variables listed above for incorrect values", "error", err)
5253
}
5354

5455
dryRun := viper.GetBool("dry-run")

cmd/config/generate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ var generateCmd = &cobra.Command{
2121
slogs.Logr.Fatal("error loading default config", "error", err)
2222
}
2323

24+
logDetectedChiaEnvVars()
2425
err = cfg.FillValuesFromEnvironment()
2526
if err != nil {
26-
slogs.Logr.Fatal("error filling values from environment", "error", err)
27+
slogs.Logr.Fatal("error applying chia environment variables to config. Check the environment variables listed above for incorrect values", "error", err)
2728
}
2829

2930
valuesToSet := viper.GetStringMapString("set")

0 commit comments

Comments
 (0)