feat: add check-config command
4 files changed, 62 insertions(+), 16 deletions(-)
A cmd/searchix-web/check-config.go
@@ -0,0 +1,42 @@ +package main + +import ( + "errors" + "fmt" + "strings" + + "github.com/Southclaws/fault" + "github.com/pelletier/go-toml/v2" + + "alin.ovh/searchix/internal/config" +) + +type CheckConfig struct{} + +func (*CheckConfig) Execute(_ []string) error { + err := config.Load(string(globalOptions.Config), &config.Config{}) + if err != nil { + var details *toml.StrictMissingError + if errors.As(err, &details) { + return fmt.Errorf("%s", strings.ReplaceAll(details.String(), "missing", "unknown")) + } + + return fault.Wrap(err) + } + + return nil +} + +func init() { + var opts CheckConfig + + _, err := parser.AddCommand( + "check-config", + "validate configuration", + "Validate the configuration file", + &opts, + ) + if err != nil { + panic(err) + } +}
M cmd/searchix-web/main.go → cmd/searchix-web/main.go
@@ -31,7 +31,7 @@ func main() { parser.CommandHandler = func(cmd flags.Commander, args []string) (err error) { switch cmd.(type) { - case *PrintDefaultsOptions, *Version: + case *PrintDefaultsOptions, *Version, *CheckConfig: default: logger = log.Configure(!globalOptions.Dev)
M internal/config/config.go → internal/config/config.go
@@ -107,28 +107,31 @@ return } +func Load(filename string, target *Config) error { + //nolint:forbidigo // need to read config file from anywhere + f, err := os.Open(filename) + if err != nil { + return fault.Wrap(err, fmsg.With("reading config failed")) + } + defer f.Close() + + dec := toml.NewDecoder(f) + dec.DisallowUnknownFields() + err = dec.Decode(&target) + + return fault.Wrap(err) +} + func GetConfig(filename string, log *log.Logger) (*Config, error) { + log.Debug("reading config", "filename", filename) + config := DefaultConfig if filename != "" { - log.Debug("reading config", "filename", filename) - //nolint:forbidigo // need to read config file from anywhere - f, err := os.Open(filename) - if err != nil { - return nil, fault.Wrap(err, fmsg.With("reading config failed")) - } - defer f.Close() - - dec := toml.NewDecoder(f) - dec.DisallowUnknownFields() - err = dec.Decode(&config) + err := Load(filename, &config) if err != nil { var tomlError *toml.DecodeError if errors.As(err, &tomlError) { return nil, fault.Wrap(err, fmsg.With(tomlError.Error())) - } - var missingConfigError *toml.StrictMissingError - if errors.As(err, &missingConfigError) { - return nil, fault.Newf("unexpected config: %s", missingConfigError.String()) } return nil, fault.Wrap(err, fmsg.With("config error"))
M nix/modules/default.nix → nix/modules/default.nix
@@ -252,6 +252,7 @@ inherit (cfg) environment; serviceConfig = defaultServiceConfig // { + ExecStartPre = "${package}/bin/searchix-web --config ${configFile} check-config"; ExecStart = "${package}/bin/searchix-web --config ${configFile} serve"; } // lib.optionalAttrs (cfg.settings.web.port < 1024) {