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

+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