Skip to content

Use limactl json output #5

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 4 commits into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

[Lima](https://github.com/lima-vm/lima) is an alternative to using Docker Desktop on your Mac.

## Description

![Screen shot of Lima menu with a running vm](https://raw.githubusercontent.com/unixorn/lima-xbar-plugin/main/pix/limactl-screen-shot.png)

This plugin is compatible with [xbar](https://xbarapp.com/) and [SwiftBar](https://github.com/swiftbar/SwiftBar), and lets you start and stop lima VMs from the menubar.

![Screen shot of Lima menu with a running vm](https://raw.githubusercontent.com/unixorn/lima-xbar-plugin/main/pix/limactl-screen-shot.png)
## Installation

### Dependencies

- [xbar](https://xbarapp.com/) - Allows you to make custom menubar apps with just scripts

- [jq](https://stedolan.github.io/jq/) - `brew install jq` - Used to parse the output of `limactl`
71 changes: 47 additions & 24 deletions lima-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
#
# Dependencies:
# lima - https://github.com/lima-vm/lima
# jq - https://stedolan.github.io/jq/

# InService output color (default green)
# Running VM color (default green)
RUNNING_VM_COLOR="#29cc00"

# OutOfService output color (default red)
# Stopped VM color (default red)
STOPPED_VM_COLOR="#ff0033"

set -o pipefail
Expand Down Expand Up @@ -65,31 +66,64 @@ logger -t 'limamenu' "RUNNING_VM_COLOR=$RUNNING_VM_COLOR"
logger -t 'limamenu' "STOPPED_VM_COLOR=$STOPPED_VM_COLOR"
logger -t 'limamenu' "XBAR_PLUGIN=$XBAR_PLUGIN"

function printMenu() {
# shellcheck disable=SC2059
function warnIfMissingDependencies() {
local depsOK=1
local warnings=""
if ! has limactl; then
warnings=$(printf "${warnings}limactl is not in your PATH! - click to get started | color=$STOPPED_VM_COLOR href=https://github.com/lima-vm/lima#getting-started\n")
depsOK=0
fi
if ! has jq; then
warnings=$(printf "${warnings}\njq is not in your PATH! - click for installation instructions | color=$STOPPED_VM_COLOR href=https://stedolan.github.io/jq/download/\n")
depsOK=0
fi
if [[ depsOK -eq 0 ]]; then
echo "🐋 ⛔ | color=$STOPPED_VM_COLOR"
echo '---'
echo "$warnings"
exit 0
fi
}

function printMenuBarIcon() {
# Bar title
local menuBarIcon
menuBarIcon="🐋 ⛔ | color=$STOPPED_VM_COLOR"
for limavm in $(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | awk '{ print $1 }')

for vm in $(limactl list --json | jq -r '.status')
do
vmstatus=$(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | grep "$limavm" | awk '{ print $2 }')
if [[ "$vmstatus" == 'Running' ]]; then
if [[ "$vm" == 'Running' ]]; then
menuBarIcon="🐋 🏃 | color=$RUNNING_VM_COLOR"
fi
done
echo "$menuBarIcon"
echo '---'
}

for limavm in $(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | awk '{ print $1 }')
function printMenu() {
warnIfMissingDependencies

printMenuBarIcon

local name
local vmstatus

for raw in $(limactl list --json)
do
vmstatus=$(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | grep "$limavm" | awk '{ print $2 }')
if [[ "$vmstatus" == 'Running' ]]; then
menuItem="$limavm VM is running - ⛔ | color=$RUNNING_VM_COLOR | bash=$XBAR_PLUGIN param1=stop param2=$limavm terminal=false refresh=true"
name=$(echo "$raw"| jq -r '.name' )
vmstatus=$(echo "$raw" | jq -r '.status')
if [[ $vmstatus == 'Running' ]]; then
menuItem="$name VM is running - ⛔ | color=$RUNNING_VM_COLOR | bash=$XBAR_PLUGIN param1=stop param2=$name terminal=false refresh=true"
else
menuItem="$limavm VM is stopped - ▶️ | color=$STOPPED_VM_COLOR | bash=$XBAR_PLUGIN param1=start param2=$limavm terminal=false refresh=true"
menuItem="$name VM is stopped - ▶️ | color=$STOPPED_VM_COLOR | bash=$XBAR_PLUGIN param1=start param2=$name terminal=false refresh=true"
fi
echo "$menuItem"
done

echo "force rescan | bash=limactl param1=list terminal=false refresh=true"
echo "Lima home | href=https://github.com/lima-vm/lima"
limactl --version
}

function processMenuCommand() {
Expand All @@ -105,16 +139,5 @@ function processMenuCommand() {
esac
}

function warnLimactlIsMissing() {
echo "🐋 ❌ | color=$STOPPED_VM_COLOR"
echo '---'
echo "limactl is not in your PATH! | color=$STOPPED_VM_COLOR"
echo "Lima home | href=https://github.com/lima-vm/lima"
}

if has limactl; then
printMenu
processMenuCommand "$@"
else
warnLimactlIsMissing
fi
printMenu
processMenuCommand "$@"