checkpoint: manage home dirs

This commit is contained in:
Dan Anglin 2024-09-10 08:41:31 +01:00
parent 97ec604911
commit 0c9e47588e
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
8 changed files with 200 additions and 70 deletions

View file

@ -1,27 +0,0 @@
{
"directories": [
".local/bin",
".local/cache",
".local/config",
".local/data",
".local/state",
".local/state/bash",
".local/state/less",
"Applications",
"Certificates",
"Docker",
"Documents",
"Downloads",
"Games",
"Git",
"Laboratory",
"Laboratory/Training",
"Laboratory/Lab",
"Music",
"Notes",
"Pictures",
"Projects",
"Templates",
"Videos"
]
}

27
config/falcon.json Normal file
View file

@ -0,0 +1,27 @@
{
"directories": {
"useDefaultDirectories": true,
"includeXDGDirectories": true,
"additionalDirectories": [
"./local/goblin",
"./local/software",
"Certificates",
"Docker",
"Installations",
"ISOs",
"Laboratory",
"Laboratory/Lab",
"Laboratory/Training",
"Notes",
"Projects"
]
},
"git": {
"gpgSign": true,
"user": {
"email": "daangling@gmail.com",
"name": "Dan Anglin",
"signingKey": "0C1D44CFBEE68638"
}
}
}

15
config/sparrow.json Normal file
View file

@ -0,0 +1,15 @@
{
"directories": {
"useDefaultDirectories": true,
"includeXDGDirectories": true,
"additionalDirectories": [],
},
"git": {
"gpgSign": false,
"user": {
"email": "daangling@gmail.com",
"name": "Dan Anglin",
"signingKey": ""
}
}
}

View file

@ -7,31 +7,83 @@ import (
"fmt"
"os"
"path/filepath"
)
const (
configDir string = "./config/"
configFileName string = "config.json"
"strings"
)
type config struct {
Directories []string `json:"directories"`
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"`
User string `json:"user"`
SigningKey string `json:"signingKey"`
}
func newConfig() (config, error) {
var cfg config
cfg := defaultConfig()
path := filepath.Join(configDir, configFileName)
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 cfg, fmt.Errorf("unable to open the file: %w", err)
return config{}, fmt.Errorf("unable to open the file: %w", err)
}
defer file.Close()
if err = json.NewDecoder(file).Decode(&cfg); err != nil {
return cfg, fmt.Errorf("unable to decode the JSON file: %w", err)
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: "",
User: "",
SigningKey: "",
},
},
}
}

8
magefiles/const.go Normal file
View file

@ -0,0 +1,8 @@
//go:build mage
package main
const (
configDir string = "./config"
rootManagedDir string = "./managed"
)

View file

@ -12,7 +12,7 @@ import (
)
const (
dirModePerm fs.FileMode = 0700
dirModePerm fs.FileMode = 0o700
)
// Directories ensure that the directories specified in the configuration is present within the home directory.
@ -27,30 +27,35 @@ func Directories() error {
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)
}
directories := make([]string, 0)
fmt.Printf("Successfully ensured %s is present.\n", path)
if config.Directories.UseDefaultDirectories{
defaultHomeDirs := homeDirectories(userHome, defaultDirectories())
directories = append(directories, defaultHomeDirs...)
}
if config.Directories.IncludeXDGDirectories{
directories = append(directories, xdgDirectories()...)
}
/*
for each dir:
if len(config.Directories.AdditionalDirectories) != 0 {
additionalHomeDirs := homeDirectories(userHome, config.Directories.AdditionalDirectories)
directories = append(directories, additionalHomeDirs...)
}
- 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
*/
for _, dir := range slices.All(directories) {
if err := ensureDirectory(dir); err != nil {
return fmt.Errorf("unable to ensure that %s is present: %w", dir, err)
}
fmt.Printf("Successfully ensured %s is present.\n", dir)
}
return nil
}
func ensureDirectory(path string) error {
info, err := os.Stat(path);
info, err := os.Stat(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if err := os.Mkdir(path, dirModePerm); err != nil {
@ -60,7 +65,10 @@ func ensureDirectory(path string) error {
return nil
}
return fmt.Errorf("received an unexpected error after attempting to get the directory information: %w", err)
return fmt.Errorf(
"received an unexpected error after attempting to get the directory information: %w",
err,
)
}
if !info.IsDir() {
@ -75,3 +83,49 @@ func ensureDirectory(path string) error {
return nil
}
func defaultDirectories() []string {
return []string{
".local",
".local/bin",
"Applications",
"Documents",
"Downloads",
"Games",
"Git",
"Music",
"Pictures",
"Templates",
"Videos",
}
}
func homeDirectories(userHome string, directories []string) []string{
absolutePaths := make([]string, len(directories))
for ind := range slices.All(directories) {
absolutePaths[ind] = filepath.Join(userHome, directories[ind])
}
return absolutePaths
}
func xdgDirectories() []string {
xdgEnvVars := []string{
"XDG_DATA_HOME",
"XDG_CONFIG_HOME",
"XDG_CACHE_HOME",
"XDG_STATE_HOME",
}
directories := make([]string, 0)
for _, envVar := range slices.All(xdgEnvVars) {
directory := os.Getenv(envVar)
if directory != "" {
directories = append(directories, directory)
}
}
return directories
}

View file

@ -1,36 +1,40 @@
[alias]
br = branch
cm = commit
co = checkout
df = diff
gr = log --all --graph --decorate --format=format:'%C(bold "#62ccff")%h%C(reset) %C(bold "#fa9c43")(%ai)%C(reset) %C(auto)%d%C(reset)%n%C(italic "#929292")%an:%C(reset) %C("#ffffff")%s%C(reset)%n'
# mr allows you to checkout a merge request locally.
# $1 is the name of the remote
# $2 is the ID of the merge request
# e.g. git mr origin 3
# e.g. git mr upstream 5
mr = !sh -c 'git fetch $1 merge-requests/$2/head:mr-$1-$2 && git checkout mr-$1-$2' -
pl = pull
ps = push
sr = reset --soft
st = status
up = !sh -c 'git checkout $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@') && git fetch --all && git pull origin $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')'
sw = switch
rb = rebase
[commit]
gpgsign = true
template = $gitmessage_output
gpgsign = {{ .Git.GpgSign }}
template = {{ env "XDG_CONFIG_HOME" | printf "%s/git/message" }}
[core]
excludesFile = $gitignore_output
excludesFile = {{ env "XDG_CONFIG_HOME" | printf "%s/git/ignore" }}
[diff]
tool = vimdiff
[fetch]
prune = true
[gpg]
program = gpg2
program = gpg
[pull]
rebase = false
[user]
email = $git_user_email
name = $git_user_name
signingkey = $git_user_signingkey
email = {{ .Git.User.Email }}
name = {{ .Git.User.Name }}
{{- if .Git.GpgSign -}}
{{ print "" }}
signingkey = {{ .Git.User.SigningKey }}
{{- end -}}
{{ print "" }}
[init]
defaultBranch = main
[filter "lfs"]
required = true
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process

View file

@ -1,3 +0,0 @@
GIT_USER_NAME="Dan Anglin"
GIT_USER_EMAIL="daangling@gmail.com"
GIT_USER_SIGNINGKEY="0C1D44CFBEE68638"