42 lines
873 B
Bash
Executable File
42 lines
873 B
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
mode=$1
|
|
query=$2
|
|
jq_args=$3
|
|
CASHEFILE=$HOME/.cache/waybbar_cashe
|
|
|
|
|
|
# Function to strip first and last characters if they are double quotes
|
|
strip_quotes() {
|
|
local str="$1"
|
|
# Check if the string starts and ends with double quotes
|
|
if [[ $str == \"*\" ]]; then
|
|
# Strip the first and last characters
|
|
str="${str:1:${#str}-2}"
|
|
fi
|
|
echo "$str"
|
|
}
|
|
|
|
|
|
if [ -f "$CASHEFILE" ]; then
|
|
data=$(cat "$CASHEFILE")
|
|
errors=$(echo "$data" | jq empty 2>&1 >/dev/null)
|
|
if [ -n "$error" ] || [ "$data" = "" ]; then
|
|
data="{}"
|
|
fi
|
|
else
|
|
data="{}"
|
|
fi
|
|
|
|
#if [ "$jq_args" = "" ]; then
|
|
# result=$(echo "$data" | jq "$query" )
|
|
#else
|
|
result=$(echo "$data" | jq $jq_args "$query" )
|
|
#fi
|
|
|
|
if [ "$mode" = "query" ]; then
|
|
echo $(strip_quotes $result) | grep --invert-match "null"
|
|
else
|
|
echo $result > $CASHEFILE
|
|
fi
|