Files
dotfiles/gen

124 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Generates a new run, webapp, or script file.
if [ -z "$DEV_ENV" ]; then
echo "env var DEV_ENV needs to be present"
exit 1
fi
file=""
run="0"
webapp="0"
script="0"
while [[ $# -gt 0 ]]; do
echo "Arg: \"$1\""
if [[ "$1" == "run" ]]; then
run="1"
elif [[ "$1" == "webapp" ]]; then
webapp="1"
elif [[ "$1" == "script" ]]; then
script="1"
else
file="$1"
fi
shift
done
log() { echo "$1"; }
fail_if_exists() {
if [[ -f $1 ]]; then
log "file exists: $dest"
exit 1
fi
}
generate_run() {
local dest="$DEV_ENV/runs/$file"
fail_if_exists $dest
log "Creating new run: $dest"
cat >"$dest" <<'EOF'
#!/usr/bin/env bash
yay ${1:-"-S --noconfirm"} # packages
EOF
chmod +x $dest
}
generate_webapp() {
local dest="$DEV_ENV/env/webapps/$file"
# Check that the destination ends with '.json', fix if not.
if [[ ! $dest =~ \.json$ ]]; then
dest="$dest.json"
fi
fail_if_exists $dest
log "Creating new webapp: $dest"
cat >"$dest" <<'EOF'
{
"name": "My App",
"url": "https://example.com",
"icon": "https://icon.com"
}
EOF
}
generate_script() {
local dest="$DEV_ENV/env/.local/scripts/$file"
fail_if_exists $dest
log "Creating new script: $dest"
cat >"$dest" <<'EOF'
#!/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"}
# Logging utility function, use in place of echo.
log() {
logging log --source "$THIS_FILE" "$@"
}
################################################################################
# MAIN
################################################################################
# Setup logging file and label.
source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL"
log "Starting $THIS..."
EOF
chmod +x $dest
echo $dest
}
############################## MAIN ##############################
if [[ -z "$file" ]]; then
log "No file name supplied."
exit 1
fi
if [[ $run == "1" ]]; then
generate_run
elif [[ $webapp == "1" ]]; then
generate_webapp
elif [[ $script == "1" ]]; then
generate_script
else
log "Must supply either \"run\", \"webapp\", or \"script\" option."
exit 1
fi