Skip to content

Commit 6295baa

Browse files
committed
add is.sh library functions
1 parent 5e7c1b2 commit 6295baa

File tree

14 files changed

+650
-0
lines changed

14 files changed

+650
-0
lines changed

examples/is/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Is Example
2+
==================================================
3+
4+
This example was generated with:
5+
6+
$ bashly init
7+
$ bashly add is
8+
$ bashly generate

examples/is/exist

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
#!/usr/bin/env bash
2+
# This script was generated by bashly (https://github.com/DannyBen/bashly)
3+
# Modifying it manually is not recommended
4+
5+
# :command.root_command
6+
root_command() {
7+
# :src/root_command.sh
8+
file=${args[filename]}
9+
10+
if is existing "$file" ; then
11+
echo "File exists"
12+
13+
if is writeable "$file" ; then
14+
echo "... and is writeable"
15+
else
16+
echo "... and is NOT writeable"
17+
fi
18+
19+
else
20+
echo "File does not exist"
21+
22+
fi
23+
}
24+
25+
# :command.version_command
26+
version_command() {
27+
echo "$version"
28+
}
29+
30+
# :command.usage
31+
exist_usage() {
32+
if [[ -n $long_usage ]]; then
33+
printf "exist - Sample application that uses the is.sh functions\n"
34+
echo
35+
else
36+
printf "exist - Sample application that uses the is.sh functions\n"
37+
echo
38+
fi
39+
40+
printf "Usage:\n"
41+
printf " exist FILENAME\n"
42+
printf " exist --help | -h\n"
43+
printf " exist --version | -v\n"
44+
echo
45+
46+
if [[ -n $long_usage ]]; then
47+
printf "Options:\n"
48+
# :command.usage_fixed_flags
49+
echo " --help, -h"
50+
printf " Show this help\n"
51+
echo
52+
echo " --version, -v"
53+
printf " Show version number\n"
54+
echo
55+
56+
# :command.usage_args
57+
printf "Arguments:\n"
58+
59+
# :argument.usage
60+
echo " FILENAME"
61+
printf " The file to check if exists\n"
62+
echo
63+
64+
# :command.usage_examples
65+
printf "Examples:\n"
66+
67+
printf " exist somefile\n"
68+
echo
69+
70+
fi
71+
}
72+
73+
# :command.inspect_args
74+
inspect_args() {
75+
echo args:
76+
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
77+
}
78+
79+
# :command.user_lib
80+
# :src/lib/is.sh
81+
#!/bin/bash
82+
#
83+
# Copyright (c) 2016 Józef Sokołowski
84+
# Distributed under the MIT License
85+
#
86+
# For most current version checkout repository:
87+
# https://github.com/qzb/is.sh
88+
#
89+
90+
is() {
91+
if [ "$1" == "--help" ]; then
92+
cat << EOF
93+
Conditions:
94+
is equal VALUE_A VALUE_B
95+
is matching REGEXP VALUE
96+
is substring VALUE_A VALUE_B
97+
is empty VALUE
98+
is number VALUE
99+
is gt NUMBER_A NUMBER_B
100+
is lt NUMBER_A NUMBER_B
101+
is ge NUMBER_A NUMBER_B
102+
is le NUMBER_A NUMBER_B
103+
is file PATH
104+
is dir PATH
105+
is link PATH
106+
is existing PATH
107+
is readable PATH
108+
is writeable PATH
109+
is executable PATH
110+
is available COMMAND
111+
is older PATH_A PATH_B
112+
is newer PATH_A PATH_B
113+
is true VALUE
114+
is false VALUE
115+
116+
Negation:
117+
is not equal VALUE_A VALUE_B
118+
119+
Optional article:
120+
is not a number VALUE
121+
is an existing PATH
122+
is the file PATH
123+
EOF
124+
exit
125+
fi
126+
127+
if [ "$1" == "--version" ]; then
128+
echo "is.sh 1.1.0"
129+
exit
130+
fi
131+
132+
local condition="$1"
133+
local value_a="$2"
134+
local value_b="$3"
135+
136+
if [ "$condition" == "not" ]; then
137+
shift 1
138+
! is "${@}"
139+
return $?
140+
fi
141+
142+
if [ "$condition" == "a" ] || [ "$condition" == "an" ] || [ "$condition" == "the" ]; then
143+
shift 1
144+
is "${@}"
145+
return $?
146+
fi
147+
148+
case "$condition" in
149+
file)
150+
[ -f "$value_a" ]; return $?;;
151+
dir|directory)
152+
[ -d "$value_a" ]; return $?;;
153+
link|symlink)
154+
[ -L "$value_a" ]; return $?;;
155+
existent|existing|exist|exists)
156+
[ -e "$value_a" ]; return $?;;
157+
readable)
158+
[ -r "$value_a" ]; return $?;;
159+
writeable)
160+
[ -w "$value_a" ]; return $?;;
161+
executable)
162+
[ -x "$value_a" ]; return $?;;
163+
available|installed)
164+
which "$value_a"; return $?;;
165+
empty)
166+
[ -z "$value_a" ]; return $?;;
167+
number)
168+
echo "$value_a" | grep -E '^[0-9]+(\.[0-9]+)?$'; return $?;;
169+
older)
170+
[ "$value_a" -ot "$value_b" ]; return $?;;
171+
newer)
172+
[ "$value_a" -nt "$value_b" ]; return $?;;
173+
gt)
174+
is not a number "$value_a" && return 1;
175+
is not a number "$value_b" && return 1;
176+
awk "BEGIN {exit $value_a > $value_b ? 0 : 1}"; return $?;;
177+
lt)
178+
is not a number "$value_a" && return 1;
179+
is not a number "$value_b" && return 1;
180+
awk "BEGIN {exit $value_a < $value_b ? 0 : 1}"; return $?;;
181+
ge)
182+
is not a number "$value_a" && return 1;
183+
is not a number "$value_b" && return 1;
184+
awk "BEGIN {exit $value_a >= $value_b ? 0 : 1}"; return $?;;
185+
le)
186+
is not a number "$value_a" && return 1;
187+
is not a number "$value_b" && return 1;
188+
awk "BEGIN {exit $value_a <= $value_b ? 0 : 1}"; return $?;;
189+
eq|equal)
190+
[ "$value_a" = "$value_b" ] && return 0;
191+
is not a number "$value_a" && return 1;
192+
is not a number "$value_b" && return 1;
193+
awk "BEGIN {exit $value_a == $value_b ? 0 : 1}"; return $?;;
194+
match|matching)
195+
echo "$value_b" | grep -xE "$value_a"; return $?;;
196+
substr|substring)
197+
echo "$value_b" | grep -F "$value_a"; return $?;;
198+
true)
199+
[ "$value_a" == true ] || [ "$value_a" == 0 ]; return $?;;
200+
false)
201+
[ "$value_a" != true ] && [ "$value_a" != 0 ]; return $?;;
202+
esac > /dev/null
203+
204+
return 1
205+
}
206+
207+
# :command.command_functions
208+
209+
# :command.parse_requirements
210+
parse_requirements() {
211+
# :command.fixed_flag_filter
212+
case "$1" in
213+
--version | -v )
214+
version_command
215+
exit
216+
;;
217+
218+
--help | -h )
219+
long_usage=yes
220+
exist_usage
221+
exit 1
222+
;;
223+
224+
esac
225+
# :command.environment_variables_filter
226+
# :command.dependencies_filter
227+
# :command.command_filter
228+
action="root"
229+
# :command.required_args_filter
230+
if [[ $1 && $1 != -* ]]; then
231+
args[filename]=$1
232+
shift
233+
else
234+
printf "missing required argument: FILENAME\nusage: exist FILENAME\n"
235+
exit 1
236+
fi
237+
# :command.required_flags_filter
238+
# :command.parse_requirements_while
239+
while [[ $# -gt 0 ]]; do
240+
key="$1"
241+
case "$key" in
242+
243+
-* )
244+
printf "invalid option: %s\n" "$key"
245+
exit 1
246+
;;
247+
248+
* )
249+
# :command.parse_requirements_case
250+
if [[ ! ${args[filename]} ]]; then
251+
args[filename]=$1
252+
shift
253+
else
254+
printf "invalid argument: %s\n" "$key"
255+
exit 1
256+
fi
257+
;;
258+
259+
esac
260+
done
261+
# :command.default_assignments
262+
}
263+
264+
# :command.initialize
265+
initialize() {
266+
version="0.1.0"
267+
long_usage=''
268+
set -e
269+
270+
# :src/initialize.sh
271+
# Code here runs inside the initialize() function
272+
# Use it for anything that you need to run before any other function, like
273+
# setting environment vairables:
274+
# CONFIG_FILE=settings.ini
275+
#
276+
# Feel free to empty (but not delete) this file.
277+
}
278+
279+
# :command.run
280+
run() {
281+
declare -A args
282+
parse_requirements "$@"
283+
284+
if [[ ${args[--version]} ]]; then
285+
version_command
286+
elif [[ ${args[--help]} ]]; then
287+
long_usage=yes
288+
exist_usage
289+
elif [[ $action == "root" ]]; then
290+
root_command
291+
fi
292+
}
293+
294+
initialize
295+
run "$@"

examples/is/src/bashly.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: exist
2+
help: Sample application that uses the is.sh functions
3+
version: 0.1.0
4+
5+
args:
6+
- name: filename
7+
required: true
8+
help: The file to check if exists
9+
10+
examples:
11+
- exist somefile

examples/is/src/initialize.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Code here runs inside the initialize() function
2+
# Use it for anything that you need to run before any other function, like
3+
# setting environment vairables:
4+
# CONFIG_FILE=settings.ini
5+
#
6+
# Feel free to empty (but not delete) this file.

0 commit comments

Comments
 (0)