fix: set default values for custom sources https://codeberg.org/alanpearce/searchix/issues/4
5 files changed, 40 insertions(+), 16 deletions(-)
M go.mod → go.mod
@@ -10,6 +10,7 @@ github.com/andybalholm/brotli v1.1.1 github.com/bcicen/jstream v1.0.1 github.com/blevesearch/bleve/v2 v2.5.2 github.com/blevesearch/bleve_index_api v1.2.8 + github.com/creasty/defaults v1.8.0 github.com/crewjam/csp v0.0.2 github.com/fsnotify/fsnotify v1.9.0 github.com/getsentry/sentry-go v0.33.0
M go.sum → go.sum
@@ -53,6 +53,8 @@ github.com/blevesearch/zapx/v15 v15.4.2 h1:sWxpDE0QQOTjyxYbAVjt3+0ieu8NCE0fDRaFxEsp31k= github.com/blevesearch/zapx/v15 v15.4.2/go.mod h1:1pssev/59FsuWcgSnTa0OeEpOzmhtmr/0/11H0Z8+Nw= github.com/blevesearch/zapx/v16 v16.2.4 h1:tGgfvleXTAkwsD5mEzgM3zCS/7pgocTCnO1oyAUjlww= github.com/blevesearch/zapx/v16 v16.2.4/go.mod h1:Rti/REtuuMmzwsI8/C/qIzRaEoSK/wiFYw5e5ctUKKs= +github.com/creasty/defaults v1.8.0 h1:z27FJxCAa0JKt3utc0sCImAEb+spPucmKoOdLHvHYKk= +github.com/creasty/defaults v1.8.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM= github.com/crewjam/csp v0.0.2 h1:fIq6o0Z6bkABlvLT3kB0XgPnVX9iNXSAGMILs6AqHVw= github.com/crewjam/csp v0.0.2/go.mod h1:0tirp4wHwMLZZtV+HXRqGFkUO7uD2ux+1ECvK+7/xFI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
M gomod2nix.toml → gomod2nix.toml
@@ -79,6 +79,9 @@ hash = "sha256-R8Eh3N4e8CDXiW47J8ZBnfMY1TTnX1SJPwQc4gYChi8=" [mod."github.com/blevesearch/zapx/v16"] version = "v16.2.4" hash = "sha256-Jo5k7DflV/ghszOWJTCOGVyyLMvlvSYyxRrmSIFjyEE=" + [mod."github.com/creasty/defaults"] + version = "v1.8.0" + hash = "sha256-I1LE1cfOhMS5JxB7+fWTKieefw2Gge1UhIZh+A6pa6s=" [mod."github.com/crewjam/csp"] version = "v0.0.2" hash = "sha256-4vlGmDdQjPiXmueCV51fJH/hRcG8eqhCi9TENCXjzfA="
M internal/config/config.go → internal/config/config.go
@@ -7,6 +7,7 @@ "os" "time" "alin.ovh/x/log" + "github.com/creasty/defaults" "github.com/pelletier/go-toml/v2" "gitlab.com/tozd/go/errors" )@@ -141,6 +142,15 @@ maps.DeleteFunc(config.Importer.Sources, func(_ string, v *Source) bool { return !v.Enable }) + + for k, v := range config.Importer.Sources { + if v.Key == "" { + v.Key = k + } + if err := defaults.Set(v); err != nil { + return nil, errors.Wrap(err, "setting defaults failed") + } + } return &config, nil }
M internal/config/structs.go → internal/config/structs.go
@@ -5,7 +5,9 @@ // keep config structs here so that lll ignores the long lines (go doesn't support multi-line struct tags) import ( "fmt" + "strings" + "github.com/creasty/defaults" "go.uber.org/zap/zapcore" )@@ -37,22 +39,22 @@ UpdateAt LocalTime `comment:"Time of day (UTC) to run fetch/import process"` } type Source struct { - Name string `comment:"Human-readable name of source for generating links"` - Order uint `comment:"Order in which to show source in web interface."` - Key string `comment:"Machine-readable name of source. Must be URL- and path-safe."` - Enable bool `comment:"Controls whether to show in the web interface and to run fetch/import jobs."` - Fetcher Fetcher `comment:"How to fetch options.json. One of 'channel', 'channel-nixpkgs' or 'download'."` - Importer ImporterType `comment:"Kind of data available from source. Currently supports 'packages' and 'options'."` - Channel string `comment:"(Fetcher=channel) Local name for channel, (Fetcher=channel-nixpkgs) Remote name of channel."` - URL string `comment:"(Fetcher=channel) Remote URL for channel, (Fetcher=download) Path containing files named 'revision' and 'options.json'."` - Attribute string `comment:"(Fetcher=channel) Nix attribute name (i.e. nix-build -A) that builds an {options,packages}.json"` - ImportPath string `comment:"(Fetcher=channel) Sub-path of imported channel which contains the attribute above, e.g. release.nix"` - Timeout Duration `comment:"Abort import if it takes longer than this."` - OutputPath string `comment:"(Fetcher=channel) Path under ./result symlink to folder containing {options,packages}.json."` - Repo Repository `comment:"Used to generate declaration/definition links"` - Programs ProgramsDB `comment:"Used to enable searching for programs in multi-program packages"` - Manpages Manpages `comment:"Used to enable searching for manpages"` - JSONDepth int `comment:"Depth at which packages/object object is to be found"` + Name string `default:"-" comment:"Human-readable name of source for generating links"` + Order uint ` comment:"Order in which to show source in web interface."` + Key string ` comment:"Machine-readable name of source. Must be URL- and path-safe."` + Enable bool ` comment:"Controls whether to show in the web interface and to run fetch/import jobs."` + Fetcher Fetcher ` comment:"How to fetch options.json. One of 'channel', 'channel-nixpkgs' or 'download'."` + Importer ImporterType `default:"options" comment:"Kind of data available from source. Currently supports 'packages' and 'options'."` + Channel string ` comment:"(Fetcher=channel) Local name for channel, (Fetcher=channel-nixpkgs) Remote name of channel."` + URL string ` comment:"(Fetcher=channel) Remote URL for channel, (Fetcher=download) Path containing files named 'revision' and 'options.json'."` + Attribute string `default:"options" comment:"(Fetcher=channel) Nix attribute name (i.e. nix-build -A) that builds an {options,packages}.json"` + ImportPath string ` comment:"(Fetcher=channel) Sub-path of imported channel which contains the attribute above, e.g. release.nix"` + Timeout Duration `default:"5m" comment:"Abort import if it takes longer than this."` + OutputPath string `default:"share/doc/nixos" comment:"(Fetcher=channel) Path under ./result symlink to folder containing {options,packages}.json."` + Repo Repository ` comment:"Used to generate declaration/definition links"` + Programs ProgramsDB ` comment:"Used to enable searching for programs in multi-program packages"` + Manpages Manpages ` comment:"Used to enable searching for manpages"` + JSONDepth int `default:"1" comment:"Depth at which packages/object object is to be found"` } type ProgramsDB struct {@@ -75,3 +77,9 @@ default: return fmt.Sprintf("Source(%s)", source.Name) } } + +func (source *Source) SetDefaults() { + if defaults.CanUpdate(source.Name) { + source.Name = strings.ToTitle(source.Key[0:1]) + source.Key[1:] + } +}