WIP: Adds kanatctl to runs/after/system, adds ability for kanatactl to install service as user or system service.

This commit is contained in:
2025-11-10 11:36:21 -05:00
parent 74011a46bc
commit 3c98a008c8
5 changed files with 157 additions and 77 deletions

View File

@@ -15,7 +15,7 @@ DEV_ENV=${DEV_ENV:-""}
KBD=${KBD:-""} # Keyboard config to use, either "voyager" or "macbook"
usage() {
cat <<EOF
cat <<EOF
Manages kanata qmk keyboard program, which needs to be built locally. Currently the linux builds
are only for x86. Also manages the kanata systemd service.
@@ -41,17 +41,17 @@ EOF
# Logging utility function, use in place of echo.
log() {
logging log --source "$THIS_FILE" "$@"
logging log --source "$THIS_FILE" "$@"
}
# Bootstrap a new machine, by building and installing the kanata executable,
# installing the systemd service files, and enable / start the service.
bootstrap() {
log "Bootstrapping new system..."
$THIS_FILE pkg install
$THIS_FILE service install
$THIS_FILE service enable
$THIS_FILE service start
log "Bootstrapping new system..."
$THIS_FILE pkg install
$THIS_FILE service install
$THIS_FILE service enable
$THIS_FILE service start
}
################################################################################
@@ -63,29 +63,29 @@ source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL"
while [[ $# -gt 0 ]]; do
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
elif [[ $1 == "bootstrap" ]]; then
bootstrap && exit 0
elif [[ $1 == "config" ]]; then
shift
THIS="$THIS config" $SCRIPTS/utils/kanatactl/config "$@"
exit $?
elif [[ $1 == "service" ]]; then
shift
THIS="$THIS service" $SCRIPTS/utils/kanatactl/service "$@"
exit $?
elif [[ $1 == "pkg" ]]; then
shift
THIS="$THIS pkg" $SCRIPTS/utils/kanatactl/pkg "$@"
exit $?
elif [[ $1 == "logs" ]]; then
bat ${LOG_DIR:-/tmp/logs}/$LOG_FILE && exit 0
elif [[ $1 == "update" ]]; then
install_or_update "Updating" && exit 0
else
break
fi
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
elif [[ $1 == "bootstrap" ]]; then
bootstrap && exit 0
elif [[ $1 == "config" ]]; then
shift
THIS="$THIS config" $SCRIPTS/utils/kanatactl/config "$@"
exit $?
elif [[ $1 == "service" ]]; then
shift
THIS="$THIS service" $SCRIPTS/utils/kanatactl/service "$@"
exit $?
elif [[ $1 == "pkg" ]]; then
shift
THIS="$THIS pkg" $SCRIPTS/utils/kanatactl/pkg "$@"
exit $?
elif [[ $1 == "logs" ]]; then
bat ${LOG_DIR:-/tmp/logs}/$LOG_FILE && exit 0
elif [[ $1 == "update" ]]; then
install_or_update "Updating" && exit 0
else
break
fi
done
# If we've made it here, then we didn't handle the command.

View File

@@ -11,8 +11,15 @@ THIS=${THIS:-$LOG_LABEL}
LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"}
DEV_ENV=${DEV_ENV:-""}
declare user_flag system_flag prompt_flag mode
declare -a args
user_flag="1"
system_flag="0"
prompt_flag="0"
mode=""
usage() {
cat <<EOF
cat <<EOF
Manages the kanata systemd service.
USAGE:
@@ -21,6 +28,9 @@ USAGE:
FLAGS:
-h | --help: Show this help page.
--user: Setup systemd service as a user service (default).
--system: Setup systemd service as a system service.
--prompt: Prompt the user to install service as system or user service.
COMMANDS:
enable: Enable the kanata service.
@@ -40,52 +50,90 @@ EOF
# Logging utility function, use in place of echo.
log() {
logging log --source "$THIS_FILE" "$@"
logging log --source "$THIS_FILE" "$@"
}
enable_service() {
log "Enabling kanata service..."
sudo systemctl enable kanata.service
log "Enabling kanata service..."
if [[ $user_flag == "1" ]]; then
systemctl --user enable --now kanata.service
else
sudo systemctl enable --now kanata.service
fi
}
get_status() {
log "Getting kanata service status..."
systemctl status kanata.service
log "Getting kanata service status..."
systemctl status kanata.service
}
prompt_user() {
log "Prompting user for how they would like to setup kanata systemd service..."
local choice
choice=$(echo -e "user\nsystem" | fzf --header "How would you like to setup the kanata systemd service?")
if [[ $choice == "system" ]]; then
user_flag="0"
system_flag="1"
elif [[ $choice == "user" ]]; then
user_flag="1"
system_flag="0"
else
exit 1
fi
}
install_service() {
if [[ -z $DEV_ENV ]]; then
log --error "DEV_ENV is not set properly." && exit 1
fi
local service_dir="user"
# Ensure the configuration is copied / setup otherwise the keyboard may not work.
"$SCRIPTS/kanatactl" config install "$@"
if [[ -z $DEV_ENV ]]; then
log --error "DEV_ENV is not set properly." && exit 1
fi
log "Starting install service..."
log "Installing kanata service..."
[[ -f /etc/systemd/system/kanata.service ]] && sudo rm -rf /etc/systemd/system/kanata.service
sudo cp "$DEV_ENV/env/etc/systemd/system/kanata.service" /etc/systemd/system
sudo systemctl daemon-reload
# Ensure the configuration is copied / setup otherwise the keyboard may not work.
"$SCRIPTS/kanatactl" config install "$@"
# Prompt the user for how they would like to install the systemd service, if the
# prompt flag is set.
[[ $prompt_flag == "1" ]] && prompt_user || exit 1
[[ $system_flag == "1" ]] && service_dir="system"
log "Installing kanata service as: '$service_dir' service..."
if [[ $service_dir == "system" ]]; then
[[ -f /etc/systemd/system/kanata.service ]] && sudo rm -rf /etc/systemd/system/kanata.service
sudo cp "$DEV_ENV/env/etc/systemd/system/kanata.service" /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable --now kanata.service
else
[[ -f ~/.config/systemd/user/kanata.service ]] && rm -rf ~/.config/systemd/user/kanata.service
mkdir -p ~/.config/systemd/user &>/dev/null
cp "$DEV_ENV/env/etc/systemd/user/kanata.service" ~/.config/systemd/user
systemctl --user daemon-reload
systemctl --user enable --now kanata.service
fi
}
start_service() {
log "Starting kanata service..."
systemctl start kanata.service
log "Starting kanata service..."
systemctl start kanata.service
}
stop_service() {
log "Stopping kanata service..."
sudo systemctl stop kanata.service
log "Stopping kanata service..."
sudo systemctl stop kanata.service
}
disable_service() {
log "Disabling kanata service..."
stop_service
sudo systemctl disable kanata.service
log "Disabling kanata service..."
stop_service
sudo systemctl disable kanata.service
}
restart_service() {
log "Restarting kanata service..."
sudo systemctl restart kanata.service
log "Restarting kanata service..."
sudo systemctl restart kanata.service
}
################################################################################
@@ -96,26 +144,42 @@ restart_service() {
source "$SCRIPTS/hypr/logging"
setup-logging "$LOG_FILE" "$LOG_LABEL"
# Parse flags / arguments.
while [[ $# -gt 0 ]]; do
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
elif [[ $1 == "disable" ]]; then
disable_service && exit $?
elif [[ $1 == "enable" ]]; then
enable_service && exit $?
elif [[ $1 == "install" ]]; then
shift
install_service "$@" && exit $?
elif [[ $1 == "start" ]]; then
start_service && exit $?
elif [[ $1 == "status" ]]; then
get_status && exit $?
elif [[ $1 == "stop" ]]; then
stop_service && exit $?
elif [[ $1 == "restart" ]]; then
restart_service && exit $?
fi
if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
usage && exit 0
elif [[ $1 == "--user" ]]; then
user_flag="1"
elif [[ $1 == "--system" ]]; then
system_flag="1"
elif [[ $1 == "--prompt" ]]; then
prompt_flag="1"
# Set first non-flag to the mode / subcommand.
elif [[ -z $mode ]]; then
mode="$1"
# Add any other arguments to the 'args' array.
else
args+=("$1")
fi
shift
done
# If we made it here, then none of the subcommands handled the args.
usage && exit 1
# Handle mode.
if [[ $mode == "disable" ]]; then
disable_service && exit $?
elif [[ $mode == "enable" ]]; then
enable_service && exit $?
elif [[ $mode == "install" ]]; then
install_service "${args[@]}" && exit $?
elif [[ $mode == "start" ]]; then
start_service && exit $?
elif [[ $mode == "status" ]]; then
get_status && exit $?
elif [[ $mode == "stop" ]]; then
stop_service && exit $?
elif [[ $mode == "restart" ]]; then
restart_service && exit $?
else
# If we made it here, then none of the subcommands handled the args.
usage && exit 1
fi

12
env/etc/systemd/user/kanata.service vendored Normal file
View File

@@ -0,0 +1,12 @@
[Unit]
Description=Kanata Service
Documentation=https://github.com/jtroo/kanata
[Service]
Environment=PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin
Type=simple
ExecStart=/bin/sh -c 'exec /usr/bin/kanata --cfg /home/michael/.config/kanata/config.kbd'
Restart=no
[Install]
WantedBy=default.target

View File

@@ -8,6 +8,7 @@ install() {
log " Setting up espanso."
sudo setcap "cap_dac_override+p" "$(which espanso)"
espanso service register
systemctl --user daemon-reload
espanso service start
}

View File

@@ -4,16 +4,19 @@ set -e
set -o nounset
set -o pipefail
SCRIPTS="${DEV_ENV}/env/.local/scripts"
install() {
log " Setting user shell to 'zsh'."
sudo chsh --shell "$(which zsh)"
log " Enabling up systemd services."
systemctl --user daemon-reload
sudo systemctl daemon-reload
sudo systemctl enable --now pcscd.service
sudo systemctl enable --now firewalld.service
systemctl --user enable --now logout-task.service
systemctl --user enable --now battery-monitor.timer
systemctl --user enable --now tmux-kill-sessions.timer
sudo systemctl enable --now pcscd.service
sudo systemctl enable --now firewalld.service
SCRIPTS="$SCRIPTS" "$SCRIPTS/kanatactl" service install --prompt
}
arg=${1:-""}