Dan Anglin
903e5d8b66
- ensure configured home directories are present and their mode set to 0700 - move dotfiles to files - added JSON config
77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
//go:build mage
|
|
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
)
|
|
|
|
const (
|
|
dirModePerm fs.FileMode = 0700
|
|
)
|
|
|
|
// Directories ensure that the directories specified in the configuration is present within the home directory.
|
|
func Directories() error {
|
|
config, err := newConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("unable to load the configuration: %w", err)
|
|
}
|
|
|
|
userHome, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return fmt.Errorf("unable to get the user's home directory: %w", err)
|
|
}
|
|
|
|
for _, dir := range slices.All(config.Directories) {
|
|
path := filepath.Join(userHome, dir)
|
|
if err := ensureDirectory(path); err != nil {
|
|
return fmt.Errorf("unable to ensure that %s is present: %w", path, err)
|
|
}
|
|
|
|
fmt.Printf("Successfully ensured %s is present.\n", path)
|
|
}
|
|
|
|
|
|
/*
|
|
for each dir:
|
|
|
|
- get full path
|
|
- get dirinfo
|
|
- if no path, create directory and set mode to 0700 and exit (create a separate function for this)
|
|
- ensure existing directory mode is set to 0700
|
|
*/
|
|
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|