#!/bin/zsh _DEPENDENCIES+=( psql ) _REQUIRED_ENV+=() source ${0:a:h}/common.zsh ##################################################################### _RUN_SQL_POSTGRES() { local _HOST _NAME _PASS _PORT _USER INPUT_FILE while [[ $# -gt 0 ]] do case $1 in --host | -h ) _HOST="$2"; shift 2 ;; --name | -d ) _NAME="$2"; shift 2 ;; --pass | -w ) _PASS="$2"; shift 2 ;; --port | -p ) _PORT="$2"; shift 2 ;; --user | -U ) _USER="$2"; shift 2 ;; --file | -i ) INPUT_FILE="$2"; shift 2 ;; * ) shift 1 ;; esac done [ ! $_HOST ] && _HOST=127.0.0.1 [ ! $_NAME ] && _NAME=postgres [ ! $_PORT ] && _PORT=5432 [ ! $_USER ] && _USER=postgres local SQL_DIR="$SCWRYPTS_DATA_PATH/sql" [ ! -d $SQL_DIR ] && mkdir -p $SQL_DIR cd $SQL_DIR [[ $(ls "*.sql" 2>&1 | wc -l) -eq 0 ]] && { __ERROR "you haven't made any SQL commands yet" __REMINDER "add '.sql' files here: '$SQL_DIR/'" exit 1 } [ ! $INPUT_FILE ] && INPUT_FILE=$(\ __FZF 'select a sql file to run' ) [ ! $INPUT_FILE ] && __ABORT [ ! -f $INPUT_FILE ] && { __FAIL 2 "no such sql file '$SQL_DIR/$INPUT_FILE'" } __STATUS "loading $INPUT_FILE preview..." _LESS $INPUT_FILE __STATUS "login : $_USER@$_HOST:$_PORT/$_NAME" __STATUS "command : ./$INPUT_FILE" __yN 'run this command?' || __ABORT __STATUS "running './$INPUT_FILE'" PGPASSWORD="$_PASS" psql \ -h $_HOST \ -p $_PORT \ -U $_USER \ -d $_NAME \ < $INPUT_FILE \ && __SUCCESS "finished running './$INPUT_FILE'" \ || __FAIL 3 "something went wrong running './$INPUT_FILE' (see above)" } ##################################################################### __WARNING __WARNING 'this function is in a beta state' __WARNING _RUN_SQL_POSTGRES $@