filelist: Add FileList type

This adds a new type, FileList, to manage another new type (File). File
contains the mapping of source --> dest for a file or directory.
This commit is contained in:
Clayton Craft
2023-02-17 23:31:21 -08:00
parent a4be663e13
commit 662f559286

View File

@@ -1,5 +1,57 @@
package filelist
import "sync"
type FileLister interface {
List() ([]string, error)
List() (*FileList, error)
}
type File struct {
Source string
Dest string
}
type FileList struct {
m map[string]string
sync.RWMutex
}
func NewFileList() *FileList {
return &FileList{
m: make(map[string]string),
}
}
func (f *FileList) Add(src string, dest string) {
f.Lock()
defer f.Unlock()
f.m[src] = dest
}
func (f *FileList) Get(src string) (string, bool) {
f.RLock()
defer f.RUnlock()
dest, found := f.m[src]
return dest, found
}
// iterate through the list and and send each one as a new File over the
// returned channel
func (f *FileList) IterItems() <-chan File {
ch := make(chan File)
go func() {
f.RLock()
defer f.RUnlock()
for src, dest := range f.m {
ch <- File{
Source: src,
Dest: dest,
}
}
close(ch)
}()
return ch
}