manager/magefiles/files.go
Dan Anglin 2402833b1a
feat: a new home manager
This commit updates the scope of this project to manage the files and
directories within my home directory. The Makefile and helper bash
scripts are now replaced with mage targets so that the home directory is
now managed with Mage. The state of the home directory is managed using
a JSON configuration for each machine host. The manager is a set of mage
targets to manage various aspects of the home directory. At the moment
the manager can:

- ensure specified directories are present within the home directory.
- ensure application configuration files are up-to-date and have the
  correct symlinks within the user's home configuration directory.
- manages the user's bash profile (a.k.a bashrc) file.

Other notable changes:

- The X11 xinitrc is removed because it is not currently used and won't
  be used for the forseeable future as we slowly move to Wayland.
- All bashrc configurations are now defined in one file and is now fully
  managed by the manager.
- The dunst configuration is currently removed but will make a comeback.
- The ansible configuration is removed as it is no longer used.
- The logrotate configuration is updated and now generated from a
  template.
- Added configuration for the foot terminal.
- Added configuration for the River window manager.
2024-09-12 16:35:06 +01:00

84 lines
2 KiB
Go

//go:build mage
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"strings"
"github.com/magefile/mage/sh"
)
// Files ensure that the configuration files in the managed directory is up to date and
// ensures that they are symlinked correctly to the files in the user's home configuration
// directory.
func Files() error {
homeConfigDirectory, err := os.UserConfigDir()
if err != nil {
return fmt.Errorf("unable to get the user's home configuration directory: %w", err)
}
config, err := newConfig()
if err != nil {
return fmt.Errorf("unable to load the configuration: %w", err)
}
managedConfig := managedConfigSet(config.ManagedConfigurations)
if err = filepath.WalkDir(rootFilesDir, manageFilesFunc(homeConfigDirectory, managedConfig)); err != nil {
return fmt.Errorf("received an error while processing the files: %w", err)
}
return nil
}
func manageFilesFunc(homeConfigDirectory string, managedConfig map[string]struct{}) fs.WalkDirFunc {
return func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == rootFilesDir {
return nil
}
relativePath := strings.TrimPrefix(path, rootFilesDir+"/")
appConfigName := strings.SplitN(relativePath, "/", 2)[0]
if _, exists := managedConfig[appConfigName]; !exists {
return nil
}
managedPath := filepath.Join(rootManagedDir, relativePath)
configPath := filepath.Join(homeConfigDirectory, relativePath)
if d.IsDir() {
dirs := []string{managedPath, configPath}
for _, dir := range slices.All(dirs) {
if err := ensureDirectory(dir); err != nil {
return fmt.Errorf("unable to ensure the existence of the directory %q: %w", dir, err)
}
}
return nil
}
fmt.Println("Processing file:", relativePath)
if err := sh.Copy(managedPath, path); err != nil {
return fmt.Errorf("unable to copy %s to %s: %w", path, managedPath, err)
}
if err := ensureSymlink(managedPath, configPath); err != nil {
return err
}
return nil
}
}