Skip to content

Commit

Permalink
Add cli helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nadavspi committed Jan 19, 2024
1 parent a03da94 commit 26056c5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
3 changes: 0 additions & 3 deletions home/dot_config/zsh/conf.d/podman.zsh

This file was deleted.

56 changes: 56 additions & 0 deletions home/dot_local/bin/executable_cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash

container=dotfiles
image=dotfiles
service=dotfiles-cli.service

function get_metadata() {
# Output:
# $0 image id
# $1 git revision
# $2 created datetime
# $3 image name
echo $(echo "$@" | jq --raw-output '.[0] |
# `podman ps` returns .ImageID, `podman image inspect` returns .Id
([.ImageID, .Id] | map(select(. != null)))[0],
.Labels["org.opencontainers.image.revision"],
.Labels["org.opencontainers.image.created"], .Image
')
}

function output() {
local arr
read -a arr <<< "$@"
image=$(echo ${arr[0]} | cut -c1-7)
revision=$(echo ${arr[1]} | cut -c1-7)
datetime=${arr[2]}
echo "$image $datetime $revision"
}

active=$(systemctl --user is-active $service)
latest=$(get_metadata `podman image inspect $image`)
running=$(get_metadata `podman ps -f name=dotfiles --format="json"`)
# can get this from $running or $latest
image_name=$(podman inspect $container|jq '.[0].ImageName' | cut -d '"' -f 2)

echo "Image: $image_name"
echo "Status: $active"
echo "Last checked for updates:"

printf "\n"
headings=$(printf "%-7s %-24s %-7s" "Image" "Created" "Revision")
if [[ "$(output $latest)" == "$(output $running)" ]]; then
printf "󰄬 Running the latest image\n\n"
echo "$headings"
echo "$(output $latest)"
gum confirm "Check for updates?" && podman pull $image && $0 || exit 0
else
printf "󰚰 Update available\n\n"
echo " $headings"
echo " $(output $latest)"
echo "$(output $running)"
gum confirm "Restart container?" && \
gum spin --spinner line --title "Restarting $service" -- systemctl --user restart $service && \
systemctl --user status $service \
|| exit 0
fi

0 comments on commit 26056c5

Please sign in to comment.