return $lastexit
}
+get_date_time()
+{
+ date --iso-8601=secondes
+}
+
+get_backup_type()
+{
+ local config="$1"
+ local backup_path=
+ backup_path=$(echo "$config" | jq --raw-output '.backup_path')
+
+ [ -f "$backup_path/day" ] && echo "day" && return 0
+ [ -f "$backup_path/week" ] && echo "week" && return 0
+ [ -f "$backup_path/month" ] && echo "month" && return 0
+
+ echo "none"
+}
+
+log()
+{
+ local config="$1"
+ local msg="$2"
+ local logfile=
+ local datetime=
+ logfile=$(echo "$config" | jq --raw-output '.backup_logfile')
+ [ -n "$logfile" ] || return 0
+ [ "$logfile" != "null" ] || return 0
+ datetime=$(get_date_time)
+ type=$(get_backup_type "$config")
+
+ logmsg="$datetime [$type] $msg"
+ echo "ℹ️ INFO: $logmsg"
+ echo "$logmsg" >> "$logfile"
+}
+
exists_needed_commands()
{
command -v yq > /dev/null \
[ -d build/2000-01-01_19_backup ]
rm --recursive build/2000-01-01_19_backup
rmdir build/2000-01-01_*
-}
\ No newline at end of file
+}
+
+# bats test_tags=backup:log
+@test "success no config" {
+ mkdir --parents build
+ run log "$config"
+
+ assert_success
+ assert_output ""
+}
+
+# bats test_tags=backup:log
+@test "success no log config has no logfile entry" {
+ mkdir --parents build
+ config="{}"
+ run log "$config"
+
+ assert_success
+ assert_output ""
+}
+
+write_log_entry() {
+ local type="$1"
+ mkdir --parents build
+ touch "build/$type"
+ local config=
+ config=$(yq '.' "$CONFIG_FILE")
+ get_date_time() {
+ echo "2000-01-01T23:30:45+02:00"
+ }
+
+ run log "$config" "log entry"
+
+ assert_success
+ assert_output "ℹ️ INFO: 2000-01-01T23:30:45+02:00 [$type] log entry"
+ assert_equal "$(cat "build/backup.log")" "2000-01-01T23:30:45+02:00 [$type] log entry"
+ rm "build/$type"
+ rm "build/backup.log"
+}
+
+for type in none day week month; do
+ bats_test_function --tags "backup:log" \
+ --description "success log has logfile entry for type $type" \
+ -- \
+ write_log_entry "$type"
+done
\ No newline at end of file