Skip to content

Instantly share code, notes, and snippets.

@FYHenry
Last active May 29, 2024 12:25
Show Gist options
  • Save FYHenry/b041bc3d9e206dfd54f8feeaf80dd731 to your computer and use it in GitHub Desktop.
Save FYHenry/b041bc3d9e206dfd54f8feeaf80dd731 to your computer and use it in GitHub Desktop.
write-sql-file
# -*- shell-script -*-
_write_sql_file()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="--help --version --name --major --minor --directory"
if [[ "${cur}" = -* ]]
then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -F _write_sql_file write-sql-file
#!/bin/sh
if [ $# -eq 0 ]
then
echo 'Need some --help ?'
exit 0
fi
if [ $# -eq 1 ] && [ "$1" = '-h' ] || [ "$1" = '--help' ]
then
echo "Tool to write complete versioned SQL file name.
${0} --name NAME [--major MAJOR] [--minor MINOR] [--directory DIRECTORY]
${0} [--major MAJOR] [--minor MINOR] [--directory DIRECTORY] -- NAME
--name NAME Name core of SQL file.
--major MAJOR Number for major version.
--minor MINOR Number for minor version.
--directory DIRECTORY Directory path where the SQL file will be
written. Without directory, the file name will be shown in standard output.
Without core name defined the standard input is used to define it."
echo 'File name format : V${MAJOR}_${MINOR}_${TIMESTAMP}__${NAME}.sql'
exit 0
fi
if [ $# -eq 1 ] && [ "$1" = '-V' ] || [ "$1" = '--version' ]
then
echo '1.1'
exit 0
fi
# show-filename NAME MAJOR MINOR DIRECTORY
showfilename() {
NAME="$1"
MAJOR="$2"
MINOR="$3"
DIRECTORY="$4"
if [ -z "$4" ]
then
echo "V${MAJOR:=0}_${MINOR:=0}_$( date '+%s' )__${NAME}.sql"
else
true >"${DIRECTORY}/V${MAJOR:=0}_${MINOR:=0}_$( date '+%s' )__${NAME}.sql"
fi
}
name=
directory=
major=
minor=
while [ $# -gt 0 ]
do
case $1 in
(-n|--name)
if [ "$name" ]
then
echo 'Too many names !'
exit 1
fi
if [ -z "$2" ]
then
echo 'Without name value !'
exit 1
fi
name="$2"
shift 2;;
(-d|--directory)
if [ "$directory" ]
then
echo 'Too many directory paths !'
exit 1
fi
if [ -z "$2" ]
then
echo 'No directory path value !'
exit 1
fi
directory="$2"
shift 2;;
(-M|--major)
if [ "$major" ]
then
echo 'Too many major versions !'
exit 1
fi
if [ -z "$2" ]
then
echo 'Without major version value !'
exit 1
fi
major="$2"
shift 2;;
(-m|--minor)
if [ "$minor" ]
then
echo 'Too many minor versions !'
exit 1
fi
if [ -z "$2" ]
then
echo 'Without minor version value !'
exit 1
fi
minor="$2"
shift 2;;
(--)
if [ "$name" ]
then
echo 'Too many names!'
exit 1
fi
if [ $# -ne 2 ]
then
echo 'Only as last argument !'
exit 1
fi
name="$2"
shift 2;;
(*)
echo "Bad argument!"
exit 1;;
esac
done
if [ "$name" ]
then
showfilename "$name" "$major" "$minor" "$directory"
else
if [ -t 0 ]
then
echo 'Name value missing !'
exit 1
else
read -r name
showfilename "$name" "$major" "$minor" "$directory"
fi
fi
@FYHenry
Copy link
Author

FYHenry commented Nov 4, 2023

Installation

Do alias as write-sql-file for write-sql-file.sh

Copy write-sql-file file in auto-completion directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment