1
0
mirror of https://github.com/RyanGreenup/cadmus.git synced 2025-08-13 17:44:08 +02:00

[FEAT] Added a Config File

Script to Generate Config File
Code to parse config file
This commit is contained in:
ryangreenup
2020-07-20 01:51:13 +10:00
parent 9f59ce3fcf
commit 343c3809c6
6 changed files with 185 additions and 10 deletions

0
.Rhistory Normal file
View File

View File

@@ -91,7 +91,7 @@ readFirstArgument () {
SkimNotes () { SkimNotes () {
## Change directory if One was specified, exit if no directory exists ## Change directory if One was specified, exit if no directory exists
cd "${1}" cd "${1}"
FILE="$(SkimGrep)" FILE="$(SkimGrep)"

View File

@@ -1,8 +1,12 @@
#! /usr/bin/env bash #! /usr/bin/env bash
readonly NOTES_DIR="$HOME/Notes/MD/notes" ## TODO Global Variables are bad IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
readonly SERVER_DIR="/srv/www/html/MD" readonly script_name=$(basename "${0}")
readonly RECOLL_CONFIG_DIR="$HOME/.cadmus" script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
readonly MKDOCS_YML="$HOME/Notes/mkdocs.yml" ## This may be above the notes directory, the readonly script_dir=$(realpath "${script_dir}""/""${script_name}" | xargs dirname)
readonly CONFIG=$(realpath "${script_dir}/../config.json") # one directory above
## readonly SERVER_DIR="/srv/www/html/MD"
## readonly RECOLL_CONFIG_DIR="$HOME/.cadmus"
## readonly MKDOCS_YML="$HOME/Notes/mkdocs.yml" ## This may be above the notes directory, the
## notes directory may not contain all the ## notes directory may not contain all the
## images for want of seperation and mkdocs is ## images for want of seperation and mkdocs is
## fidally, so specify the path to mkdocs.yml. ## fidally, so specify the path to mkdocs.yml.
@@ -18,7 +22,7 @@ set -o pipefail
# * Main Functions # * Main Functions
main() { main() {
checkConfig
setvars setvars
[[ -z "${1:-}" ]] && mainHelp && exit 0 [[ -z "${1:-}" ]] && mainHelp && exit 0
arguments "${@:-}" arguments "${@:-}"
@@ -26,12 +30,14 @@ main() {
function setvars() { function setvars() {
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
readonly script_name=$(basename "${0}")
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
readonly script_dir=$(realpath "${script_dir}""/""${script_name}" | xargs dirname)
readonly TERMINAL="kitty" readonly TERMINAL="kitty"
readonly TERMINAL_EXEC='kitty -- ' readonly TERMINAL_EXEC='kitty -- '
readonly NOTES_DIR="$(cat "${CONFIG}" | jq -r '.notesDir')"
readonly SERVER_DIR="$(cat "${CONFIG}" | jq -r '.serverDir')"
readonly RECOLL_CONFIG_DIR="$(cat "${CONFIG}" | jq -r '.recollConfigDir')"
readonly MKDOCS_YML="$(cat "${CONFIG}" | jq -r '.mkdocsConfigDir')" ## This may be above the notes directory, the
} }
@@ -66,6 +72,33 @@ function mainHelp() {
} }
# ** Main Arguments - Level 0 # ** Main Arguments - Level 0
# *** Config
checkConfig () {
if [[ -f "${CONFIG}" ]]; then
echo "Config Detected"
else
echo -e "No config File detected at ${CONFIG}"
echo -e "Would you like to generate a config?"
echo
echo -e "Press y to continue or any other key to exit."
echo
read -d '' -s -n1 choice
if [[ "${choice}" == "y" ]]; then
config_json=$("${script_dir}/makeConfig.bash")
if [[ "${config_json}" != "" ]]; then
echo "${config_json}" > "${CONFIG}"
echo -e "This is the Config:\n"
highlight "${CONFIG}"
else
echo "Config not Generated"
exit 1
fi
else
exit 0
fi
fi
}
# *** Switch # *** Switch
arguments () { arguments () {

142
bin/makeConfig.bash Executable file
View File

@@ -0,0 +1,142 @@
#! /usr/bin/env bash
#
# Author: Ryan Greenup <ryan.greenup@protonmail.com>
# * Shell Settings
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
# * Main Function
main() {
# Use STDERR so as to not clog STDIN
# Is there a better way to do this?
echoerr() { echo -e "$@" 1>&2; }
check_for_dependencies
setVars
readFirstArgument "${@}"
AskValues
if [ "${promptComplete}" != "Completed" ]; then
exit 1
fi
MakeConfig "${@}"
}
# ** Helper Functions
# *** Check for Dependencies
check_for_dependencies () {
for i in ${DependArray[@]}; do
command -v "$i" >/dev/null 2>&1 || { echo >&2 "I require $i but it's not installed. Aborting."; exit 1; }
done
}
# **** List of Dependencies
declare -a DependArray=(
"jq"
"cat"
"xclip"
)
# *** Set variables below main
setVars () {
readonly script_name=$(basename "${0}")
readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
}
# **** Print Help
Help () {
echo
echo -e " \e[3m\e[1mMakeConfig.sh \e[0m; Helpful Shell Scripts for Markdown Notes"
echo -e " \e[1;31m--------------------------\e[0m "
echo
echo -e " \e[3m\e[1m• Usage \e[0m "
echo
echo -e " "${script_name}" [-h]"
echo -e " "${script_name}" [--help]"
echo
echo -e " Fully Interactive, just follow the prompts"
echo
echo -e " \e[3m By Design: No Options; No Arguments\e[0m"
echo
echo -e " \e[3m\e[1m• Compatability \e[0m "
echo
echo -e " This prints everything to STDERR to that STDOUT only has"
echo -e " The config file in it, I'm not sure if that's an issue "
echo
exit 0
}
# *** Read First Argument
readFirstArgument () {
if [[ "${1:-}" == "-h" ]] || [[ "${1:-}" == "--help" ]]; then
Help && exit 0
fi
}
# ***
AskValues () {
echoerr "Please Enter the Directory of you Markdown Files"
echoerr "\t (This directory should contain index.md or home.md)"
read NOTES_DIR
NOTES_DIR="$(echo "${NOTES_DIR/\~/$HOME}")"
read -d '' -s -n1 choice
# NOTES_DIR="$(cd /; sk --height 40% -i -c 'fd {}' )"
[[ -d "${NOTES_DIR}" ]] || echoerr -e "\n \e[3m\e[1m \e[1;31m ⚠ WARNING: \e[0m No Such Directory!"
echoerr "\nPlease Enter the Directory of the recoll config you want to use"
echoerr "\t Leave it blank to use the default config"
echoerr "\t This is not implemented so don't worry"
read RECOLL_CONFIG_DIR
RECOLL_CONFIG_DIR="$(echo "${RECOLL_CONFIG_DIR/\~/$HOME}")"
[[ -d "${RECOLL_CONFIG_DIR}" ]] || echoerr "\n \e[3m\e[1m \e[1;31m ⚠ WARNING: \e[0m No Such Directory!"
echoerr "\nPlease Enter the location of your mkdocs yml"
echoerr "\t (If you're not going to use this just leave it blank and press Enter)"
read MKDOCS_YML
MKDOCS_YML="$(echo "${MKDOCS_YML/\~/$HOME}")"
[[ -f "${MKDOCS_YML}" ]] || echoerr -e "\n \e[3m\e[1m \e[1;31m ⚠ WARNING: \e[0m No Such File!"
echoerr "\nPlease Enter the Directory in which you want mkdocs to build your static site"
echoerr "\t (If you're not going to use this just leave it blank and press Enter)"
read SERVER_DIR
SERVER_DIR="$(echo "${SERVER_DIR/\~/$HOME}")"
[[ -d "${SERVER_DIR}" ]] || echoerr -e "\n \e[3m\e[1m \e[1;31m ⚠ WARNING: \e[0m No Such Directory!"
promptComplete="Completed"
}
# *** Make Config
MakeConfig () {
JSON_STRING=$( jq -n \
--arg nd "$NOTES_DIR" \
--arg sd "$SERVER_DIR" \
--arg rc "$RECOLL_CONFIG_DIR" \
--arg mk "$MKDOCS_YML" \
'{notesDir: $nd, serverDir: $sd, recollConfigDir: $rc, mkdocsConfigDir: $mk}' )
echo "$JSON_STRING"
exit 0
}
# * Call Main Function
main "${@}"

0
sd Normal file
View File

0
sed Normal file
View File