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:
bwilliams committed 2026-10-21 16:22:45 +01:00
1 parent f467c6c7da
commit b0f93fd437
8 files changed
+131 -21

No files matched your search

+6 -1
View File
@@ -267,6 +267,10 @@ per key:
of the configuration.
- `default:"value"` is the field's default, parsed as if it were a string
passed to `SetValue` (e.g. `"10m"`, `"true"`).
- `init:"hidden"` on a nested section's field leaves the section out of the
file `config init` writes, unless `--all` is given (`InitDefaults`). The
test sources, `fts_mock` and `sleeper`, have it. The section is otherwise
as any other: loaded, and read and set by the `config` commands.
- A tagged field whose type is a struct is a nested section, addressed as
`section.key`. An embedded struct has its fields promoted, as Go does;
every source embeds `JSCSourcesSource` for `enabled` and `interval`.
@@ -324,7 +328,8 @@ To add a new section:
`JSCRateLimitRule`, has no constant and passes `""` to `flattenOf`.
2. Add the struct as a tagged field of its parent: `JobSearchConfig` in
`config.go`, or `JSCSources` in `conf_sources.go` for a source,
etc. The field's `cfg` tag must match the `JSCK*` constant.
etc. The field's `cfg` tag must match the `JSCK*` constant. Add
`init:"hidden"` if it's only for testing.
Nothing in `section.go` needs changing.
+20
View File
@@ -288,6 +288,26 @@ func TestConcreteSourceSections(t *testing.T) {
}
}
// InitDefaults leaves out the sections tagged init:"hidden" unless asked.
func TestInitDefaults(t *testing.T) {
all := InitDefaults(true)
shown := InitDefaults(false)
if all["sources.sleeper.enabled"] != false ||
all["sources.fts_mock.interval"] != "10m0s" {
t.Errorf("all = %v; want the test sources' defaults", all)
}
for key := range all {
hidden := strings.HasPrefix(key, "sources.fts_mock.") ||
strings.HasPrefix(key, "sources.sleeper.")
if _, found := shown[key]; found == hidden {
t.Errorf("%s: in the generated defaults = %v", key, found)
}
}
if shown["sources.eu_supply.base_url"] != "https://uk.eu-supply.com" {
t.Errorf("eu_supply missing: %v", shown)
}
}
func TestEUSupplySection(t *testing.T) {
conf := defaultConfig(t)
got := conf.Sources.EUSupply
+4 -2
View File
@@ -13,15 +13,17 @@ const JSCKSources string = "sources"
// JSCSources configures each source of opportunities, one field
// per source. Each field's cfg tag must match the section's JSCK* constant.
// The test sources (FTSMock and Sleeper) are tagged init:"hidden", so
// `config init` only writes them with --all.
// SkipRulesFile is the JSON file of rules that mark new opportunities
// Skipped (see internal/skiprules), read again when it changes; empty means
// none.
type JSCSources struct {
SkipRulesFile string `cfg:"skip_rules_file"`
FindATender JSCSourceFindATender `cfg:"find_a_tender"`
FTSMock JSCSourceFTSMock `cfg:"fts_mock"`
FTSMock JSCSourceFTSMock `cfg:"fts_mock" init:"hidden"`
EUSupply JSCSourceEUSupply `cfg:"eu_supply"`
Sleeper JSCSourceSleeper `cfg:"sleeper"`
Sleeper JSCSourceSleeper `cfg:"sleeper" init:"hidden"`
}
// ---------- Section methods (see Section) ----------
+24
View File
@@ -40,6 +40,30 @@ func LoadDefaultConfig() {
LoadToViper(conf)
}
// InitDefaults returns the default configuration as dotted keys, as
// Flatten gives it, for `config init` to write. Sections tagged
// init:"hidden" (the test sources) are left out unless includeHidden is set.
func InitDefaults(includeHidden bool) map[string]any {
var conf JobSearchConfig
if err := applyDefaults(&conf); err != nil {
panic(fmt.Sprintf("config: %v", err))
}
flat := conf.Flatten()
if includeHidden {
return flat
}
hidden := hiddenFromInitKeys("", reflect.TypeFor[JobSearchConfig]())
for _, section := range hidden {
for key := range flat {
if strings.HasPrefix(key, section+".") {
delete(flat, key)
}
}
}
return flat
}
// ---------- Section methods (see Section) ----------
// Map implements Section.
+28 -2
View File
@@ -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