-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-report
executable file
·226 lines (193 loc) · 6.09 KB
/
gen-report
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
# TODO: Make an option for generating reports with custom dates on the fly ex. gen-report -n 'bob' -p '123-123-1230' -e '[email protected]' -b 'bob inc' -h '5' -d '05-04-2020'
# Set posix mode
set -o posix;
# Import Libraries
source /lib/GenReport/get.sh;
source /lib/GenReport/set.sh;
# Get todays date for file naming convention
report_date=$(date +%Y-%m-%d); # Sets file name later
# Set report path
report_path=/home/awesomepilot/Documents/Work/Notes
# Get the current report file
report_file="$report_path/$report_date.txt";
# Show the built in help text
function show_help() {
# declare a variable to hold the content of help_doc.txt
local help_text;
help_text=$(</usr/share/help_doc.txt);
# Echo help text to screen
echo "$help_text";
return 0;
}
# A function to export the report as a text file
function export_report() {
local report_to_export="$1";
if ! report_exists "$report_to_export"; then
echo "That report doesn't exist. Nothing exported...";
exit 1;
fi
local report_string=$(get_report "$report_to_export");
unstringify_report "$report_string" > /home/"$USER"/Documents/Notes/"$report_to_export.txt";
}
# Parse arguments and set variables
while getopts "n:p:e:b:l:t:d:x:h:-:kq" arg; do
case "$arg" in
n)
client_name=${OPTARG}; # Set Client Name
;;
p)
client_phone=${OPTARG}; # Set Client Phone
;;
e)
client_email=${OPTARG}; # Set Client Email
;;
b)
client_bizz_name=${OPTARG}; # Set Client Business Name
;;
h)
client_hours=${OPTARG}; # Set Client Hours
;;
l)
entry_to_list=${OPTARG}; # Get the entry to list
rep_string=$(get_report "$entry_to_list");
unstringify_report "$rep_string";
exit 0;
;;
k)
# Get output of get_all_report_keys() and put it in an array
key_arr=($(get_all_report_keys));
# Iterate through the array and list our options on seperate lines
for key in "${key_arr[@]}"; do
echo "$key";
done
exit 0;
;;
t) # (t)ransform edits reports
edit_report ${OPTARG};
exit 0;
;;
q)
get_latest_report;
exit 0;
;;
d)
if ! delete_report ${OPTARG}; then
echo "There was a problem deleting ${OPTARG}, it might not exist...";
exit 1;
fi
exit 0;
;;
x)
export_report ${OPTARG};
exit 0;
;;
-)
case "${OPTARG}" in
'help')
show_help;
exit 0;
;;
'test')
if ! bash /lib/GenReport/test/master_test.sh; then # Run tests, be sure to run with bash and not sh
echo "Failed to start tests...is your default CLI environment /bin/sh? If so please change to /bin/bash";
exit 1;
fi
exit 0;
;;
*)
echo "Something went terribly wrong, please contact [email protected] to report this issue.";
exit 1;
esac
;;
?)
show_help;
exit 1;
;;
*)
echo "Oops you may have made an invalid entry. Pleae try again.";
exit 1;
esac
done
function create_work_notes() {
# Clear screen
clear;
# Get work notes
echo "Enter work notes, hit Ctl-D to save>";
work=$(</dev/stdin); # Cannot be local
# Check if work notes were left blank
if [ -z "$work" ]; then
if (whiptail --title "Leave notes blank?" --yesno "Are you sure you want to leave your work notes blank?" --yes-button "No" --no-button "Yes" 8 78) then
work=$(</dev/stdin);
else
work='N/A';
fi
fi
return 0;
}
function create_side_notes() {
# Clear screen
clear;
# Set side notes
echo "Enter side notes if any, hit Ctl-D to save>";
notes=$(</dev/stdin); # Cannot be local
# Check if side notes were left blank
if [ -z "$notes" ]; then
notes='N/A';
fi
return 0;
}
# Create report at ~/$USER/Documents/Work/Notes/
function create_report() {
# cat > $report_path/$report_date.txt << _EOF_
cat > /tmp/report.txt << _EOF_
(Client Report - Date: $report_date)
------------------------------------------
## Client Information ##
------------------------------------------
Client Name: $client_name
Client Phone: $client_phone
Client Email: $client_email
Client Business: $client_bizz_name
Client Hours: $client_hours
------------------------------------------
## Work Performed ##
------------------------------------------
$work
------------------------------------------
## Notes ##
------------------------------------------
$notes
------------------------------------------
_EOF_
report_text=/tmp/report.txt;
# Insert generated report into redis
if (! set_report "$client_name" "$report_text") then
echo "There was a problem inserting your report...Make sure Redis is up?";
return 1;
fi
rm -rf "$report_text";
return 0;
}
# Check if all required arguments are accounted for
if [ -z "$client_name" ]; then
echo "You cannot leave Client Name field empty...";
exit 1;
elif [ -z "$client_phone" ]; then
echo "You cannot leave Client Phone field empty...";
exit 1;
elif [ -z "$client_email" ]; then
echo "You cannot leave Client Email field empty...";
exit 1;
elif [ -z "$client_bizz_name" ]; then
echo "You cannot leave Client Bussiness Name field empty...";
exit 1;
elif [ -z "$client_hours" ]; then
echo "You cannot leave Client Hours field empty...";
exit 1;
else
# Create new report if not exists and overwrite if it does
create_work_notes;
create_side_notes;
create_report;
fi