|
#!/usr/bin/env bash |
|
|
|
help=" |
|
run command with specify sscript and version |
|
|
|
## Requirement |
|
1. docker |
|
2. run script file |
|
3. test script files (optional) |
|
|
|
## Usage |
|
run by './run' 'argument', if wrong you might didn't give permission to it. |
|
|
|
### Argument |
|
First argument should be one of list |
|
1. t | test |
|
2. c | cov | coverage |
|
3. custom | command |
|
Next n argument script will passing by keyword |
|
|
|
#### Accept keyword |
|
1. input file |
|
- f | file | with | of |
|
2. input script type |
|
- i | in |
|
3. input script version |
|
- v | version |
|
4. input custom command (for custom action only) |
|
- c | command |
|
- w | which |
|
- wi | which_is | which-is |
|
|
|
#### Note |
|
1. version should valid in kamontat/shell-test in docker hub |
|
- cut of script name out |
|
- link: https://hub.docker.com/r/kamontat/shell-test/tags/ |
|
2. input command **MUST** be at latest of the script |
|
|
|
### Example |
|
run test of download file in bash-3.0 |
|
- run test of download in bash version 3.0 |
|
|
|
run test of download file in bash-3.0 |
|
- run t in bash v 3.0 file download |
|
|
|
run coverage of download file in zsh-5.3.1-r0 |
|
- run cov with download in zsh v 5.3.1-r0 |
|
|
|
run bash version in bash-4.4.12 |
|
- run command which-is bash --version |
|
|
|
# Who wrote |
|
Kamontat Chantrachirathumrong (github.com/kamontat) |
|
" |
|
|
|
# run test file download in bash version 3.0 |
|
# run test in bash version 3.0 file download |
|
|
|
# run coverage file download in bash version 3.0 |
|
# run coverage in bash version 3.0 file download |
|
|
|
# run custom in bash version 3.0 command bash --version |
|
# run command in bash version 3.0 which_is bash --version |
|
|
|
keyword_help=("help" "h" "-h" "--help") |
|
|
|
keyword_test=("t" "test") |
|
keyword_cov=("c" "cov" "coverage") |
|
keyword_custom=("custom" "command") |
|
|
|
keyword_file=("f" "file" "with" "of") |
|
keyword_in=("i" "in") |
|
keyword_version=("v" "version") |
|
keyword_command=("which" "which_is" "which-is" "w" "wi" "c" "command") |
|
|
|
action= |
|
file= |
|
script_name="bash" |
|
script_version="latest" |
|
run_command=() |
|
|
|
command -v docker &>/dev/null || exit 1 |
|
|
|
# required 2 parameters |
|
# 1) text - input text |
|
# 2) option - option to check |
|
# return boolean |
|
is_match() { |
|
local text="$1" |
|
shift |
|
for i in "$@"; do |
|
[[ "$text" == "$i" ]] && return 0 |
|
done |
|
return 1 |
|
} |
|
|
|
if is_match "$1" "${keyword_test[@]}"; then |
|
action="test" |
|
shift |
|
fi |
|
|
|
if is_match "$1" "${keyword_cov[@]}"; then |
|
action="shellcheck" |
|
shift |
|
fi |
|
|
|
if is_match "$1" "${keyword_custom[@]}"; then |
|
action="custom" |
|
shift |
|
fi |
|
|
|
if is_match "$1" "${keyword_help[@]}"; then |
|
echo "$help" |
|
exit |
|
fi |
|
|
|
[ -z $action ] && exit 2 |
|
|
|
until [ -z "$1" ]; do |
|
if is_match "$1" "${keyword_file[@]}"; then |
|
file="$2" |
|
[ -f "$file" ] || exit 10 # not file |
|
shift 2 |
|
elif is_match "$1" "${keyword_in[@]}"; then |
|
script_name="$2" |
|
[[ $action == "test" ]] && action="$script_name" |
|
shift 2 |
|
elif is_match "$1" "${keyword_version[@]}"; then |
|
script_version="$2" |
|
shift 2 |
|
elif is_match "$1" "${keyword_command[@]}"; then |
|
shift |
|
run_command=("$@") |
|
break |
|
else |
|
echo "unknown key ($1) accept [file|in|version|...]" |
|
exit 5 |
|
fi |
|
done |
|
|
|
v="" |
|
[ -f "$file" ] && v="-v $(realpath "$file")":/test |
|
|
|
[[ $action == "custom" ]] && action="" |
|
|
|
# echo "action: $action" |
|
# echo "file: $file" |
|
# echo "script: $script_name - $script_version" |
|
# echo "custom command: ${run_command[*]}" |
|
# exit 155 |
|
|
|
docker_command="docker run --entrypoint=$action --rm -it $v kamontat/shell-test:$script_name-$script_version" |
|
|
|
if [[ $action == "" ]]; then |
|
$docker_command "${run_command[@]}" |
|
else |
|
$docker_command test |
|
fi |