-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake
executable file
·118 lines (94 loc) · 2.56 KB
/
fake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
set -euo pipefail
# -----------------------------------------------------------------------------
# CONFIG
# -----------------------------------------------------------------------------
__VERSION="0.1"
__NAME="Faker in Bash"
__FILE=$(basename "$0")
__DIR=$(dirname "$0")
readonly __VERSION __FILE __NAME __DIR
o_locale=en
# -----------------------------------------------------------------------------
# PRIVATE interfaces
# -----------------------------------------------------------------------------
function _lorem {
readarray -t ents < "$__DIR/locale/$o_locale"/lorem.txt
declare -a words
i=0
while [ "$i" -lt "${1:-1}" ]; do
rand=$(perl -e "print int(rand(${#ents[@]}))")
words+=("${ents["$rand"]}")
((i += 1))
done
printf "%s" "${words[*]}"
}
function _firstname {
readarray -t ents < "$__DIR/locale/$o_locale"/first-name.txt
rand=$(perl -e "print int(rand(${#ents[@]}))")
echo "${ents["$rand"]}"
}
function _lastname {
readarray -t ents < "$__DIR/locale/$o_locale"/last-name.txt
rand=$(perl -e "print int(rand(${#ents[@]}))")
echo "${ents["$rand"]}"
}
function _email {
readarray -t ents < "$__DIR/locale/$o_locale"/email.txt
rand=$(perl -e "print int(rand(${#ents[@]}))")
prefix=$(_firstname)$(_lastname)
echo "${prefix,,}@${ents["$rand"]}"
}
# -----------------------------------------------------------------------------
# PUBLIC interfaces
# -----------------------------------------------------------------------------
function lorem { # Loremipsum run command: lorem 5 - to get 5 words
local cnt
if [[ ${1:-1} =~ ^[0-9]+ ]]; then
cnt="${1:-1}"
fi
_lorem "$cnt"
}
function name { # Firstname + Lastname
echo "$(_firstname)" "$(_lastname)"
}
function firstname { # Firstname
_firstname
}
function lastname { # Firstname
_lastname
}
function email { # E-Mail address
_email
}
# -----------------------------------------------------------------------------
function _parseOptions {
while [[ $# -ge 1 ]]; do
case $1 in
--locale)
shift
o_locale=$1
shift
continue
;;
esac
if [[ $# -gt 0 ]]; then
shift
fi
done
}
function help { # Display this helpful help information
local functions
functions=$(grep -E "^function [^_]*?$" "$0" | sed -E "s/ *{ *# */#/g" | column -t -s '#' | sed -E "s/^function / /g" | sort)
echo "$__NAME v $__VERSION"
echo "Usage: $__FILE <task> [args]"
echo ""
echo "Tasks:"
echo "$functions"
echo ""
echo "Options:"
echo " --locale en"
echo ""
}
_parseOptions "$@"
"${@:-help}"