diff --git a/env/.local/scripts/swift-dev b/env/.local/scripts/swift-dev new file mode 100755 index 0000000..ffa7bff --- /dev/null +++ b/env/.local/scripts/swift-dev @@ -0,0 +1,107 @@ +#!/usr/bin/env bash + +set -e +set -o nounset +set -o pipefail + +SCRIPTS=${SCRIPTS:-$HOME/.local/scripts} +THIS_FILE=${BASH_SOURCE[0]} +LOG_LABEL=$(basename "$THIS_FILE") +THIS=${THIS:-$LOG_LABEL} +LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"} + +XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config} +XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share} +IMAGE="git.housh.dev/michael/swift-dev-container" +IMAGE_TAG="latest" + +declare cmd dir pull_flag run_flag +cmd="" +dir="" +pull_flag="0" +run_flag="1" + +usage() { + cat < -- + +FLAGS: + -h | --help: Show this help page. + -p | --pull: Used with run command, to pull/update image before running. + -t | --tag : Used with run command to specify the image tag to use. + +COMMANDS: + pull : Pull the dev container image, tag is optional (default; 'latest'). + run (default): Run the dev container in the directory, if directory not supplied it + will default to PWD. + +NOTES: + Run command is implied / default, so it's not required to be passed if you just want to run the + container. For example in your swift project directory you can run 'swift-dev' and a container + will start. + + This will properly mount '~/.config/nvim' and '~/.local/share/nvim' inside the container. + + If you would like to run a custom command / not run neovim in the container then it MUST follow + '--'. For example, 'swift-dev -- /bin/bash'. + +EOF +} + +# Logging utility function, use in place of echo. +log() { + logging log --source "$THIS_FILE" "$@" +} + +pull() { + log "Pulling image: '$IMAGE:$IMAGE_TAG'" + podman pull "$IMAGE:$IMAGE_TAG" +} + +run() { + [[ -z $dir ]] && dir="$PWD" + [[ -z $cmd ]] && cmd="/root/.local/share/bob/nvim-bin/nvim" + log "Running '$IMAGE:$IMAGE_TAG', in: '$dir'" + podman run -it --rm \ + --volume "$XDG_CONFIG_HOME/nvim":/root/.config/nvim \ + --volume "$XDG_DATA_HOME/nvim":/root/.local/share/nvim \ + --volume "$dir":/root/dev \ + "$IMAGE:$IMAGE_TAG" "$cmd" +} + +################################################################################ +# MAIN +################################################################################ + +# Setup logging file and label. +source "$SCRIPTS/hypr/logging" +setup-logging "$LOG_FILE" "$LOG_LABEL" + +while [[ $# -gt 0 ]]; do + if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then + usage && exit 0 + elif [[ $1 == "-p" ]] || [[ $1 == "--pull" ]]; then + pull_flag="1" + elif [[ $1 == "-t" ]] || [[ $1 == "--tag" ]]; then + [[ -n ${2:-""} ]] || (log --error "Must supply a tag" && exit 1) + IMAGE_TAG=$2 + shift + elif [[ $1 == "pull" ]]; then + run_flag="0" + pull_flag="1" + elif [[ $1 == "--" ]]; then + shift + cmd="$*" + elif [[ ! $1 == "run" ]]; then + dir="$1" + fi + shift +done + +[[ $pull_flag = "1" ]] && pull +[[ $run_flag = "1" ]] && run