prepareExportDir creates dir, or checks that it's empty, so an export never mixes with other files.
(dir string)
| 147 | // prepareExportDir creates dir, or checks that it's empty, so an export never |
| 148 | // mixes with other files. |
| 149 | func prepareExportDir(dir string) error { |
| 150 | entries, err := os.ReadDir(dir) |
| 151 | switch { |
| 152 | case errors.Is(err, fs.ErrNotExist): |
| 153 | return os.MkdirAll(dir, 0o755) |
| 154 | case err != nil: |
| 155 | return err |
| 156 | case len(entries) > 0: |
| 157 | return fmt.Errorf("%s isn't empty; export to a new folder", dir) |
| 158 | } |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | func writeFile(path string, write func(io.Writer) error) error { |
| 163 | f, err := os.Create(path) |
no test coverage detected