fix: make fetcher check on creation that it supports Source.Importer
Alan Pearce alan@alanpearce.eu
Mon, 20 May 2024 23:55:40 +0200
4 files changed, 57 insertions(+), 41 deletions(-)
M internal/fetcher/channel.go → internal/fetcher/channel.go
@@ -22,6 +22,23 @@ SourceFile string Logger *slog.Logger } +func NewChannelFetcher( + source *config.Source, + dataPath string, + logger *slog.Logger, +) (*ChannelFetcher, error) { + switch source.Importer { + case config.Options: + return &ChannelFetcher{ + DataPath: dataPath, + Source: source, + Logger: logger, + }, nil + default: + return nil, fmt.Errorf("unsupported importer type %s", source.Importer) + } +} + func (i *ChannelFetcher) FetchIfNeeded( parent context.Context, ) (f FetchedFiles, updated bool, err error) { @@ -91,8 +108,7 @@ updated = before != after f = FetchedFiles{ - Options: path.Join(dest, i.Source.OutputPath, "options.json"), - Packages: path.Join(dest, i.Source.OutputPath, "packages.json"), + Options: path.Join(dest, i.Source.OutputPath, "options.json"), } return
M internal/fetcher/download.go → internal/fetcher/download.go
@@ -2,6 +2,7 @@ package fetcher import ( "context" + "fmt" "log/slog" "net/url" "path" @@ -16,6 +17,23 @@ DataPath string Source *config.Source SourceFile string Logger *slog.Logger +} + +func NewDownloadFetcher( + source *config.Source, + dataPath string, + logger *slog.Logger, +) (*DownloadFetcher, error) { + switch source.Importer { + case config.Options: + return &DownloadFetcher{ + DataPath: dataPath, + Source: source, + Logger: logger, + }, nil + default: + return nil, fmt.Errorf("unsupported importer type %s", source.Importer) + } } var files = map[string]string{
M internal/fetcher/main.go → internal/fetcher/main.go
@@ -18,42 +18,6 @@ type Fetcher interface { FetchIfNeeded(context.Context) (FetchedFiles, bool, error) } -func NewNixpkgsChannelFetcher( - source *config.Source, - dataPath string, - logger *slog.Logger, -) *NixpkgsChannelFetcher { - return &NixpkgsChannelFetcher{ - DataPath: dataPath, - Source: source, - Logger: logger, - } -} - -func NewChannelFetcher( - source *config.Source, - dataPath string, - logger *slog.Logger, -) *ChannelFetcher { - return &ChannelFetcher{ - DataPath: dataPath, - Source: source, - Logger: logger, - } -} - -func NewDownloadFetcher( - source *config.Source, - dataPath string, - logger *slog.Logger, -) *DownloadFetcher { - return &DownloadFetcher{ - DataPath: dataPath, - Source: source, - Logger: logger, - } -} - func New( source *config.Source, fetcherDataPath string, @@ -61,11 +25,11 @@ logger *slog.Logger, ) (fetcher Fetcher, err error) { switch source.Fetcher { case config.ChannelNixpkgs: - fetcher = NewNixpkgsChannelFetcher(source, fetcherDataPath, logger) + fetcher, err = NewNixpkgsChannelFetcher(source, fetcherDataPath, logger) case config.Channel: - fetcher = NewChannelFetcher(source, fetcherDataPath, logger) + fetcher, err = NewChannelFetcher(source, fetcherDataPath, logger) case config.Download: - fetcher = NewDownloadFetcher(source, fetcherDataPath, logger) + fetcher, err = NewDownloadFetcher(source, fetcherDataPath, logger) default: err = errors.Errorf("unsupported fetcher type %s", source.Fetcher.String()) }
M internal/fetcher/nixpkgs-channel.go → internal/fetcher/nixpkgs-channel.go
@@ -2,6 +2,7 @@ package fetcher import ( "context" + "fmt" "log/slog" "net/url" "path" @@ -21,6 +22,23 @@ func makeChannelURL(channel string, subPath string) (string, error) { url, err := url.JoinPath("https://channels.nixos.org/", channel, subPath) return url, errors.WithMessagef(err, "error creating URL") +} + +func NewNixpkgsChannelFetcher( + source *config.Source, + dataPath string, + logger *slog.Logger, +) (*NixpkgsChannelFetcher, error) { + switch source.Importer { + case config.Options, config.Packages: + return &NixpkgsChannelFetcher{ + DataPath: dataPath, + Source: source, + Logger: logger, + }, nil + default: + return nil, fmt.Errorf("unsupported importer type %s", source.Importer) + } } const (