diff --git a/config/config.json b/config/config.json deleted file mode 100644 index b3a1dc2..0000000 --- a/config/config.json +++ /dev/null @@ -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" - ] -} diff --git a/config/falcon.json b/config/falcon.json new file mode 100644 index 0000000..fcdb663 --- /dev/null +++ b/config/falcon.json @@ -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" + } + } +} diff --git a/config/sparrow.json b/config/sparrow.json new file mode 100644 index 0000000..a51deb5 --- /dev/null +++ b/config/sparrow.json @@ -0,0 +1,15 @@ +{ + "directories": { + "useDefaultDirectories": true, + "includeXDGDirectories": true, + "additionalDirectories": [], + }, + "git": { + "gpgSign": false, + "user": { + "email": "daangling@gmail.com", + "name": "Dan Anglin", + "signingKey": "" + } + } +} diff --git a/magefiles/config.go b/magefiles/config.go index dd444fb..587272a 100644 --- a/magefiles/config.go +++ b/magefiles/config.go @@ -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: "", + }, + }, + } +} diff --git a/magefiles/const.go b/magefiles/const.go new file mode 100644 index 0000000..5d4709d --- /dev/null +++ b/magefiles/const.go @@ -0,0 +1,8 @@ +//go:build mage + +package main + +const ( + configDir string = "./config" + rootManagedDir string = "./managed" +) diff --git a/magefiles/directories.go b/magefiles/directories.go index 1eacc74..a7ac4d4 100644 --- a/magefiles/directories.go +++ b/magefiles/directories.go @@ -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 +} diff --git a/unprocessed/git/gitconfig b/templates/git/config.gotmpl similarity index 56% rename from unprocessed/git/gitconfig rename to templates/git/config.gotmpl index 4ea6e79..bf1985a 100644 --- a/unprocessed/git/gitconfig +++ b/templates/git/config.gotmpl @@ -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 diff --git a/unprocessed/git/config/gitconfig-parameters b/unprocessed/git/config/gitconfig-parameters deleted file mode 100644 index dd8530e..0000000 --- a/unprocessed/git/config/gitconfig-parameters +++ /dev/null @@ -1,3 +0,0 @@ -GIT_USER_NAME="Dan Anglin" -GIT_USER_EMAIL="daangling@gmail.com" -GIT_USER_SIGNINGKEY="0C1D44CFBEE68638"