|
| 1 | +package fs |
| 2 | + |
| 3 | +import ( |
| 4 | + gocontext "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/bmatcuk/doublestar/v4" |
| 12 | + "github.com/flanksource/duty/artifact" |
| 13 | +) |
| 14 | + |
| 15 | +type localFS struct { |
| 16 | + base string |
| 17 | +} |
| 18 | + |
| 19 | +type localFileInfo struct { |
| 20 | + os.FileInfo |
| 21 | + fullpath string |
| 22 | +} |
| 23 | + |
| 24 | +func (t localFileInfo) FullPath() string { |
| 25 | + return t.fullpath |
| 26 | +} |
| 27 | + |
| 28 | +func NewLocalFS(base string) *localFS { |
| 29 | + return &localFS{base: base} |
| 30 | +} |
| 31 | + |
| 32 | +func (t *localFS) Close() error { |
| 33 | + return nil |
| 34 | +} |
| 35 | + |
| 36 | +func (t *localFS) ReadDir(name string) ([]artifact.FileInfo, error) { |
| 37 | + if strings.Contains(name, "*") { |
| 38 | + return t.ReadDirGlob(name) |
| 39 | + } |
| 40 | + |
| 41 | + path := filepath.Join(t.base, name) |
| 42 | + files, err := os.ReadDir(path) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + output := make([]artifact.FileInfo, 0, len(files)) |
| 48 | + for _, match := range files { |
| 49 | + fullPath := filepath.Join(path, match.Name()) |
| 50 | + info, err := os.Stat(fullPath) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + output = append(output, localFileInfo{FileInfo: info, fullpath: fullPath}) |
| 56 | + } |
| 57 | + |
| 58 | + return output, nil |
| 59 | +} |
| 60 | + |
| 61 | +func (t *localFS) ReadDirGlob(name string) ([]artifact.FileInfo, error) { |
| 62 | + base, pattern := doublestar.SplitPattern(filepath.Join(t.base, name)) |
| 63 | + matches, err := doublestar.Glob(os.DirFS(base), pattern) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + output := make([]artifact.FileInfo, 0, len(matches)) |
| 69 | + for _, match := range matches { |
| 70 | + fullPath := filepath.Join(base, match) |
| 71 | + info, err := os.Stat(fullPath) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + output = append(output, localFileInfo{FileInfo: info, fullpath: fullPath}) |
| 77 | + } |
| 78 | + |
| 79 | + return output, nil |
| 80 | +} |
| 81 | + |
| 82 | +func (t *localFS) Stat(name string) (os.FileInfo, error) { |
| 83 | + return os.Stat(filepath.Join(t.base, name)) |
| 84 | +} |
| 85 | + |
| 86 | +func (t *localFS) Read(_ gocontext.Context, path string) (io.ReadCloser, error) { |
| 87 | + return os.Open(filepath.Join(t.base, path)) |
| 88 | +} |
| 89 | + |
| 90 | +func (t *localFS) Write(_ gocontext.Context, path string, data io.Reader) (os.FileInfo, error) { |
| 91 | + fullpath := filepath.Join(t.base, path) |
| 92 | + |
| 93 | + err := os.MkdirAll(filepath.Dir(fullpath), os.ModePerm) |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("error creating base directory: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + f, err := os.Create(fullpath) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + _, err = io.Copy(f, data) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + return t.Stat(path) |
| 109 | +} |
0 commit comments