package app import ( "context" "fmt" "gitea.maximumdirect.net/eric/distributor/internal/adapters/local" "gitea.maximumdirect.net/eric/distributor/internal/config" "gitea.maximumdirect.net/eric/distributor/internal/storage" ) const storagePathKey = "path" type backendFactory struct { registry *storage.Registry } func newBackendFactory() *backendFactory { registry := storage.NewRegistry() _ = registry.Register(config.BackendLocal, func(ctx context.Context, cfg storage.OpenConfig) (storage.Backend, error) { if err := ctx.Err(); err != nil { return nil, err } return local.New(cfg[storagePathKey]) }) return &backendFactory{registry: registry} } func (f *backendFactory) openSource(ctx context.Context, source config.Backend) (storage.Backend, error) { if source.Backend != config.BackendLocal { return nil, fmt.Errorf("source backend %s is not implemented for execution", source.Backend) } return f.registry.Open(ctx, source.Backend, storage.OpenConfig{storagePathKey: source.Path}) } func (f *backendFactory) openDestination(ctx context.Context, destination config.Destination) (storage.Backend, error) { if destination.Backend != config.BackendLocal { return nil, fmt.Errorf("backend %s is not implemented for execution", destination.Backend) } return f.registry.Open(ctx, destination.Backend, storage.OpenConfig{storagePathKey: destination.Path}) } func (f *backendFactory) openLocalPath(ctx context.Context, path string) (storage.Backend, error) { return f.registry.Open(ctx, config.BackendLocal, storage.OpenConfig{storagePathKey: path}) }