From 18e8a3dcbc96be086f69237c77dc797d68cc10f5 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Wed, 7 Jul 2021 17:50:13 +0300
Subject: [PATCH] A bunch of tweaks for working in a Toolbox container.

- Replace the bfg shell alias with a script, use the jar in the Docker
  image if available.
- Use full Docker image names for use with Podman.
- Remove kpcli and keybase aliases, not in use anymore.
- In the notify script, get the host' hostname if in a Toolbox
  container.
- Script to update the workbench container.
---
 .bashrc                 | 14 ++++++++------
 Documents/bin/bfg       | 13 +++++++++++++
 Documents/bin/notify    |  7 ++++++-
 Documents/bin/update-wb | 28 ++++++++++++++++++++++++++++
 4 files changed, 55 insertions(+), 7 deletions(-)
 create mode 100755 Documents/bin/bfg
 create mode 100755 Documents/bin/update-wb

diff --git a/.bashrc b/.bashrc
index b341e16..e7d4b98 100644
--- a/.bashrc
+++ b/.bashrc
@@ -66,7 +66,6 @@ alias ansible-local-playbook='ansible-playbook -i localhost, -c local -e "ansibl
 alias ansible-local='ansible localhost -c local -i localhost, -e "ansible_python_interpreter=$(which python3)"'
 alias aptitude='aptitude --display-format %p --quiet'
 alias bell="printf '\\a'"
-alias bfg='java -jar $HOME/.local/share/bfg/bfg.jar'
 alias black='black --line-length 79'
 alias blue="printf '\e[1;94m%s\e[0m\n'"
 alias bold="printf '\e[1m%s\e[0m\n'"
@@ -92,12 +91,10 @@ alias gcc='gcc --std=c99 -Wall -Wextra -Werror -pedantic'
 alias gen-mac='hexdump -n5 -e '\''"02" 5/1 ":%02X" "\n"'\'' /dev/urandom'
 alias gen-ssh-config="rc_make .ssh/config"
 alias green="printf '\e[1;92m%s\e[0m\n'"
-alias hostlocal='docker run --rm --privileged --net=host gliderlabs/hostlocal'
+alias hostlocal='docker run --rm --privileged --net=host docker.io/gliderlabs/hostlocal'
 alias http-server='python3 -m http.server 8080'
 alias httpbin='gunicorn httpbin:app --bind 0.0.0.0:8080'
 alias jjb='jenkins-jobs'
-alias killkeybase='pgrep --full keybase | xargs -rt kill'
-alias kpcli='kpcli --kdb ~/Documents/Database.kdbx'
 alias l='ls -F'
 alias la='ls -AF'
 alias listen_tcp='nc -vlk 0.0.0.0'
@@ -124,8 +121,13 @@ alias netdata='docker run --detach \
                           --volume /var/run/docker.sock:/var/run/docker.sock \
                           --publish 19999:19999 \
                           --security-opt apparmor=unconfined \
-                          netdata/netdata'
-alias newman='docker run --rm -u "$(id -u):$(id -g)" -v "$PWD:/etc/newman" -t postman/newman_alpine33'
+                          docker.io/netdata/netdata'
+# shellcheck disable=SC1004
+alias newman='docker run --rm \
+                         -u "$(id -u):$(id -g)" \
+                         -v "$PWD:/etc/newman" \
+                         -t \
+                         docker.io/postman/newman_alpine33'
 alias nextcloudcmd='flatpak run --command=nextcloudcmd com.nextcloud.desktopclient.nextcloud'
 # shellcheck disable=SC2139
 alias notify="notify --hint \"string:desktop-entry:$(basename "${GIO_LAUNCHED_DESKTOP_FILE:-io.elementary.terminal.desktop}")\""
diff --git a/Documents/bin/bfg b/Documents/bin/bfg
new file mode 100755
index 0000000..cd65bed
--- /dev/null
+++ b/Documents/bin/bfg
@@ -0,0 +1,13 @@
+#!/bin/sh
+set -eu
+
+if [ -f /usr/local/share/bfg/bfg.jar ]
+then
+    exec java -jar /usr/local/share/bfg/bfg.jar "$@"
+elif [ -f "${XDG_DATA_HOME:-$HOME/.local/share}/bfg/bfg.jar" ]
+then
+    exec java "${XDG_DATA_HOME:-$HOME/.local/share}/bfg/bfg.jar" "$@"
+else
+    echo "Can't find the BFG jar, exiting." >&2
+    exit 1
+fi
diff --git a/Documents/bin/notify b/Documents/bin/notify
index 7e15523..0a9bc6d 100755
--- a/Documents/bin/notify
+++ b/Documents/bin/notify
@@ -32,6 +32,12 @@ else
     hostname="$(sysctl -a 2>/dev/null | awk -F '=' '/^kern[a-z]*\.hostname/ {print $2}')"
 fi
 
+# Get the host' hostname in case
+if [ "$hostname" = 'toolbox' ]
+then
+    hostname="$(/usr/libexec/flatpak-xdg-utils/flatpak-spawn --host hostname)"
+fi
+
 #FIXME: Remove domain, just hostname.
 
 case "$hostname" in
@@ -41,7 +47,6 @@ case "$hostname" in
     kodi) nextcloud;;
     ns1) nextcloud;;
     ns4) nextcloud;;
-    workbench) nextcloud;;
     YOGA-C930) notify_send "$@";;
     *) ;;
 esac
diff --git a/Documents/bin/update-wb b/Documents/bin/update-wb
new file mode 100755
index 0000000..448701f
--- /dev/null
+++ b/Documents/bin/update-wb
@@ -0,0 +1,28 @@
+#!/bin/sh
+set -eu
+
+IMAGE='registry.shore.co.il/workbench'
+CONTAINER='workbench'
+
+is_latest() {
+    podman image exists "$IMAGE" || return 1
+    toolbox run --container "$CONTAINER" true 2>/dev/null || return 1
+    current="$(podman image inspect "$IMAGE" --format '{{.Digest}}')"
+    podman image pull --quiet "$IMAGE" > /dev/null
+    new="$(podman image inspect "$IMAGE" --format '{{.Digest}}')"
+    [ "$new" = "$current" ] || return 1
+}
+
+update() {
+    toolbox rm --force "$CONTAINER" 2>/dev/null || true
+    yes | podman image prune --filter 'label=com.github.containers.toolbox=true'
+    toolbox create --image "$IMAGE"
+}
+
+if [ "$(hostname)" = 'toolbox' ]
+then
+    /usr/libexec/flatpak-xdg-utils/flatpak-spawn --host "$0" "$@"
+elif ! (is_latest)
+then
+    update
+fi
-- 
GitLab