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

+26
View File
@@ -214,6 +214,32 @@ func TestConfigInit(t *testing.T) {
}
}
// The test sources are left out of the file unless --all is given.
func TestConfigInitTestSources(t *testing.T) {
cmdtest.Setup(t, "")
mustRun(t, "config", "init", "./default.yaml")
written := cmdtest.ReadFile(t, "default.yaml")
cmdtest.Contains(t, written, "find_a_tender:", "eu_supply:")
for _, hidden := range []string{"fts_mock", "sleeper"} {
if strings.Contains(written, hidden) {
t.Errorf("init wrote %s without --all:\n%s", hidden, written)
}
}
mustRun(t, "config", "init", "--all", "./all.yaml")
cmdtest.Contains(
t, cmdtest.ReadFile(t, "all.yaml"), "fts_mock:", "sleeper:",
)
// The test sources still load and are set as usual.
mustRun(
t, "--config", "default.yaml",
"config", "set", "sources.sleeper.enabled", "true",
)
cmdtest.Contains(t, cmdtest.ReadFile(t, "default.yaml"), "sleeper:")
}
// With no path (or a blank one), init searches the default locations. The
// working directory is one, so its config.yaml is found. (Not finding one
// would write to /etc/job-search, which a test can't do.)
+22 -15
View File
@@ -17,7 +17,11 @@ func commandInit() *cobra.Command {
cmd := &cobra.Command{
Use: "init [config_path]",
Args: cobra.MatchAll(cobra.MinimumNArgs(0), cobra.MaximumNArgs(1)),
Short: "Create an empty configuration file",
Short: "Create a configuration file holding the defaults",
Long: `Create a configuration file holding the defaults, at config_path or,
without one, the first config.yaml found in /etc/job-search then the working
directory (/etc/job-search/config.yaml if there is none). The test sources
(fts_mock and sleeper) are left out unless --all is given.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("command persistent pre-run", "command", "config.init")
},
@@ -64,26 +68,29 @@ func commandInit() *cobra.Command {
return err
}
// Load default configuration
config.LoadDefaultConfig()
if viper.ConfigFileUsed() == "" {
viper.SetConfigFile(config.EtcConfigPath + "/config.yaml")
path := viper.ConfigFileUsed()
if path == "" {
path = config.EtcConfigPath + "/config.yaml"
}
fmt.Print(
"writing default configuration to file: " +
viper.ConfigFileUsed() +
"\n",
)
if err := viper.WriteConfig(); err != nil {
return err
// A Viper of its own, holding only the keys to write: the global
// one would write every default, the hidden sections' included.
all, _ := cmd.Flags().GetBool("all")
generated := viper.New()
for key, value := range config.InitDefaults(all) {
generated.Set(key, value)
}
generated.SetConfigFile(path)
return nil
fmt.Print("writing default configuration to file: " + path + "\n")
return generated.WriteConfig()
},
}
cmd.Flags().Bool(
"all", false, "also write the test sources (fts_mock and sleeper)",
)
// Config flag should be hidden as the config path should be provided
cmd.SetHelpFunc(func(command *cobra.Command, args []string) {
command.Flags().MarkHidden("config") //nolint:errcheck,gosec // a root flag, so it always exists