Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Last active August 7, 2021 00:20
Show Gist options
  • Save rummelonp/c252526b8f246888e6206dc9fb36fd74 to your computer and use it in GitHub Desktop.
Save rummelonp/c252526b8f246888e6206dc9fb36fd74 to your computer and use it in GitHub Desktop.
[WIP] sqldef の zsh 補完を作りたい
#compdef mysqldef psqldef mssqldef sqlite3def
# ------------------------------------------------------------------------------
# Copyright (c) 2021 Kazuya Takeshima
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for sqldef (https://github.com/k0kubun/sqldef).
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Kazuya Takeshima (https://github.com/mitukiii)
#
# ------------------------------------------------------------------------------
_mysqldef() {
_arguments \
': :_guard "^-*" "db_name"'
}
_psqldef() {
_arguments \
'(-U --user)'{-U,--user=}'[PostgreSQL user name (default: postgres)]:user:_psql_users' \
'(-W --password)'{-W,--password=}'[PostgreSQL user password, overridden by $PGPASSWORD]:password:' \
'(-H --host)'{-H,--host=}'[Host or socket directory to connect to the PostgreSQL server (default: 127.0.0.1)]:host:_hosts' \
'(-p --port)'{-p,--port=}'[Port used for the connection (default: 5432)]:port:' \
--password-prompt'[Force PostgreSQL user password prompt]' \
'(-f --file)'{-f,--file=}'[Read schema SQL from the file, rather than stdin (default: -)]:file:_files -g "*.sql"' \
--dry-run"[Don't run DDLs but just show them]" \
--export'[Just dump the current schema to stdout]' \
--skip-drop'[Skip destructive changes such as DROP]' \
'(- *)'--help'[Show this help]' \
'(- *)'--version'[Show this version]' \
': :_psql_databases'
}
_psql_users() {
local -a psql_users
psql_users=($(psql --user postgres --tuples-only --command 'SELECT rolname FROM pg_catalog.pg_roles WHERE rolcanlogin = true'))
_describe -t psql_users 'PostgreSQL user' psql_users
}
_psql_databases() {
local -a psql_databases
psql_databases=($(psql --user postgres --tuples-only --command "SELECT datname FROM pg_catalog.pg_database WHERE datname NOT LIKE 'template%'"))
_describe -t psql_databases 'PostgreSQL database' psql_databases
}
_mssqldef() {
_arguments \
': :_guard "^-*" "db_name"'
}
_sqlite3def() {
_arguments \
': :_guard "^-*" "db_name"'
}
_sqldef() {
case "$service" in
mysqldef) _mysqldef "$@";;
psqldef) _psqldef "$@";;
mssqldef) _mssqldef "$@";;
sqlite3def) _sqlite3def "$@";;
esac
}
_sqldef "$@"
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment