Skip to content

Commit 3ec0ea2

Browse files
authored
usage: skip structs marked as hidden in usage help output (grafana#5655)
Signed-off-by: Miguel Ángel Ortuño <[email protected]>
1 parent 308fe73 commit 3ec0ea2

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

cmd/mimir/main_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,10 @@ func TestFieldCategoryOverridesNotStale(t *testing.T) {
443443

444444
fs := flag.NewFlagSet("test", flag.PanicOnError)
445445

446+
// Add ignored flags.
447+
flagext.IgnoredFlag(fs, configFileOption, "Configuration file to load.")
448+
_ = fs.Bool(configExpandEnv, false, "Expands ${var} or $var in config according to the values of the environment variables.")
449+
446450
var (
447451
cfg mimir.Config
448452
mf mainFlags

pkg/util/fieldcategory/overrides.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ var overrides = map[string]Category{
6868
"server.path-prefix": Advanced,
6969
"server.register-instrumentation": Advanced,
7070
"server.log-request-at-info-level-enabled": Advanced,
71+
72+
// main.go global flags
73+
"config.file": Basic,
74+
"config.expand-env": Basic,
7175
}
7276

7377
func AddOverrides(o map[string]Category) {

pkg/util/usage/usage.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func Usage(printAll bool, configs ...interface{}) error {
5959
case "deprecated":
6060
fieldCat = fieldcategory.Deprecated
6161
}
62+
} else {
63+
// The field is neither an override nor has been parsed, so we'll skip it.
64+
return
6265
}
6366

6467
if fieldCat != fieldcategory.Basic && !printAll {
@@ -149,7 +152,7 @@ func parseStructure(structure interface{}, fields map[uintptr]reflect.StructFiel
149152
fields[fieldValue.Addr().Pointer()] = field
150153

151154
// Recurse if a struct
152-
if field.Type.Kind() != reflect.Struct || ignoreStructType(field.Type) || !field.IsExported() {
155+
if field.Type.Kind() != reflect.Struct || isFieldHidden(field) || ignoreStructType(field.Type) || !field.IsExported() {
153156
continue
154157
}
155158

0 commit comments

Comments
 (0)