checkpoint: manage home directories

- ensure configured home directories are present and their mode set to
  0700
- move dotfiles to files
- added JSON config
This commit is contained in:
Dan Anglin 2024-09-09 18:02:53 +01:00
parent ba0a415b03
commit 903e5d8b66
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
49 changed files with 197 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
config/git/config/*
!config/git/config/.gitkeep
!config/git/config/README.md
managed/*
!managed/.gitkeep

27
config/config.json Normal file
View file

@ -0,0 +1,27 @@
{
"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"
]
}

24
files/bash/environment Normal file
View file

@ -0,0 +1,24 @@
## vim: ft=sh :
# == Add personal bin directory to PATH
if ! [[ "${PATH}" =~ "${HOME}/.local/bin" ]]; then
export PATH=${HOME}/.local/bin:${PATH}
fi
# == Add custom scripts directory to PATH
if ! [[ "${PATH}" =~ "${HOME}/.local/scripts" ]]; then
export PATH=${HOME}/.local/scripts:${PATH}
fi
# == Add AppImage directory to PATH
if ! [[ "${PATH}" =~ "${HOME}/Applications" ]]; then
export PATH=${HOME}/Applications:${PATH}
fi
# == The XDG Directories
export XDG_CONFIG_HOME=${HOME}/.local/config
export XDG_DATA_HOME=${HOME}/.local/share
export XDG_CACHE_HOME=${HOME}/.local/cache
export XDG_STATE_HOME=${HOME}/.local/state
export LOG_HOME="${XDG_STATE_HOME}/logs"

View file

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

11
files/zk/config.toml Normal file
View file

@ -0,0 +1,11 @@
[note]
default-title = "Note"
extension = "md"
filename = "{{date now '%Y%m%d%H%M%S'}}-{{slug title}}"
language = "en"
[extra]
author = "dananglin"
[tool]
editor = "nvim"

37
magefiles/config.go Normal file
View file

@ -0,0 +1,37 @@
//go:build mage
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
const (
configDir string = "./config/"
configFileName string = "config.json"
)
type config struct {
Directories []string `json:"directories"`
}
func newConfig() (config, error) {
var cfg config
path := filepath.Join(configDir, configFileName)
file, err := os.Open(path)
if err != nil {
return cfg, 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 cfg, nil
}

77
magefiles/directories.go Normal file
View file

@ -0,0 +1,77 @@
//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
}

3
magefiles/go.mod Normal file
View file

@ -0,0 +1,3 @@
module codeflow.dananglin.me.uk/linux-home/manager/magefiles
go 1.23.1

13
magefiles/main.go Normal file
View file

@ -0,0 +1,13 @@
//go:build ignore
package main
import (
"os"
"github.com/magefile/mage/mage"
)
func main() {
os.Exit(mage.Main())
}

0
managed/.gitkeep Normal file
View file