manager/magefiles/config.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

112 lines
2.8 KiB
Go

//go:build mage
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)
type config struct {
ManagedConfigurations []string `json:"managedConfigurations"`
BashProfile configBashProfile `json:"bashProfile"`
Directories configDirectories `json:"directories"`
Git configGit `json:"git"`
}
type configDirectories struct {
UseDefaultDirectories bool `json:"useDefaultDirectories"`
IncludeXDGDirectories bool `json:"includeXDGDirectories"`
AdditionalDirectories []string `json:"additionalDirectories"`
}
type configGit struct {
GpgSign bool `json:"gpgSign"`
User configGitUser `json:"user"`
}
type configGitUser struct {
Email string `json:"email"`
Name string `json:"name"`
SigningKey string `json:"signingKey"`
}
type configBashProfile struct {
Manage bool `json:"manage"`
Filename string `json:"filename"`
SessionPaths []configBashProfileSessionPath `json:"sessionPaths"`
XdgDirectories map[string]string `json:"xdgDirectories"`
EnvironmentVariables map[string]string `json:"environmentVariables"`
Aliases map[string]string `json:"aliases"`
Commands []configBashProfileCommand `json:"commands"`
}
type configBashProfileSessionPath struct {
Path string `json:"path"`
Description string `json:"description"`
}
type configBashProfileCommand struct {
Command string `json:"command"`
Description string `json:"description"`
}
func newConfig() (config, error) {
cfg := defaultConfig()
path, err := configFilePath()
if err != nil {
return config{}, fmt.Errorf("unable to calculate the config file path: %w", err)
}
file, err := os.Open(path)
if err != nil {
return config{}, fmt.Errorf("unable to open the file: %w", err)
}
defer file.Close()
if err = json.NewDecoder(file).Decode(&cfg); err != nil {
return config{}, fmt.Errorf("unable to decode the JSON file: %w", err)
}
return cfg, nil
}
func configFilePath() (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", fmt.Errorf("unable to get the machine's hostname: %w", err)
}
hostnameParts := strings.SplitN(hostname, "-", 3)
if len(hostnameParts) != 3 {
return "", fmt.Errorf("unexpected hostname format")
}
identifier := hostnameParts[1]
return filepath.Join(configDir, identifier+".json"), nil
}
func defaultConfig() config {
return config{
Directories: configDirectories{
UseDefaultDirectories: true,
IncludeXDGDirectories: true,
AdditionalDirectories: []string{},
},
Git: configGit{
GpgSign: false,
User: configGitUser{
Email: "",
Name: "",
SigningKey: "",
},
},
ManagedConfigurations: []string{},
}
}