50 lines
954 B
Go
50 lines
954 B
Go
|
//go:build mage
|
||
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"io/fs"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
dirModePerm fs.FileMode = 0o700
|
||
|
|
||
|
configDir string = "config"
|
||
|
rootManagedDir string = "managed"
|
||
|
rootFilesDir string = "files"
|
||
|
rootTemplateDir string = "templates"
|
||
|
)
|
||
|
|
||
|
func ensureDirectory(path string) error {
|
||
|
info, err := os.Stat(path)
|
||
|
if err != nil {
|
||
|
if errors.Is(err, os.ErrNotExist) {
|
||
|
if err := os.Mkdir(path, dirModePerm); err != nil {
|
||
|
return fmt.Errorf("unable to create the directory: %w", err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return fmt.Errorf(
|
||
|
"received an unexpected error after attempting to get the directory information: %w",
|
||
|
err,
|
||
|
)
|
||
|
}
|
||
|
|
||
|
if !info.IsDir() {
|
||
|
return errors.New("the path exists but it is not a directory")
|
||
|
}
|
||
|
|
||
|
if info.Mode().Perm() != dirModePerm {
|
||
|
if err := os.Chmod(path, dirModePerm); err != nil {
|
||
|
return fmt.Errorf("unable to update the directory's mode to %d: %w", dirModePerm, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|