-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilerplate.sh
123 lines (108 loc) · 2.61 KB
/
boilerplate.sh
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
119
120
121
122
123
#!/bin/bash
# boilerplate.sh
# The greatest script that does nothing.
#
# How to use:
# Export the following variables or pass them as parameters.
# $ ./boilerplate.sh \
# <MESSAGE>
#
# Example:
# $ ./boilerplate.sh \
# "hello, friend"
#
# NOTE:
# this script does nothing.
# START
# Constants
declare -r SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
declare -r TEMP_MESSAGE_FILE=$(mktemp)
# Parameters
declare -r MESSAGE=${MESSAGE:-$1}
# Helpers
if [ -z "$TERM" ] || [ "$TERM" == "dumb" ]; then
tput() {
return 0
}
fi
if ! type tput >/dev/null 2>&1; then
tput() {
return 0
}
fi
function log_info() {
local CYAN=$(tput setaf 6)
local NC=$(tput sgr0)
echo "${CYAN}[INFO ]${NC} $*" 1>&2
}
function log_warning() {
local YELLOW=$(tput setaf 3)
local NC=$(tput sgr0)
echo "${YELLOW}[WARNING]${NC} $*" 1>&2
}
function log_debug() {
local PURPLE=$(tput setaf 5)
local NC=$(tput sgr0)
echo "${PURPLE}[DEBUG ]${NC} $*" 1>&2
}
function log_error() {
local RED=$(tput setaf 1)
local NC=$(tput sgr0)
echo "${RED}[ERROR ]${NC} $*" 1>&2
}
function log_success() {
local GREEN=$(tput setaf 2)
local NC=$(tput sgr0)
echo "${GREEN}[SUCCESS]${NC} $*" 1>&2
}
function log_title() {
local GREEN=$(tput setaf 2)
local BOLD=$(tput bold)
local NC=$(tput sgr0)
echo 1>&2
echo "${GREEN}${BOLD}---- $* ----${NC}" 1>&2
}
function h_run() {
local ORANGE=$(tput setaf 3)
local NC=$(tput sgr0)
echo "${ORANGE}\$${NC} $*" 1>&2
eval "$*"
}
function err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
function print_help() {
# Prints help section from the top of the file
#
# It stops until it finds the '# START' line
echo "HELP:"
while read -r LINE; do
if [[ "${LINE}" == "#!/bin/bash" ]] || [[ "${LINE}" == "" ]]; then
continue
fi
if [[ "${LINE}" == "# START" ]]; then
return
fi
echo "${LINE}" | sed 's/^# / /g' | sed 's/^#//g'
done <${BASH_SOURCE[0]}
}
# Functions
function write_to_temp_file() {
# Write input string to temp file
# Globals:
# TEMP_MESSAGE_FILE
# Arguments:
# 1: IN_STRING
# Outputs:
# None
local IN_STRING=$1
log_debug "Writing to temp file: ${TEMP_MESSAGE_FILE}"
echo ${IN_STRING} > ${TEMP_MESSAGE_FILE}
}
# Main
if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
# Script is being invoked directly instead of being sourced
write_to_temp_file "quero cafe"
log_info "Print the temp file content"
cat ${TEMP_MESSAGE_FILE}
fi