Skip to content

Commit 7a3b9a5

Browse files
committed
filesystem: split out leading directory creation
Extract leading directory creation into its own private function ('createLeadingDirs()') in 'filesystem.go'. Although 'WriteFile()' is still the only usage of this functionality, it could prove useful for functions added to 'fileSystem' in the future. Signed-off-by: Victoria Dye <[email protected]>
1 parent 6d6829b commit 7a3b9a5

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

internal/common/filesystem.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ func NewFileSystem() FileSystem {
3131
return &fileSystem{}
3232
}
3333

34+
func (f *fileSystem) createLeadingDirs(filename string) error {
35+
parentDir := path.Dir(filename)
36+
err := os.MkdirAll(parentDir, DefaultDirPermissions)
37+
if err != nil {
38+
return fmt.Errorf("error creating parent directories: %w", err)
39+
}
40+
41+
return nil
42+
}
43+
3444
func (f *fileSystem) GetLocalExecutable(name string) (string, error) {
3545
thisExePath, err := os.Executable()
3646
if err != nil {
@@ -64,11 +74,9 @@ func (f *fileSystem) FileExists(filename string) (bool, error) {
6474
}
6575

6676
func (f *fileSystem) WriteFile(filename string, content []byte) error {
67-
// Get filename parent path
68-
parentDir := path.Dir(filename)
69-
err := os.MkdirAll(parentDir, DefaultDirPermissions)
77+
err := f.createLeadingDirs(filename)
7078
if err != nil {
71-
return fmt.Errorf("error creating parent directories: %w", err)
79+
return err
7280
}
7381

7482
err = os.WriteFile(filename, content, DefaultFilePermissions)

0 commit comments

Comments
 (0)