Skip to content

Add container submenu #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions lima-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ function printMenu() {
if [[ $vmstatus == 'Running' ]]; then
echo "$name VM is running | color=$RUNNING_VM_COLOR"
echo "--⛔ Stop $name VM | bash=$XBAR_PLUGIN param1=stop param2=$name terminal=false refresh=true"

echo "-- Containers"
for container in $(vmContainers)
do
echo "---- $container"
# echo "------ start | bash=$XBAR_PLUGIN param1=startContainer param2=$name param3=$container terminal=false refresh=true"
echo "------ stop | bash=$XBAR_PLUGIN param1=stopContainer param2=$name param3=$container terminal=false refresh=true"
echo "------ kill | bash=$XBAR_PLUGIN param1=killContainer param2=$name param3=$container terminal=false refresh=true"
echo "------ pause | bash=$XBAR_PLUGIN param1=pauseContainer param2=$name param3=$container terminal=false refresh=true"
echo "------ unpause | bash=$XBAR_PLUGIN param1=unpauseContainer param2=$name param3=$container terminal=false refresh=true"
done

echo "-- Images"
for image in $(vmImages)
do
Expand All @@ -168,6 +180,22 @@ function printMenu() {
limactl --version
}

function vmContainers() {
# default VM doesn't need to be specified
if [[ "$VM" != 'default' ]]; then
export LIMA_INSTANCE="$VM"
fi
# shellcheck disable=SC2001,SC2046,SC2005
containerList="[$(echo $(lima nerdctl ps --format '{{json .}},') | sed 's/,$//')]"
# Can have spaces in our data, deal by using base64 (ugly)
for row in $(echo "${containerList}" | jq -r '.[] | @base64'); do
_jq() {
echo "${row}" | base64 --decode | jq -r "${1}"
}
_jq '.Names'
done
}

function vmImages() {
# default VM doesn't need to be specified
if [[ "$VM" != 'default' ]]; then
Expand Down Expand Up @@ -220,6 +248,78 @@ function rmImage() {
fi
}

function stopContainer() {
# arg1 = container
# arg2 = VM
local containerName
local VM
containerName="$1"
VM="$2"
if [[ "$VM" != 'default' ]]; then
export LIMA_INSTANCE="$VM"
fi
displayNotification lima "Stopping ${containerName} on ${VM}..."
if lima nerdctl container stop "${containerName}"; then
displayNotification Lima "Stopped ${containerName}"
else
displayAlert Lima "Failed to stop ${containerName} on ${VM}"
fi
}

function killContainer() {
# arg1 = container
# arg2 = VM
local containerName
local VM
containerName="$1"
VM="$2"
if [[ "$VM" != 'default' ]]; then
export LIMA_INSTANCE="$VM"
fi
displayNotification lima "Killing ${containerName} on ${VM}..."
if lima nerdctl container kill "${containerName}"; then
displayNotification Lima "Killed ${containerName}"
else
displayAlert Lima "Failed to kill ${containerName} on ${VM}"
fi
}

function pauseContainer() {
# arg1 = container
# arg2 = VM
local containerName
local VM
containerName="$1"
VM="$2"
if [[ "$VM" != 'default' ]]; then
export LIMA_INSTANCE="$VM"
fi
displayNotification lima "Pausing ${containerName} on ${VM}..."
if lima nerdctl container pause "${containerName}"; then
displayNotification Lima "Paused ${containerName}"
else
displayAlert Lima "Failed to pause ${containerName} on ${VM}"
fi
}

function unpauseContainer() {
# arg1 = container
# arg2 = VM
local containerName
local VM
containerName="$1"
VM="$2"
if [[ "$VM" != 'default' ]]; then
export LIMA_INSTANCE="$VM"
fi
displayNotification lima "Unpausing ${containerName} on ${VM}..."
if lima nerdctl container unpause "${containerName}"; then
displayNotification Lima "Unpaused ${containerName}"
else
displayAlert Lima "Failed to unpause ${containerName} on ${VM}"
fi
}

function processMenuCommand() {
case "$1" in
images)
Expand All @@ -228,6 +328,18 @@ function processMenuCommand() {
pull)
pullImage "$2" "$3" # pull imagename vmname
;;
stopContainer)
stopContainer "$3" "$2" # stopContainer containerName VMname
;;
killContainer)
killContainer "$3" "$2" # killContainer containerName VMname
;;
pauseContainer)
pauseContainer "$3" "$2" # pauseContainer containerName VMname
;;
unpauseContainer)
unpauseContainer "$3" "$2" # unpauseContainer containerName VMname
;;
rmImage)
rmImage "$2" "$3" # pull imagename vmname
;;
Expand Down