config init now leaves out the test sources unless given --all
A section tagged init:"hidden" isn't written to the file config init generates; fts_mock and sleeper are. config init --all writes them too. They still load, and the config commands read and set them as before.
This commit is contained in:
1 parent
f467c6c7da
commit
b0f93fd437
8 files changed
+131
-21
No files matched your search
@@ -50,6 +50,10 @@ const (
|
||||
keyTag = "cfg"
|
||||
// defaultTag holds a field's default value.
|
||||
defaultTag = "default"
|
||||
// initTag set to initHidden on a section field leaves the section out of
|
||||
// the file `config init` writes (see InitDefaults), as for test sources.
|
||||
initTag = "init"
|
||||
initHidden = "hidden"
|
||||
)
|
||||
|
||||
// The unknown key error and the field types section.go handles specially.
|
||||
@@ -109,6 +113,8 @@ type field struct {
|
||||
section bool
|
||||
defaultValue string
|
||||
hasDefault bool
|
||||
// hiddenFromInit is set by initTag (see InitDefaults).
|
||||
hiddenFromInit bool
|
||||
}
|
||||
|
||||
// isSectionList reports whether listType is a list of sections, []S for a
|
||||
@@ -146,8 +152,9 @@ func fieldsOf(structType reflect.Type) []field {
|
||||
fieldType: structField.Type,
|
||||
section: structField.Type.Kind() == reflect.Struct &&
|
||||
structField.Type != timeType,
|
||||
defaultValue: defaultValue,
|
||||
hasDefault: hasDefault,
|
||||
defaultValue: defaultValue,
|
||||
hasDefault: hasDefault,
|
||||
hiddenFromInit: structField.Tag.Get(initTag) == initHidden,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -244,6 +251,25 @@ func flattenInto(flat map[string]any, prefix string, section reflect.Value) {
|
||||
}
|
||||
}
|
||||
|
||||
// hiddenFromInitKeys returns the dotted keys of the sections under section
|
||||
// (prefixed with prefix, if non-empty) whose field has initTag set to
|
||||
// initHidden, descending into nested sections.
|
||||
func hiddenFromInitKeys(prefix string, sectionType reflect.Type) []string {
|
||||
var hidden []string
|
||||
for _, configField := range fieldsOf(sectionType) {
|
||||
if !configField.section {
|
||||
continue
|
||||
}
|
||||
key := joinKey(prefix, configField.key)
|
||||
if configField.hiddenFromInit {
|
||||
hidden = append(hidden, key)
|
||||
continue
|
||||
}
|
||||
hidden = append(hidden, hiddenFromInitKeys(key, configField.fieldType)...)
|
||||
}
|
||||
return hidden
|
||||
}
|
||||
|
||||
// exportValue returns a field's value as Map, Flatten and ValueOf give it
|
||||
// out: durations and byte sizes become strings such as "1m30s" and "32KiB",
|
||||
// so values agree with kindOf and can be passed back to SetValue; []string is
|
||||
|
||||
Reference in new issue
Block a user