Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/is/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Is Example
==================================================

This example was generated with:

$ bashly init
$ bashly add is
$ bashly generate
291 changes: 291 additions & 0 deletions examples/is/exist
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
#!/usr/bin/env bash
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended

# :command.root_command
root_command() {
# :src/root_command.sh
file=${args[filename]}

if is existing "$file" ; then
echo "File exists"

if is writeable "$file" ; then
echo "... and is writeable"
else
echo "... and is NOT writeable"
fi

else
echo "File does not exist"

fi
}

# :command.version_command
version_command() {
echo "$version"
}

# :command.usage
exist_usage() {
if [[ -n $long_usage ]]; then
printf "exist - Sample application that uses the is.sh functions\n"
echo
else
printf "exist - Sample application that uses the is.sh functions\n"
echo
fi

printf "Usage:\n"
printf " exist FILENAME\n"
printf " exist --help | -h\n"
printf " exist --version | -v\n"
echo

if [[ -n $long_usage ]]; then
printf "Options:\n"
# :command.usage_fixed_flags
echo " --help, -h"
printf " Show this help\n"
echo
echo " --version, -v"
printf " Show version number\n"
echo

# :command.usage_args
printf "Arguments:\n"

# :argument.usage
echo " FILENAME"
printf " The file to check if exists\n"
echo

# :command.usage_examples
printf "Examples:\n"

printf " exist somefile\n"
echo

fi
}

# :command.inspect_args
inspect_args() {
echo args:
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
}

# :command.user_lib
# :src/lib/is.sh
# is.sh copyright notice:
# Copyright (c) 2016 Józef Sokołowski
# Distributed under the MIT License
# For most current version checkout repository:
# https://github.com/qzb/is.sh
is() {
if [ "$1" == "--help" ]; then
cat << EOF
Conditions:
is equal VALUE_A VALUE_B
is matching REGEXP VALUE
is substring VALUE_A VALUE_B
is empty VALUE
is number VALUE
is gt NUMBER_A NUMBER_B
is lt NUMBER_A NUMBER_B
is ge NUMBER_A NUMBER_B
is le NUMBER_A NUMBER_B
is file PATH
is dir PATH
is link PATH
is existing PATH
is readable PATH
is writeable PATH
is executable PATH
is available COMMAND
is older PATH_A PATH_B
is newer PATH_A PATH_B
is true VALUE
is false VALUE

Negation:
is not equal VALUE_A VALUE_B

Optional article:
is not a number VALUE
is an existing PATH
is the file PATH
EOF
exit
fi

if [ "$1" == "--version" ]; then
echo "is.sh 1.1.0"
exit
fi

local condition="$1"
local value_a="$2"
local value_b="$3"

if [ "$condition" == "not" ]; then
shift 1
! is "${@}"
return $?
fi

if [ "$condition" == "a" ] || [ "$condition" == "an" ] || [ "$condition" == "the" ]; then
shift 1
is "${@}"
return $?
fi

case "$condition" in
file)
[ -f "$value_a" ]; return $?;;
dir|directory)
[ -d "$value_a" ]; return $?;;
link|symlink)
[ -L "$value_a" ]; return $?;;
existent|existing|exist|exists)
[ -e "$value_a" ]; return $?;;
readable)
[ -r "$value_a" ]; return $?;;
writeable)
[ -w "$value_a" ]; return $?;;
executable)
[ -x "$value_a" ]; return $?;;
available|installed)
which "$value_a"; return $?;;
empty)
[ -z "$value_a" ]; return $?;;
number)
echo "$value_a" | grep -E '^[0-9]+(\.[0-9]+)?$'; return $?;;
older)
[ "$value_a" -ot "$value_b" ]; return $?;;
newer)
[ "$value_a" -nt "$value_b" ]; return $?;;
gt)
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a > $value_b ? 0 : 1}"; return $?;;
lt)
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a < $value_b ? 0 : 1}"; return $?;;
ge)
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a >= $value_b ? 0 : 1}"; return $?;;
le)
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a <= $value_b ? 0 : 1}"; return $?;;
eq|equal)
[ "$value_a" = "$value_b" ] && return 0;
is not a number "$value_a" && return 1;
is not a number "$value_b" && return 1;
awk "BEGIN {exit $value_a == $value_b ? 0 : 1}"; return $?;;
match|matching)
echo "$value_b" | grep -xE "$value_a"; return $?;;
substr|substring)
echo "$value_b" | grep -F "$value_a"; return $?;;
true)
[ "$value_a" == true ] || [ "$value_a" == 0 ]; return $?;;
false)
[ "$value_a" != true ] && [ "$value_a" != 0 ]; return $?;;
esac > /dev/null

return 1
}

# :command.command_functions

# :command.parse_requirements
parse_requirements() {
# :command.fixed_flag_filter
case "$1" in
--version | -v )
version_command
exit
;;

--help | -h )
long_usage=yes
exist_usage
exit 1
;;

esac
# :command.environment_variables_filter
# :command.dependencies_filter
# :command.command_filter
action="root"
# :command.required_args_filter
if [[ $1 && $1 != -* ]]; then
args[filename]=$1
shift
else
printf "missing required argument: FILENAME\nusage: exist FILENAME\n"
exit 1
fi
# :command.required_flags_filter
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in

-* )
printf "invalid option: %s\n" "$key"
exit 1
;;

* )
# :command.parse_requirements_case
if [[ ! ${args[filename]} ]]; then
args[filename]=$1
shift
else
printf "invalid argument: %s\n" "$key"
exit 1
fi
;;

esac
done
# :command.default_assignments
}

# :command.initialize
initialize() {
version="0.1.0"
long_usage=''
set -e

# :src/initialize.sh
# Code here runs inside the initialize() function
# Use it for anything that you need to run before any other function, like
# setting environment vairables:
# CONFIG_FILE=settings.ini
#
# Feel free to empty (but not delete) this file.
}

# :command.run
run() {
declare -A args
parse_requirements "$@"

if [[ ${args[--version]} ]]; then
version_command
elif [[ ${args[--help]} ]]; then
long_usage=yes
exist_usage
elif [[ $action == "root" ]]; then
root_command
fi
}

initialize
run "$@"
11 changes: 11 additions & 0 deletions examples/is/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: exist
help: Sample application that uses the is.sh functions
version: 0.1.0

args:
- name: filename
required: true
help: The file to check if exists

examples:
- exist somefile
6 changes: 6 additions & 0 deletions examples/is/src/initialize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Code here runs inside the initialize() function
# Use it for anything that you need to run before any other function, like
# setting environment vairables:
# CONFIG_FILE=settings.ini
#
# Feel free to empty (but not delete) this file.
Loading