| 1 | package filesystems |
| 2 | |
| 3 | import ( |
| 4 | "io/fs" |
| 5 | "strings" |
| 6 | "sync" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | DefaultFileSystemKey = "default" |
| 11 | ) |
| 12 | |
| 13 | var DefaultFileSystem = &wrapperFs{key: DefaultFileSystemKey, FS: OsFS{}} |
| 14 | |
| 15 | // wrapperFs exists so can easily add to wrapperFs down the line |
| 16 | type wrapperFs struct { |
| 17 | key string |
| 18 | fs.FS |
| 19 | } |
| 20 | |
| 21 | // FileSystemMap stores a map of filesystems |
| 22 | // the empty key will be overwritten to be the default key |
| 23 | // it includes a default filesystem, based off the os fs |
| 24 | type FileSystemMap struct { |
| 25 | m sync.Map |
| 26 | } |
| 27 | |
| 28 | // note that the first invocation of key cannot be called in a racy context. |
| 29 | func (f *FileSystemMap) key(k string) string { |
| 30 | if k == "" { |
| 31 | k = DefaultFileSystemKey |
| 32 | } |
| 33 | return k |
| 34 | } |
| 35 | |
| 36 | // Register will add the filesystem with key to later be retrieved |
| 37 | // A call with a nil fs will call unregister, ensuring that a call to Default() will never be nil |
| 38 | func (f *FileSystemMap) Register(k string, v fs.FS) { |
| 39 | k = f.key(k) |
| 40 | if v == nil { |
| 41 | f.Unregister(k) |
| 42 | return |
| 43 | } |
| 44 | f.m.Store(k, &wrapperFs{key: k, FS: v}) |
| 45 | } |
| 46 | |
| 47 | // Unregister will remove the filesystem with key from the filesystem map |
| 48 | // if the key is the default key, it will set the default to the osFS instead of deleting it |
| 49 | // modules should call this on cleanup to be safe |
| 50 | func (f *FileSystemMap) Unregister(k string) { |
| 51 | k = f.key(k) |
| 52 | if k == DefaultFileSystemKey { |
| 53 | f.m.Store(k, DefaultFileSystem) |
| 54 | } else { |
| 55 | f.m.Delete(k) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Get will get a filesystem with a given key |
| 60 | func (f *FileSystemMap) Get(k string) (v fs.FS, ok bool) { |
| 61 | k = f.key(k) |
| 62 | c, ok := f.m.Load(strings.TrimSpace(k)) |
| 63 | if !ok { |
| 64 | if k == DefaultFileSystemKey { |
| 65 | f.m.Store(k, DefaultFileSystem) |
| 66 | return DefaultFileSystem, true |
| 67 | } |
| 68 | return nil, ok |
| 69 | } |
| 70 | return c.(fs.FS), true |
| 71 | } |
| 72 | |
| 73 | // Default will get the default filesystem in the filesystem map |
| 74 | func (f *FileSystemMap) Default() fs.FS { |
| 75 | val, _ := f.Get(DefaultFileSystemKey) |
| 76 | return val |
| 77 | } |