Skip to content

Commit

Permalink
feat: check if the name is duplicated
Browse files Browse the repository at this point in the history
when add piece storage
  • Loading branch information
LinZexiao committed Jul 4, 2022
1 parent 6b6180f commit 3d22688
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ func (m *MarketConfig) RemovePieceStorage(name string) error {
return fmt.Errorf("piece storage %s not found", name)
}

func (cfg *MarketConfig) AddFsPieceStorage(fsps *FsPieceStorage) (err error) {
func (cfg *MarketConfig) AddFsPieceStorage(fsps *FsPieceStorage) error {
cfg.PieceStorage.Fs = append(cfg.PieceStorage.Fs, fsps)
return SaveConfig(cfg)
}

func (cfg *MarketConfig) AddS3PieceStorage(fsps *S3PieceStorage) (err error) {
func (cfg *MarketConfig) AddS3PieceStorage(fsps *S3PieceStorage) error {
cfg.PieceStorage.S3 = append(cfg.PieceStorage.S3, fsps)
return SaveConfig(cfg)
}
Expand Down
14 changes: 13 additions & 1 deletion piecestorage/storagemgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,20 @@ func (p *PieceStorageManager) AddMemPieceStorage(s IPieceStorage) {
p.storages = append(p.storages, s)
}

func (p *PieceStorageManager) AddPieceStorage(s IPieceStorage) {
func (p *PieceStorageManager) AddPieceStorage(s IPieceStorage) error {
// check if storage already exist in manager and it's name is not empty
if s.GetName() == "" {
return fmt.Errorf("storage name is empty")
}
for _, st := range p.storages {
if st.GetName() == s.GetName() {
return fmt.Errorf("duplicate storage name: %s", s.GetName())
}
}

p.storages = append(p.storages, s)

return nil
}

func randStorageSelector(storages []IPieceStorage) (IPieceStorage, error) {
Expand Down

0 comments on commit 3d22688

Please sign in to comment.