comments for all lines and small formatting changes

This commit is contained in:
Jacob Salmela 2017-05-21 23:25:53 -05:00
parent 5d7ef9281f
commit 69fe889f92
No known key found for this signature in database
GPG key ID: 1962FF1A5046135E

View file

@ -47,11 +47,15 @@ else
fi fi
echo_succes_or_fail() { echo_succes_or_fail() {
# Set the first argument passed to tihs function as a named variable for better readability
local message="${1}" local message="${1}"
# If the command was successful (a zero),
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo -e " ${TICK} ${message}" # show success
echo -e " ${TICK} ${message}"
else else
echo -e " ${CROSS} ${message}" # Otherwise, show a error
echo -e " ${CROSS} ${message}"
fi fi
} }
@ -59,17 +63,20 @@ initiate_debug() {
# Clear the screen so the debug log is readable # Clear the screen so the debug log is readable
clear clear
echo -e "${COL_LIGHT_PURPLE}*** [ INITIALIZING ]${COL_NC}" echo -e "${COL_LIGHT_PURPLE}*** [ INITIALIZING ]${COL_NC}"
echo -e " ${INFO} $(date "+%Y-%m-%d:%H:%M:%S") debug log has been initiated." # Timestamp the start of the log
echo -e " ${INFO} $(date "+%Y-%m-%d:%H:%M:%S") debug log has been initiated."
} }
# This is a function for visually displaying the curent test that is being run. # This is a function for visually displaying the curent test that is being run.
# Accepts one variable: the name of what is being diagnosed # Accepts one variable: the name of what is being diagnosed
# Colors do not show in the dasboard, but the icons do: [i], [✓], and [✗]
echo_current_diagnostic() { echo_current_diagnostic() {
# Colors are used for visually distinguishing each test in the output # Colors are used for visually distinguishing each test in the output
echo -e "\n${COL_LIGHT_PURPLE}*** [ DIAGNOSING ]:${COL_NC} ${1}" echo -e "\n${COL_LIGHT_PURPLE}*** [ DIAGNOSING ]:${COL_NC} ${1}"
} }
if_file_exists() { if_file_exists() {
# Set the first argument passed to tihs function as a named variable for better readability
local file_to_test="${1}" local file_to_test="${1}"
# If the file is readable # If the file is readable
if [[ -r "${file_to_test}" ]]; then if [[ -r "${file_to_test}" ]]; then
@ -88,13 +95,17 @@ get_distro_attributes() {
# but we'll keep it within the function for better unit testing # but we'll keep it within the function for better unit testing
IFS=$'\r\n' command eval 'distro_info=( $(cat /etc/*release) )' IFS=$'\r\n' command eval 'distro_info=( $(cat /etc/*release) )'
# Set a named variable for better readability
local distro_attribute local distro_attribute
# For each line found in an /etc/*release file,
for distro_attribute in "${distro_info[@]}"; do for distro_attribute in "${distro_info[@]}"; do
# Display the information with the ${INFO} icon (we need just the OS PRETTY_NAME) # display the information with the ${INFO} icon
pretty_name_key=$(echo "${distro_attribute}" | grep "PRETTY_NAME" | cut -d '=' -f1) pretty_name_key=$(echo "${distro_attribute}" | grep "PRETTY_NAME" | cut -d '=' -f1)
# we need just the OS PRETTY_NAME, so print it when we find it
if [[ "${pretty_name_key}" == "PRETTY_NAME" ]]; then if [[ "${pretty_name_key}" == "PRETTY_NAME" ]]; then
PRETTY_NAME=$(echo "${distro_attribute}" | grep "PRETTY_NAME" | cut -d '=' -f2- | tr -d '"') PRETTY_NAME=$(echo "${distro_attribute}" | grep "PRETTY_NAME" | cut -d '=' -f2- | tr -d '"')
echo " ${INFO} ${PRETTY_NAME}" echo " ${INFO} ${PRETTY_NAME}"
# Otherwise, do nothing
else else
: :
fi fi
@ -104,6 +115,8 @@ get_distro_attributes() {
} }
diagnose_operating_system() { diagnose_operating_system() {
local faq_url="https://discourse.pi-hole.net/t/hardware-software-requirements/273"
local error_msg="Distribution unknown -- most likely you are on an unsupported platform and may run into issues."
# Display the current test that is running # Display the current test that is running
echo_current_diagnostic "Operating system" echo_current_diagnostic "Operating system"
@ -111,21 +124,25 @@ diagnose_operating_system() {
if_file_exists /etc/*release && \ if_file_exists /etc/*release && \
# display the attributes to the user # display the attributes to the user
get_distro_attributes || \ get_distro_attributes || \
# If it doesn't exist, it's not a system we currently support # If it doesn't exist, it's not a system we currently support and link to FAQ
echo -e " ${CROSS} ${COL_LIGHT_RED}Distribution unknown -- most likely you are on an unsupported platform and may run into issues.${COL_NC} echo -e " ${CROSS} ${COL_LIGHT_RED}${error_msg}${COL_NC}
${INFO} ${COL_LIGHT_RED}Please see${COL_NC}: ${COL_CYAN}https://discourse.pi-hole.net/t/hardware-software-requirements/273${COL_NC}" ${INFO} ${COL_LIGHT_RED}Please see${COL_NC}: ${COL_CYAN}${faq_url}${COL_NC}"
} }
parse_file() { parse_file() {
# Set the first argument passed to tihs function as a named variable for better readability
local filename="${1}" local filename="${1}"
# Put the current Internal Field Separator into another variable so it can be restored later
OLD_IFS="$IFS" OLD_IFS="$IFS"
# Get the lines that are in the file(s) and store them in an array for parsing later
IFS=$'\r\n' command eval 'file_info=( $(cat "${filename}") )' IFS=$'\r\n' command eval 'file_info=( $(cat "${filename}") )'
# Set a named variable for better readability
local file_lines local file_lines
# For each lin in the file,
for file_lines in "${file_info[@]}"; do for file_lines in "${file_info[@]}"; do
# Display the information with the ${INFO} icon # display the information with the ${INFO} icon
# No need to show the support URLs so they are grepped out echo " ${INFO} ${file_lines}"
echo " ${INFO} ${file_lines}"
done done
# Set the IFS back to what it was # Set the IFS back to what it was
IFS="$OLD_IFS" IFS="$OLD_IFS"
@ -138,40 +155,55 @@ diagnose_setup_variables() {
# If the variable file exists, # If the variable file exists,
if_file_exists "${VARSFILE}" && \ if_file_exists "${VARSFILE}" && \
# source it # source it
echo -e " ${INFO} Sourcing ${VARSFILE}..."; echo -e " ${INFO} Sourcing ${VARSFILE}...";
source ${VARSFILE}; source ${VARSFILE};
# and display a green check mark with ${DONE} # and display a green check mark with ${DONE}
echo_succes_or_fail "${VARSFILE} is readable and has been sourced." || \ echo_succes_or_fail "${VARSFILE} is readable and has been sourced." || \
# Othwerwise, error out # Othwerwise, error out
echo_succes_or_fail "${VARSFILE} is not readable. echo_succes_or_fail "${VARSFILE} is not readable.
${INFO} $(ls -l ${VARSFILE} 2>/dev/null)"; ${INFO} $(ls -l ${VARSFILE} 2>/dev/null)";
parse_file "${VARSFILE}" parse_file "${VARSFILE}"
} }
# This function can check a directory exists
# Pi-hole has files in several places, so we will reuse this function
dir_check() { dir_check() {
# Set the first argument passed to tihs function as a named variable for better readability
local directory="${1}" local directory="${1}"
# Display the current test that is running
echo_current_diagnostic "contents of ${directory}" echo_current_diagnostic "contents of ${directory}"
# For each file in the directory,
for filename in "${directory}"*; do for filename in "${directory}"*; do
# check if exists first; if it does,
if_file_exists "${filename}" && \ if_file_exists "${filename}" && \
# show a success message
echo_succes_or_fail "Files detected" || \ echo_succes_or_fail "Files detected" || \
# Otherwise, show an error
echo_succes_or_fail "directory does not exist" echo_succes_or_fail "directory does not exist"
done done
} }
list_files_in_dir() { list_files_in_dir() {
# Set the first argument passed to tihs function as a named variable for better readability
local dir_to_parse="${1}" local dir_to_parse="${1}"
# Set another local variable for better readability
local filename local filename
# Store the files found in an array
files_found=( $(ls "${dir_to_parse}") ) files_found=( $(ls "${dir_to_parse}") )
# For each file in the arry,
for each_file in "${files_found[@]}"; do for each_file in "${files_found[@]}"; do
# Display the information with the ${INFO} icon # display the information with the ${INFO} icon
echo " ${INFO} ${each_file}" echo " ${INFO} ${each_file}"
done done
} }
check_dnsmasq_d() { check_dnsmasq_d() {
# Set a local variable for better readability
local directory=/etc/dnsmasq.d local directory=/etc/dnsmasq.d
# Check if the directory exists
dir_check "${directory}" dir_check "${directory}"
# if it does, list the files in it
list_files_in_dir "${directory}" list_files_in_dir "${directory}"
} }