From 69d63cb9ce4b0e5396e3c1db6eb66a6a2b783069 Mon Sep 17 00:00:00 2001 From: kake26 Date: Fri, 29 Mar 2024 14:54:50 -0500 Subject: [PATCH] initial commit --- README.md | 7 ++++++ config.ini | 7 ++++++ config.json | 6 +++++ config_ini.bash | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ config_json.bash | 44 +++++++++++++++++++++++++++++++++++++ script.bash | 18 +++++++++++++++ 6 files changed, 139 insertions(+) create mode 100644 README.md create mode 100644 config.ini create mode 100644 config.json create mode 100755 config_ini.bash create mode 100755 config_json.bash create mode 100644 script.bash diff --git a/README.md b/README.md new file mode 100644 index 0000000..eae8533 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# lactozora the bash framework + +A framework for making bash scripts with some useful features + +# Where did the name come from? + +Well, one day I was watching a youtube video about a new type of scam that was discovered by the youtuber Pleasent Green. Which dealt with becoming a reseller / middle man for a fictiious herb called lactozora. I basically just stole the name. \ No newline at end of file diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..d6764a7 --- /dev/null +++ b/config.ini @@ -0,0 +1,7 @@ + +[section] +key=newvalue +key2=fuck this + +[newsection] +key=newvalue diff --git a/config.json b/config.json new file mode 100644 index 0000000..9dcb86e --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "bashini": "new", + "section": { + "key": "beer" + } +} diff --git a/config_ini.bash b/config_ini.bash new file mode 100755 index 0000000..da6658c --- /dev/null +++ b/config_ini.bash @@ -0,0 +1,57 @@ +#!/bin/bash + +set -eo pipefail + +# To read a value from the INI file +# Assume the INI file is formatted like: +# [section] +# key=value + +# Function to read a value from an INI-style file +read_ini() { + local INI_FILE="$1" + local SECTION="$2" + local KEY="$3" + + # Use awk to read the specific section and key + awk -F '=' "/^\[$SECTION\]/ {section=1} section==1 && /^$KEY=/ {print \$2; exit}" "$INI_FILE" +} + +# To write or update a value in the INI file + +# Function to set a value in an INI-style file +write_ini() { + local INI_FILE="$1" + local SECTION="$2" + local KEY="$3" + local VALUE="$4" + + # Create the INI file if it doesn't exist + if [ ! -f "$INI_FILE" ]; then + touch "$INI_FILE" + fi + + # Check if the key already exists + if grep -q "^\[$SECTION\]" "$INI_FILE"; then + if grep -q "^$KEY=" "$INI_FILE"; then + # If the key exists, update it + sed -i "/^\[$SECTION\]/, /^\[/ { /^$KEY=/ s/=.*/=$VALUE/ }" "$INI_FILE" + else + # If the key does not exist, add it under the section + sed -i "/^\[$SECTION\]/a $KEY=$VALUE" "$INI_FILE" + fi + else + # If the section does not exist, add the section and the key + echo -e "\n[$SECTION]\n$KEY=$VALUE" >> "$INI_FILE" + fi +} + +# Usage +write_ini "config.ini" "section" "key" "newvalue" +write_ini "config.ini" "newsection" "key" "newvalue" +# Usage +value=$(read_ini "config.ini" "section" "key") +echo "$value" + +value=$(read_ini "config.ini" "section" "key2") +echo "$value" \ No newline at end of file diff --git a/config_json.bash b/config_json.bash new file mode 100755 index 0000000..3ecf1b5 --- /dev/null +++ b/config_json.bash @@ -0,0 +1,44 @@ +#!/bin/bash + +JSON_FILE="config.json" + +read_json() { + local JSON_FILE="$1" + local SECTION="$2" + local KEY="$3" + + jq -r ".${SECTION}.${KEY}" "$JSON_FILE" +} + +# Function to write a value to a JSON configuration file +write_json() { + local JSON_FILE="$1" + local SECTION="$2" + local KEY="$3" + local VALUE="$4" + + # It important to check if the file is valid otherwise stuff breaks + if ! jq -e . < "$JSON_FILE" > /dev/null 2>&1; then + echo "Error: JSON file is not valid JSON." + return 1 + fi + + # Using jq to set the value. The original file is replaced with a new file with the updated value. + jq ".${SECTION}.${KEY} = \"$VALUE\"" "$JSON_FILE" > "temp.json" && mv "temp.json" "$JSON_FILE" + +} + +create_json() { + local JSON_FILE="$1" + echo '{"bashini": "new"}' | jq '.' > "$JSON_FILE" # well we need something in there + +} + +check_json() { + local JSON_FILE="$1" + if [ ! -f "$JSON_FILE" ]; then + create_json "$JSON_FILE" + fi +} + +check_json "$JSON_FILE" \ No newline at end of file diff --git a/script.bash b/script.bash new file mode 100644 index 0000000..aab0975 --- /dev/null +++ b/script.bash @@ -0,0 +1,18 @@ +#!/bin/bash + +set -eo pipefail + +if [[ -n "${BASHD_DEBUG}" ]]; then # a inevitability that this will be used + set -x +fi + +trap cleanup EXIT # A little more robust cleanup + +cleanup() { + # We can clean up any temp files or what nots, but for now a place holder + true +} + +source ./config_json.bash # can be json or ini, I preffer json + +# Code goes here \ No newline at end of file