From 37deedc9b1da92571548c920721984d545269eb4 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 13 May 2024 15:07:00 +0200 Subject: refactor: move Source/Repository types to config package --- internal/config/config.go | 19 +++++++------- internal/config/repository.go | 44 +++++++++++++++++++++++++++++++ internal/config/source.go | 60 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 internal/config/repository.go create mode 100644 internal/config/source.go (limited to 'internal/config') diff --git a/internal/config/config.go b/internal/config/config.go index 71b56be..340b027 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,7 +6,6 @@ import ( "maps" "net/url" "os" - "searchix/internal/importer" "time" "github.com/pelletier/go-toml/v2" @@ -32,7 +31,7 @@ type Config struct { CSP CSP `toml:"content-security-policy"` ExtraBodyHTML template.HTML `toml:"extra-body-html"` Headers map[string]string - Sources map[string]*importer.Source + Sources map[string]*Source } var defaultConfig = Config{ @@ -43,12 +42,12 @@ var defaultConfig = Config{ Headers: map[string]string{ "x-content-type-options": "nosniff", }, - Sources: map[string]*importer.Source{ + Sources: map[string]*Source{ "nixos": { Name: "NixOS", Key: "nixos", Enable: true, - Type: importer.Channel, + Type: Channel, Channel: "nixpkgs", URL: "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz", ImportPath: "nixos/release.nix", @@ -56,7 +55,7 @@ var defaultConfig = Config{ OutputPath: "share/doc/nixos/options.json", FetchTimeout: 5 * time.Minute, ImportTimeout: 15 * time.Minute, - Repo: importer.Repository{ + Repo: Repository{ Type: "github", Owner: "NixOS", Repo: "nixpkgs", @@ -66,7 +65,7 @@ var defaultConfig = Config{ Name: "Darwin", Key: "darwin", Enable: false, - Type: importer.Channel, + Type: Channel, Channel: "darwin", URL: "https://github.com/LnL7/nix-darwin/archive/master.tar.gz", ImportPath: "release.nix", @@ -74,7 +73,7 @@ var defaultConfig = Config{ OutputPath: "share/doc/darwin/options.json", FetchTimeout: 5 * time.Minute, ImportTimeout: 15 * time.Minute, - Repo: importer.Repository{ + Repo: Repository{ Type: "github", Owner: "LnL7", Repo: "nix-darwin", @@ -86,13 +85,13 @@ var defaultConfig = Config{ Enable: false, Channel: "home-manager", URL: "https://github.com/nix-community/home-manager/archive/master.tar.gz", - Type: importer.Channel, + Type: Channel, ImportPath: "default.nix", Attribute: "docs.json", OutputPath: "share/doc/home-manager/options.json", FetchTimeout: 5 * time.Minute, ImportTimeout: 15 * time.Minute, - Repo: importer.Repository{ + Repo: Repository{ Type: "github", Owner: "nix-community", Repo: "home-manager", @@ -123,7 +122,7 @@ func GetConfig(filename string) (*Config, error) { } } - maps.DeleteFunc(config.Sources, func(_ string, v *importer.Source) bool { + maps.DeleteFunc(config.Sources, func(_ string, v *Source) bool { return !v.Enable }) diff --git a/internal/config/repository.go b/internal/config/repository.go new file mode 100644 index 0000000..8c17a4f --- /dev/null +++ b/internal/config/repository.go @@ -0,0 +1,44 @@ +package config + +import ( + "fmt" + "strings" +) + +type RepoType int + +const ( + GitHub = iota + 1 +) + +type Repository struct { + Type string `default:"github"` + Owner string + Repo string + Revision string +} + +func (f RepoType) String() string { + switch f { + case GitHub: + return "github" + default: + return fmt.Sprintf("RepoType(%d)", f) + } +} + +func parseRepoType(name string) (RepoType, error) { + switch strings.ToLower(name) { + case "github": + return GitHub, nil + default: + return Unknown, fmt.Errorf("unsupported repo type %s", name) + } +} + +func (f *RepoType) UnmarshalText(text []byte) error { + var err error + *f, err = parseRepoType(string(text)) + + return err +} diff --git a/internal/config/source.go b/internal/config/source.go new file mode 100644 index 0000000..2b1b440 --- /dev/null +++ b/internal/config/source.go @@ -0,0 +1,60 @@ +package config + +import ( + "fmt" + "time" + + "github.com/stoewer/go-strcase" +) + +type Type int + +const ( + Unknown = iota + Channel + ChannelNixpkgs +) + +func (f Type) String() string { + switch f { + case Channel: + return "channel" + case ChannelNixpkgs: + return "channel-nixpkgs" + } + + return fmt.Sprintf("Fetcher(%d)", f) +} + +func parseType(name string) (Type, error) { + switch strcase.KebabCase(name) { + case "channel": + return Channel, nil + case "channel-nixpkgs": + return ChannelNixpkgs, nil + default: + return Unknown, fmt.Errorf("unsupported fetcher %s", name) + } +} + +func (f *Type) UnmarshalText(text []byte) error { + var err error + *f, err = parseType(string(text)) + + return err +} + +type Source struct { + Name string + Key string + Enable bool + Type Type + Channel string + URL string + Attribute string + ImportPath string `toml:"import-path"` + FetchTimeout time.Duration `toml:"fetch-timeout"` + ImportTimeout time.Duration `toml:"import-timeout"` + OutputPath string `toml:"output-path"` + Repo Repository +} -- cgit 1.4.1