From 9c88e62a6b2666b7a284232e8c27797b2333da80 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Wed, 11 Sep 2024 07:54:13 +0100 Subject: [PATCH] checkpoint: rendering the bash_profile file (not symlinked yet) --- bash/profile.gotmpl | 52 ++++++++++++++++++ hosts/falcon.json | 112 ++++++++++++++++++++++++++++++++++---- magefiles/bash_profile.go | 35 ++++++++++++ magefiles/config.go | 22 +++++++- 4 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 bash/profile.gotmpl create mode 100644 magefiles/bash_profile.go diff --git a/bash/profile.gotmpl b/bash/profile.gotmpl new file mode 100644 index 0000000..5822485 --- /dev/null +++ b/bash/profile.gotmpl @@ -0,0 +1,52 @@ +# If not running interactively, don't do anything +[[ $- != *i* ]] && return + +# Environment section + +## Session Paths - These will be added to PATH +{{- range $sessionPath := .BashProfile.SessionPaths -}} +{{- $fullPath := printf "%s/%s" (env "HOME") $sessionPath.Path -}} +{{ print "" }} +{{ print "" }} +### Add {{ $sessionPath.Description }} to PATH +if ! [[ "${PATH}" =~ {{ $fullPath }} ]]; then + export PATH={{ $fullPath}}:${PATH} +fi +{{- end -}} +{{ print "" }} +{{ print "" }} +## XDG Directories +{{ print "" }} +{{- range $key, $value := .BashProfile.XdgDirectories -}} +{{ print "" }} +export {{ $key }}="{{ $value }}" +{{- end -}} +{{ print "" }} +{{ print "" }} +## Extra Environment variables +{{ print "" }} +{{- range $key, $value := .BashProfile.EnvironmentVariables -}} +{{ print "" }} +export {{ $key }}="{{ $value }}" +{{- end -}} +{{ print "" }} +{{ print "" }} +# Aliases +{{ print "" }} +{{- range $key, $value := .BashProfile.Aliases -}} +{{ print "" }} +alias {{ $key }}="{{ $value }}" +{{- end -}} +{{ print "" }} +{{ print "" }} +# Prompt + +# External Sources + +# Commands +{{ print "" }} +{{- range $command := .BashProfile.Commands -}} +{{ print "" }} +## {{ $command.Description }} +{{ $command.Command }} +{{- end -}} diff --git a/hosts/falcon.json b/hosts/falcon.json index 71d6251..ebcc1e7 100644 --- a/hosts/falcon.json +++ b/hosts/falcon.json @@ -1,9 +1,110 @@ { + "applicationConfigurations": [ + "alacritty", + "amfora", + "git", + "lf", + "logrotate", + "tmux", + "user-dirs.dirs", + "user-dirs.locale", + "zk" + ], + "bashProfile": { + "manage": true, + "sessionPaths": [ + { + "path": ".local/goblin", + "description": "the directory of go binaries" + }, + { + "path": "Applications", + "description": "the AppImage directory" + }, + { + "path": ".local/scripts", + "description": "the directory of custom scripts" + } + ], + "xdgDirectories": { + "XDG_CACHE_HOME": "${HOME}/.local/cache", + "XDG_CONFIG_HOME": "${HOME}/.local/config", + "XDG_DATA_HOME": "${HOME}/.local/share", + "XDG_STATE_HOME": "${HOME}/.local/state" + }, + "environmentVariables": { + "LOG_HOME": "${XDG_STATE_HOME}/logs", + "DOCKER_CONFIG": "${XDG_CONFIG_HOME}/docker", + "GPG_TTY": "$(tty)", + "GNUPGHOME": "${XDG_DATA_HOME}/gnupg", + "GOPATH": "${XDG_DATA_HOME}/go", + "GOROOT": "${HOME}/.local/software/go", + "GOBIN": "${HOME}/.local/goblin", + "GOOS": "linux", + "GOARCH": "amd64", + "CGO_ENABLED": "0", + "MAGEFILE_CACHE": "${XDG_CACHE_HOME}/magefile", + "MAGEFILE_ENABLE_COLOR": "true", + "HISTFILE": "${XDG_STATE_HOME}/bash/history", + "HISTCONTROL": "ignoreboth", + "HISTFILESIZE": "10000", + "HISTSIZE": "1000", + "HISTTIMEFORMAT": "%d/%m/%y %T: ", + "KUBECONFIG": "${XDG_CONFIG_HOME}/kube/config", + "LESSHISTFILE": "${XDG_STATE_HOME}/less/history", + "MINIKUBE_HOME": "${XDG_DATA_HOME}/minikube", + "BROWSER": "firefox", + "EDITOR": "nvim", + "TERMINAL": "st", + "LANG": "en_GB.UTF-8", + "MANPAGER": "nvim +Man!", + "PULUMI_SKIP_UPDATE_CHECK": "true", + "PULUMI_HOME": "${XDG_DATA_HOME}/pulumi", + "RAD_HOME": "${XDG_DATA_HOME}/radicle", + "TERMINFO": "${XDG_DATA_HOME}/terminfo", + "TERMINFO_DIRS": "${TERMINFO}:/usr/share/terminfo", + "VAGRANT_HOME": "${XDG_DATA_HOME}/vagrant" + }, + "aliases": { + "ls": "ls --color=auto", + "ll": "ls -laF", + "la": "ls -A", + "l": "ls -CF", + "rm": "rm -i", + "mv": "mv -i", + "cp": "cp -i", + "grep": "grep --color=auto", + "fgrep": "fgrep --color=auto", + "egrep": "egrep --color=auto", + "systemctl": "sudo systemctl", + "journalctl": "sudo journalctl", + "pwgen": "pwgen -s -c -n", + "dc": "docker-compose", + "vim": "nvim", + "view": "nvim -R", + "vimdiff": "nvim -d", + "freeflow": "enbas --config-dir ${XDG_CONFIG_HOME}/enbas/free-flow", + "g": "git", + "k": "kubectl", + "pass": "PASSWORD_STORE_DIR=${XDG_DATA_HOME}/pass pass" + }, + "commands": [ + { + "command": "shopt -s histappend", + "description": "Append to the history file, don't overwrite it." + }, + { + "command": "set -o vi", + "description": "Activate vi mode." + } + ] + }, "directories": { "useDefaultDirectories": true, "includeXDGDirectories": true, "additionalDirectories": [ ".local/goblin", + ".local/share/go", ".local/software", "Certificates", "Docker", @@ -16,17 +117,6 @@ "Projects" ] }, - "applicationConfigurations": [ - "alacritty", - "amfora", - "git", - "lf", - "logrotate", - "tmux", - "user-dirs.dirs", - "user-dirs.locale", - "zk" - ], "git": { "gpgSign": true, "user": { diff --git a/magefiles/bash_profile.go b/magefiles/bash_profile.go new file mode 100644 index 0000000..e26bd5e --- /dev/null +++ b/magefiles/bash_profile.go @@ -0,0 +1,35 @@ +//go:build mage + +package main + +import ( + "fmt" + "text/template" +) + +// BashProfile manages the user's Bash Profile using their configuration and the Bash Profile template. +func BashProfile() error { + const ( + bashProfileTemplateFile string = "bash/profile.gotmpl" + managedBashProfile string = "managed/bash_profile" + ) + + config, err := newConfig() + if err != nil { + return fmt.Errorf("unable to load the configuration: %w", err) + } + + if !config.BashProfile.Manage { + return nil + } + + funcMap := template.FuncMap{ + "env": env, + } + + if err := renderTemplate(config, bashProfileTemplateFile, managedBashProfile, funcMap); err != nil { + return fmt.Errorf("unable to generate the Bash Profile: %w", err) + } + + return nil +} diff --git a/magefiles/config.go b/magefiles/config.go index b487c80..8cc16c3 100644 --- a/magefiles/config.go +++ b/magefiles/config.go @@ -11,8 +11,9 @@ import ( ) type config struct { - Directories configDirectories `json:"directories"` ApplicationConfigurations []string `json:"applicationConfigurations"` + BashProfile configBashProfile `json:"bashProfile"` + Directories configDirectories `json:"directories"` Git configGit `json:"git"` } @@ -33,6 +34,25 @@ type configGitUser struct { SigningKey string `json:"signingKey"` } +type configBashProfile struct { + Manage bool `json:"manage"` + 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()