From e6771a7a365b3fd8a6e5bf8ad5cc041badd8a092 Mon Sep 17 00:00:00 2001 From: Joe Block Date: Sat, 18 Sep 2021 10:11:31 -0600 Subject: [PATCH 1/4] Check for our dependencies before generating menu We require `jq` and `limactl`, so if they're missing, add menu items that take us to the corresponding install instructions web pages. --- README.md | 12 +++++++++++- lima-plugin | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 66e9e69..10b0222 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file +## 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` \ No newline at end of file diff --git a/lima-plugin b/lima-plugin index 4e26aaa..f56544a 100755 --- a/lima-plugin +++ b/lima-plugin @@ -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 @@ -65,7 +66,28 @@ 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" +# 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 printMenu() { + warnIfMissingDependencies # Bar title menuBarIcon="🐋 ⛔ | color=$STOPPED_VM_COLOR" for limavm in $(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | awk '{ print $1 }') @@ -105,16 +127,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 \ No newline at end of file +printMenu +processMenuCommand "$@" \ No newline at end of file From 46ff5ecaac1c352cced6986d46542384e8cd17c7 Mon Sep 17 00:00:00 2001 From: Joe Block Date: Sat, 18 Sep 2021 10:40:53 -0600 Subject: [PATCH 2/4] Start using jq --- lima-plugin | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lima-plugin b/lima-plugin index f56544a..4427d9e 100755 --- a/lima-plugin +++ b/lima-plugin @@ -86,19 +86,23 @@ function warnIfMissingDependencies() { fi } -function printMenu() { - warnIfMissingDependencies +function printMenuBarIcon() { # Bar title + local limaStatus + local menuBarIcon menuBarIcon="🐋 ⛔ | color=$STOPPED_VM_COLOR" - for limavm in $(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | awk '{ print $1 }') - do - vmstatus=$(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | grep "$limavm" | awk '{ print $2 }') - if [[ "$vmstatus" == 'Running' ]]; then - menuBarIcon="🐋 🏃 | color=$RUNNING_VM_COLOR" - fi - done + limaStatus="$(limactl list --json | jq -r '.status')" + if [[ "$limaStatus" == 'Running' ]]; then + menuBarIcon="🐋 🏃 | color=$RUNNING_VM_COLOR" + fi echo "$menuBarIcon" echo '---' +} + +function printMenu() { + warnIfMissingDependencies + + printMenuBarIcon for limavm in $(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | awk '{ print $1 }') do @@ -110,8 +114,10 @@ function printMenu() { 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() { From ae9a417b01f1b1b8ab62308baeb015a30e7ee6a5 Mon Sep 17 00:00:00 2001 From: Joe Block Date: Sat, 18 Sep 2021 10:52:45 -0600 Subject: [PATCH 3/4] Deal with multiple VMs - Stop assuming there is only one lima VM - Stop assuming that if there is only one lima VM, it is named default --- lima-plugin | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lima-plugin b/lima-plugin index 4427d9e..b40161c 100755 --- a/lima-plugin +++ b/lima-plugin @@ -91,10 +91,13 @@ function printMenuBarIcon() { local limaStatus local menuBarIcon menuBarIcon="🐋 ⛔ | color=$STOPPED_VM_COLOR" - limaStatus="$(limactl list --json | jq -r '.status')" - if [[ "$limaStatus" == 'Running' ]]; then - menuBarIcon="🐋 🏃 | color=$RUNNING_VM_COLOR" - fi + + for vm in $(limactl list --json | jq -r '.status') + do + if [[ "$vm" == 'Running' ]]; then + menuBarIcon="🐋 🏃 | color=$RUNNING_VM_COLOR" + fi + done echo "$menuBarIcon" echo '---' } @@ -104,15 +107,19 @@ function printMenu() { printMenuBarIcon - for limavm in $(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | awk '{ print $1 }') + 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" + echo $menuItem done echo "force rescan | bash=limactl param1=list terminal=false refresh=true" From e530ef88cb1b310ad1772f870bb512c87a431e28 Mon Sep 17 00:00:00 2001 From: Joe Block Date: Sat, 18 Sep 2021 11:15:20 -0600 Subject: [PATCH 4/4] Shellcheck cleanups --- lima-plugin | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lima-plugin b/lima-plugin index b40161c..39fb162 100755 --- a/lima-plugin +++ b/lima-plugin @@ -88,7 +88,6 @@ function warnIfMissingDependencies() { function printMenuBarIcon() { # Bar title - local limaStatus local menuBarIcon menuBarIcon="🐋 ⛔ | color=$STOPPED_VM_COLOR" @@ -112,14 +111,14 @@ function printMenu() { for raw in $(limactl list --json) do - name=$(echo $raw | jq -r '.name' ) - vmstatus=$(echo $raw | jq -r '.status') + 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="$name VM is stopped - ▶️ | color=$STOPPED_VM_COLOR | bash=$XBAR_PLUGIN param1=start param2=$name terminal=false refresh=true" fi - echo $menuItem + echo "$menuItem" done echo "force rescan | bash=limactl param1=list terminal=false refresh=true"