This commit is contained in:
Strix 2024-06-06 02:01:36 +02:00
commit 243e102b10
10 changed files with 282 additions and 0 deletions

82
lib/a3.sh Normal file
View file

@ -0,0 +1,82 @@
#!/bin/sh
# General purpose for a3
# a3ff <func> [args]: runs functions and automatically makes the first arg the file
# a3ff => a3 file function
A3_FILE=${A3_FILE:-}
a3ff() {
[ -z "$A3_FILE" ] && return 1
local func=$1
[ -z "$func" ] && return 1
shift
$func $A3_FILE $@
}
A3_INDEX_ROOT=${A3_INDEX_ROOT:-/var/lib/a3/index}
a3index_create() {
file=$1
name=$2
[ -z "$file" ] && return 1
[ -z "$name" ] && return 1
[ "$file" = "$A3_INDEX_ROOT/pending/$name.a3" ] && return 0
mkdir -p $A3_INDEX_ROOT/pending
cp $file $A3_INDEX_ROOT/pending/$name.a3
if ! grep -q "declare_workdir" $A3_INDEX_ROOT/pending/$name.a3; then
echo -e "\ndeclare_workdir $(pwd)" >> $A3_INDEX_ROOT/pending/$name.a3
fi
}
a3index_find() {
name=$1
[ -z "$name" ] && return 1
find $A3_INDEX_ROOT -iname "$name.a3" | head -n 1
}
a3index_current_status() {
name=$1
[ -z "$name" ] && return 1
file=$(a3index_find $name)
[ -z "$file" ] && return 1
basename $(dirname $file)
}
a3index_update() {
name=$1
status=$2
[ -z "$name" ] && return 1
[ -z "$status" ] && return 1
[ -f "$A3_INDEX_ROOT/$status/$name.a3" ] && return 0
file=$(a3index_find $name)
[ -z "$file" ] && return 1
mkdir -p $A3_INDEX_ROOT/$status
mv $file $A3_INDEX_ROOT/$status/$name.a3
}
a3index_is() {
name=$1
status=$2
[ -z "$name" ] && return 1
[ -z "$status" ] && return 1
[ -f "$A3_INDEX_ROOT/$status/$name.a3" ]
}
a3read_declaration() {
file=$1
declaration=$2
[ -z "$file" ] && return 1
[ -z "$declaration" ] && return 1
shift; shift
grep "$declaration" $file | sed "s/$declaration//" | awk '{$1=$1};1'
}
a3read_meta() {
file=$1
prop=$2
[ -z "$file" ] && return 1
[ -z "$prop" ] && return 1
shift; shift
grep "#a3.$prop:" $file | sed "s/#a3.$prop://" | xargs
}

21
lib/include.sh Normal file
View file

@ -0,0 +1,21 @@
#!/bin/sh
if ! [ $A3_INCLUDE ]; then
A3_INCLUDE=1
A3_INCLUDED=""
A3_LIB_PATH=${A3_LIB_PATH:-/usr/lib/a3}
is_included() {
echo "$A3_INCLUDED" | grep -q "$1"
}
include() {
[ -z "$1" ] && return 0
is_included $1 && return 0
. $1
A3_INCLUDED="$A3_INCLUDED $1"
}
a3include_lib() {
include $A3_LIB_PATH/$1.sh
}
fi

37
lib/log.sh Normal file
View file

@ -0,0 +1,37 @@
#!/bin/sh
# levels:
# DEBUG = 0
# INFO = 1
# WARN = 2
# ERROR = 3
A3_LOG_LEVEL=${A3_LOG_LEVEL:-1}
__a3_log_level_to_text() {
case $1 in
0) echo "debug" ;;
1) echo "info" ;;
2) echo "warn" ;;
3) echo "error" ;;
4) echo "fatal" ;;
esac
}
__a3_log() {
level=$1
shift
if [ $level -ge $A3_LOG_LEVEL ]; then
echo "[$(__a3_log_level_to_text $level)] $@"
fi
}
alias LOGD="__a3_log 0"
alias LOGI="__a3_log 1"
alias LOGW="__a3_log 2"
alias LOGE="__a3_log 3"
fatal_log() {
__a3_log 4 $@
exit 1
}

9
lib/util.sh Normal file
View file

@ -0,0 +1,9 @@
#!/bin/sh
require_value() {
local name=$1; shift
if [ -z "$@" ]; then
echo "expected value for: $name"
exit 1
fi
}