Skip to content

Commit 28cbd42

Browse files
authored
Merge pull request #5 from unixorn/use-limactl-json-output
Use limactl json output
2 parents e32889d + e530ef8 commit 28cbd42

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

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

5+
## Description
6+
7+
![Screen shot of Lima menu with a running vm](https://raw.githubusercontent.com/unixorn/lima-xbar-plugin/main/pix/limactl-screen-shot.png)
8+
59
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.
610

7-
![Screen shot of Lima menu with a running vm](https://raw.githubusercontent.com/unixorn/lima-xbar-plugin/main/pix/limactl-screen-shot.png)
11+
## Installation
12+
13+
### Dependencies
14+
15+
- [xbar](https://xbarapp.com/) - Allows you to make custom menubar apps with just scripts
16+
17+
- [jq](https://stedolan.github.io/jq/) - `brew install jq` - Used to parse the output of `limactl`

lima-plugin

+47-24
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
#
1515
# Dependencies:
1616
# lima - https://github.com/lima-vm/lima
17+
# jq - https://stedolan.github.io/jq/
1718

18-
# InService output color (default green)
19+
# Running VM color (default green)
1920
RUNNING_VM_COLOR="#29cc00"
2021

21-
# OutOfService output color (default red)
22+
# Stopped VM color (default red)
2223
STOPPED_VM_COLOR="#ff0033"
2324

2425
set -o pipefail
@@ -65,31 +66,64 @@ logger -t 'limamenu' "RUNNING_VM_COLOR=$RUNNING_VM_COLOR"
6566
logger -t 'limamenu' "STOPPED_VM_COLOR=$STOPPED_VM_COLOR"
6667
logger -t 'limamenu' "XBAR_PLUGIN=$XBAR_PLUGIN"
6768

68-
function printMenu() {
69+
# shellcheck disable=SC2059
70+
function warnIfMissingDependencies() {
71+
local depsOK=1
72+
local warnings=""
73+
if ! has limactl; then
74+
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")
75+
depsOK=0
76+
fi
77+
if ! has jq; then
78+
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")
79+
depsOK=0
80+
fi
81+
if [[ depsOK -eq 0 ]]; then
82+
echo "🐋 ⛔ | color=$STOPPED_VM_COLOR"
83+
echo '---'
84+
echo "$warnings"
85+
exit 0
86+
fi
87+
}
88+
89+
function printMenuBarIcon() {
6990
# Bar title
91+
local menuBarIcon
7092
menuBarIcon="🐋 ⛔ | color=$STOPPED_VM_COLOR"
71-
for limavm in $(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | awk '{ print $1 }')
93+
94+
for vm in $(limactl list --json | jq -r '.status')
7295
do
73-
vmstatus=$(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | grep "$limavm" | awk '{ print $2 }')
74-
if [[ "$vmstatus" == 'Running' ]]; then
96+
if [[ "$vm" == 'Running' ]]; then
7597
menuBarIcon="🐋 🏃 | color=$RUNNING_VM_COLOR"
7698
fi
7799
done
78100
echo "$menuBarIcon"
79101
echo '---'
102+
}
80103

81-
for limavm in $(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | awk '{ print $1 }')
104+
function printMenu() {
105+
warnIfMissingDependencies
106+
107+
printMenuBarIcon
108+
109+
local name
110+
local vmstatus
111+
112+
for raw in $(limactl list --json)
82113
do
83-
vmstatus=$(limactl list | grep -v 'NAME STATUS SSH ARCH DIR' | grep "$limavm" | awk '{ print $2 }')
84-
if [[ "$vmstatus" == 'Running' ]]; then
85-
menuItem="$limavm VM is running - ⛔ | color=$RUNNING_VM_COLOR | bash=$XBAR_PLUGIN param1=stop param2=$limavm terminal=false refresh=true"
114+
name=$(echo "$raw"| jq -r '.name' )
115+
vmstatus=$(echo "$raw" | jq -r '.status')
116+
if [[ $vmstatus == 'Running' ]]; then
117+
menuItem="$name VM is running - ⛔ | color=$RUNNING_VM_COLOR | bash=$XBAR_PLUGIN param1=stop param2=$name terminal=false refresh=true"
86118
else
87-
menuItem="$limavm VM is stopped - ▶️ | color=$STOPPED_VM_COLOR | bash=$XBAR_PLUGIN param1=start param2=$limavm terminal=false refresh=true"
119+
menuItem="$name VM is stopped - ▶️ | color=$STOPPED_VM_COLOR | bash=$XBAR_PLUGIN param1=start param2=$name terminal=false refresh=true"
88120
fi
89121
echo "$menuItem"
90122
done
123+
91124
echo "force rescan | bash=limactl param1=list terminal=false refresh=true"
92125
echo "Lima home | href=https://github.com/lima-vm/lima"
126+
limactl --version
93127
}
94128

95129
function processMenuCommand() {
@@ -105,16 +139,5 @@ function processMenuCommand() {
105139
esac
106140
}
107141

108-
function warnLimactlIsMissing() {
109-
echo "🐋 ❌ | color=$STOPPED_VM_COLOR"
110-
echo '---'
111-
echo "limactl is not in your PATH! | color=$STOPPED_VM_COLOR"
112-
echo "Lima home | href=https://github.com/lima-vm/lima"
113-
}
114-
115-
if has limactl; then
116-
printMenu
117-
processMenuCommand "$@"
118-
else
119-
warnLimactlIsMissing
120-
fi
142+
printMenu
143+
processMenuCommand "$@"

0 commit comments

Comments
 (0)