autoinstall/autostart-manage/lib/common.sh

78 lines
1.7 KiB
Bash
Raw Normal View History

2024-01-08 22:37:31 +00:00
#!/usr/bin/env bash
_systemctl () {
systemctl --user "${@:2}" "autostart@$1.service"
}
_query_autostart_toml() {
2024-10-23 20:32:11 +00:00
groups_json=$(echo -n "$2" | tr "," "\n" | jq -R . | jq -s .)
tomlq -r \
--arg host "$HOSTNAME" \
--argjson extra_groups "$groups_json" \
2024-01-08 22:37:31 +00:00
'.hosts[$host].groups as $groups | .apps | to_entries[] | select(
(.value.hosts | contains([$host])) or
2024-10-23 20:32:11 +00:00
([.value.group] | inside($groups)) or
([.value.group] | inside($extra_groups)) or
$extra_groups == ["*"]
2024-01-08 22:37:31 +00:00
) | '"$1" \
"$XDG_CONFIG_HOME/autostart.toml"
}
_list () {
2024-10-23 20:32:11 +00:00
_query_autostart_toml '.value.alias // .key' "$1"
2024-01-08 22:37:31 +00:00
}
_autostart_run_graphical () {
2024-01-09 15:48:11 +00:00
set +e
2024-01-08 22:37:31 +00:00
pass x # Try to unlock yubikey asap
start-audio pipewire
wait-for-service "network-online.target"
# Execute only if not already running
# Don't match keepassxc-proxy
if ! (pgrep -l keepassxc | grep -v prox) >/dev/null
then
if pass x
then
(pass keepass | head -n 1 | keepassxc --pw-stdin ~/sync/passwords.kdbx) &
fi
fi
2024-01-09 15:48:11 +00:00
autoinstall run graphical
2024-01-08 22:37:31 +00:00
autostart-manage run
}
_echo_table () {
for unit in "$@"
do
2024-10-23 20:32:11 +00:00
if [ "$(_systemctl "$unit" is-enabled)" = "enabled" ]
then
_enabled=$(green "enabled")
else
_enabled=$(red "disabled")
fi
if _systemctl "$unit" is-active --quiet
then
_active=$(green "active")
else
_active=$(red "inactive")
fi
2024-01-08 22:37:31 +00:00
printf "%s\t%s\t%s\t%s\n" \
"$unit" \
"$_enabled" \
"$_active" \
2024-01-08 22:37:31 +00:00
"$(_get_autostart_cmd "$unit")"
done
}
_get_autostart_cmd () {
2024-10-23 20:32:11 +00:00
_query_autostart_toml 'select((.key == "'"$1"'") or (.value.alias == "'"$1"'")) | .value.command' "*"
2024-01-08 22:37:31 +00:00
}
_get_autostart_delay () {
2024-10-23 20:32:11 +00:00
_query_autostart_toml 'select((.key == "'"$1"'") or (.value.alias == "'"$1"'")) | .value.delay // 0' "*"
2024-01-08 22:37:31 +00:00
}