|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +die() |
| 4 | +{ |
| 5 | + local _ret=$2 |
| 6 | + test -n "$_ret" || _ret=1 |
| 7 | + test "$_PRINT_HELP" = yes && dbsynchelp >&2 |
| 8 | + echo "$1" >&2 |
| 9 | + exit ${_ret} |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +# Function that evaluates whether a value passed to it begins by a character |
| 14 | +# that is a short option of an argument the script knows about. |
| 15 | +# This is required in order to support getopts-like short options grouping. |
| 16 | +begins_with_short_option() |
| 17 | +{ |
| 18 | + local first_option all_short_options='fh' |
| 19 | + first_option="${1:0:1}" |
| 20 | + test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0 |
| 21 | +} |
| 22 | + |
| 23 | +# THE DEFAULTS INITIALIZATION - OPTIONALS |
| 24 | +_arg_full="off" |
| 25 | + |
| 26 | +dbsynchelp() { |
| 27 | + cat <<-HEREDOC |
| 28 | + Magento 2 DB Sync |
| 29 | +
|
| 30 | + Syncronize a database from production to staging, testing or development environments. |
| 31 | +
|
| 32 | + Usage: db-sync.sh [-f|--(no-)full] [-h|--help] |
| 33 | + Options: |
| 34 | + -f, --full, --no-full: full database dump (off by default) |
| 35 | + -h, --help: Prints help |
| 36 | +HEREDOC |
| 37 | +} |
| 38 | + |
| 39 | +# The parsing of the command-line |
| 40 | +parse_commandline() |
| 41 | +{ |
| 42 | + while test $# -gt 0 |
| 43 | + do |
| 44 | + _key="$1" |
| 45 | + case "$_key" in |
| 46 | + # The full argurment doesn't accept a value, |
| 47 | + # we expect the --full or -f, so we watch for them. |
| 48 | + -f|--no-full|--full) |
| 49 | + _arg_full="on" |
| 50 | + test "${1:0:5}" = "--no-" && _arg_full="off" |
| 51 | + ;; |
| 52 | + # We support getopts-style short arguments clustering, |
| 53 | + # so as -f doesn't accept value, other short options may be appended to it, so we watch for -f*. |
| 54 | + # After stripping the leading -f from the argument, we have to make sure |
| 55 | + # that the first character that follows coresponds to a short option. |
| 56 | + -f*) |
| 57 | + _arg_full="on" |
| 58 | + _next="${_key##-f}" |
| 59 | + if test -n "$_next" -a "$_next" != "$_key" |
| 60 | + then |
| 61 | + { begins_with_short_option "$_next" && shift && set -- "-f" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option." |
| 62 | + fi |
| 63 | + ;; |
| 64 | + # See the comment of option '--full' to see what's going on here - principle is the same. |
| 65 | + -h|--help) |
| 66 | + dbsynchelp |
| 67 | + exit 0 |
| 68 | + ;; |
| 69 | + # See the comment of option '-f' to see what's going on here - principle is the same. |
| 70 | + -h*) |
| 71 | + dbsynchelp |
| 72 | + exit 0 |
| 73 | + ;; |
| 74 | + *) |
| 75 | + _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1 |
| 76 | + ;; |
| 77 | + esac |
| 78 | + shift |
| 79 | + done |
| 80 | +} |
| 81 | + |
| 82 | +parse_commandline "$@" |
0 commit comments