drop defConfForType

This commit is contained in:
Anton Evangelatov 2022-02-16 12:29:16 +01:00
parent f1f878a000
commit 949ec65118
3 changed files with 11 additions and 21 deletions

View File

@ -283,7 +283,7 @@ func openRepo(cctx *cli.Context) (repo.LockedRepo, types.KeyStore, error) {
} }
} }
lr, err := r.Lock(repo.WalletRepoType{}) lr, err := r.Lock(repo.Wallet)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }

View File

@ -50,7 +50,7 @@ func NewRepoTypeFromString(t string) RepoType {
case "Worker": case "Worker":
return Worker return Worker
case "Wallet": case "Wallet":
return WalletRepoType{} return Wallet
default: default:
panic("unknown RepoType") panic("unknown RepoType")
} }
@ -113,21 +113,19 @@ func (f worker) Config() interface{} {
return &struct{}{} return &struct{}{}
} }
type WalletRepoType struct { var Wallet wallet
type wallet struct {
} }
func (f WalletRepoType) Type() string { func (f wallet) Type() string {
return "Wallet" return "Wallet"
} }
func (f WalletRepoType) Config() interface{} { func (f wallet) Config() interface{} {
return &struct{}{} return &struct{}{}
} }
func defConfForType(t RepoType) interface{} {
return t.Config()
}
var log = logging.Logger("repo") var log = logging.Logger("repo")
var ErrRepoExists = xerrors.New("repo exists") var ErrRepoExists = xerrors.New("repo exists")
@ -209,7 +207,7 @@ func (fsr *FsRepo) initConfig(t RepoType) error {
return err return err
} }
comm, err := config.ConfigComment(defConfForType(t)) comm, err := config.ConfigComment(t.Config())
if err != nil { if err != nil {
return xerrors.Errorf("comment: %w", err) return xerrors.Errorf("comment: %w", err)
} }
@ -450,7 +448,7 @@ func (fsr *fsLockedRepo) Config() (interface{}, error) {
} }
func (fsr *fsLockedRepo) loadConfigFromDisk() (interface{}, error) { func (fsr *fsLockedRepo) loadConfigFromDisk() (interface{}, error) {
return config.FromFile(fsr.configPath, defConfForType(fsr.repoType)) return config.FromFile(fsr.configPath, fsr.repoType.Config())
} }
func (fsr *fsLockedRepo) SetConfig(c func(interface{})) error { func (fsr *fsLockedRepo) SetConfig(c func(interface{})) error {

View File

@ -36,9 +36,6 @@ type MemRepo struct {
keystore map[string]types.KeyInfo keystore map[string]types.KeyInfo
blockstore blockstore.Blockstore blockstore blockstore.Blockstore
// given a repo type, produce the default config
configF func(t RepoType) interface{}
// holds the current config value // holds the current config value
config struct { config struct {
sync.Mutex sync.Mutex
@ -144,7 +141,6 @@ var _ Repo = &MemRepo{}
// MemRepoOptions contains options for memory repo // MemRepoOptions contains options for memory repo
type MemRepoOptions struct { type MemRepoOptions struct {
Ds datastore.Datastore Ds datastore.Datastore
ConfigF func(RepoType) interface{}
KeyStore map[string]types.KeyInfo KeyStore map[string]types.KeyInfo
} }
@ -155,9 +151,6 @@ func NewMemory(opts *MemRepoOptions) *MemRepo {
if opts == nil { if opts == nil {
opts = &MemRepoOptions{} opts = &MemRepoOptions{}
} }
if opts.ConfigF == nil {
opts.ConfigF = defConfForType
}
if opts.Ds == nil { if opts.Ds == nil {
opts.Ds = dssync.MutexWrap(datastore.NewMapDatastore()) opts.Ds = dssync.MutexWrap(datastore.NewMapDatastore())
} }
@ -169,7 +162,6 @@ func NewMemory(opts *MemRepoOptions) *MemRepo {
repoLock: make(chan struct{}, 1), repoLock: make(chan struct{}, 1),
blockstore: blockstore.WrapIDStore(blockstore.NewMemorySync()), blockstore: blockstore.WrapIDStore(blockstore.NewMemorySync()),
datastore: opts.Ds, datastore: opts.Ds,
configF: opts.ConfigF,
keystore: opts.KeyStore, keystore: opts.KeyStore,
} }
} }
@ -284,7 +276,7 @@ func (lmem *lockedMemRepo) Config() (interface{}, error) {
defer lmem.mem.config.Unlock() defer lmem.mem.config.Unlock()
if lmem.mem.config.val == nil { if lmem.mem.config.val == nil {
lmem.mem.config.val = lmem.mem.configF(lmem.t) lmem.mem.config.val = lmem.t.Config()
} }
return lmem.mem.config.val, nil return lmem.mem.config.val, nil
@ -299,7 +291,7 @@ func (lmem *lockedMemRepo) SetConfig(c func(interface{})) error {
defer lmem.mem.config.Unlock() defer lmem.mem.config.Unlock()
if lmem.mem.config.val == nil { if lmem.mem.config.val == nil {
lmem.mem.config.val = lmem.mem.configF(lmem.t) lmem.mem.config.val = lmem.t.Config()
} }
c(lmem.mem.config.val) c(lmem.mem.config.val)