Skip to content

Commit 16cd1a7

Browse files
authored
cmd/geth, internal/flags: print envvar config source and bad names (#28119)
1 parent 4fa3db4 commit 16cd1a7

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

cmd/geth/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ func init() {
249249
app.Before = func(ctx *cli.Context) error {
250250
maxprocs.Set() // Automatically set GOMAXPROCS to match Linux container CPU quota.
251251
flags.MigrateGlobalFlags(ctx)
252-
return debug.Setup(ctx)
252+
if err := debug.Setup(ctx); err != nil {
253+
return err
254+
}
255+
flags.CheckEnvVars(ctx, app.Flags, "GETH")
256+
return nil
253257
}
254258
app.After = func(ctx *cli.Context) error {
255259
debug.Exit()

internal/flags/helpers.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"fmt"
2121
"os"
2222
"regexp"
23+
"sort"
2324
"strings"
2425

2526
"github.com/ethereum/go-ethereum/internal/version"
27+
"github.com/ethereum/go-ethereum/log"
2628
"github.com/ethereum/go-ethereum/params"
2729
"github.com/mattn/go-isatty"
2830
"github.com/urfave/cli/v2"
@@ -263,3 +265,37 @@ func AutoEnvVars(flags []cli.Flag, prefix string) {
263265
}
264266
}
265267
}
268+
269+
// CheckEnvVars iterates over all the environment variables and checks if any of
270+
// them look like a CLI flag but is not consumed. This can be used to detect old
271+
// or mistyped names.
272+
func CheckEnvVars(ctx *cli.Context, flags []cli.Flag, prefix string) {
273+
known := make(map[string]string)
274+
for _, flag := range flags {
275+
docflag, ok := flag.(cli.DocGenerationFlag)
276+
if !ok {
277+
continue
278+
}
279+
for _, envvar := range docflag.GetEnvVars() {
280+
known[envvar] = flag.Names()[0]
281+
}
282+
}
283+
keyvals := os.Environ()
284+
sort.Strings(keyvals)
285+
286+
for _, keyval := range keyvals {
287+
key := strings.Split(keyval, "=")[0]
288+
if !strings.HasPrefix(key, prefix) {
289+
continue
290+
}
291+
if flag, ok := known[key]; ok {
292+
if ctx.Count(flag) > 0 {
293+
log.Info("Config environment variable found", "envvar", key, "shadowedby", "--"+flag)
294+
} else {
295+
log.Info("Config environment variable found", "envvar", key)
296+
}
297+
} else {
298+
log.Warn("Unknown config environment variable", "envvar", key)
299+
}
300+
}
301+
}

0 commit comments

Comments
 (0)