Skip to content

Commit 0640d99

Browse files
authored
Merge pull request #9 from unixorn/submenus-for-vms
Add images submenu functionality and notification displays
2 parents 46fb6df + c062061 commit 0640d99

File tree

2 files changed

+126
-15
lines changed

2 files changed

+126
-15
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
i: install
1+
i: lint install
22
install:
33
cp lima-plugin ~/Library/Application\ Support/xbar/plugins/lima-plugin.10s
44

Diff for: lima-plugin

+125-14
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
# <xbar.author>Joe Block</xbar.author>
1010
# <xbar.author.github>unixorn</xbar.author.github>
1111
# <xbar.desc>Control Lima VM</xbar.desc>
12-
# <xbar.dependencies>Lima</xbar.dependencies>
12+
# <xbar.dependencies>jq,lima</xbar.dependencies>
1313
# <xbar.image>https://raw.githubusercontent.com/unixorn/lima-xbar-plugin/main/pix/limactl-screen-shot.png</xbar.image>
14+
# <xbar.abouturl>https://github.com/unixorn/lima-xbar-plugin/</xbar.abouturl>
1415
#
1516
# Dependencies:
1617
# lima - https://github.com/lima-vm/lima
@@ -49,6 +50,34 @@ function has() {
4950
which "$@" > /dev/null 2>&1
5051
}
5152

53+
function displayAlert() {
54+
alertCommand="display alert \"$1\" message \"$2\""
55+
osascript -e "$alertCommand"
56+
}
57+
58+
function displayNotification() {
59+
if [[ $# -eq 1 ]]; then
60+
message_command="display notification \"$1\""
61+
osascript -e "$message_command"
62+
fi
63+
64+
if [[ $# -eq 2 ]]; then
65+
message_command="display notification \"$2\" with title \"$1\""
66+
osascript -e "$message_command"
67+
fi
68+
69+
if [[ $# -eq 3 ]]; then
70+
message_command="display notification \"$3\" with title \"$1\" subtitle \"$2\""
71+
echo "message_command: $message_command"
72+
osascript -e "$message_command"
73+
fi
74+
75+
if [[ $# -eq 4 ]]; then
76+
message_command="display notification \"$4\" with title \"$1\" subtitle \"$2\" sound name \"$3\""
77+
osascript -e "$message_command"
78+
fi
79+
}
80+
5281
# Set up a working scratch directory
5382
SCRATCH_D=$(mktemp -d)
5483

@@ -62,20 +91,24 @@ trap cleanup EXIT
6291
export PATH="$PATH:/usr/local/bin"
6392
XBAR_PLUGIN="$0"
6493

65-
logger -t 'limamenu' "RUNNING_VM_COLOR=$RUNNING_VM_COLOR"
66-
logger -t 'limamenu' "STOPPED_VM_COLOR=$STOPPED_VM_COLOR"
67-
logger -t 'limamenu' "XBAR_PLUGIN=$XBAR_PLUGIN"
68-
6994
# shellcheck disable=SC2059
7095
function warnIfMissingDependencies() {
7196
local depsOK=1
7297
local warnings=""
98+
if ! has jq; then
99+
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")
100+
depsOK=0
101+
fi
102+
if ! has lima; then
103+
warnings=$(printf "${warnings}lima is not in your PATH! - click to get started | color=$STOPPED_VM_COLOR href=https://github.com/lima-vm/lima#getting-started\n")
104+
depsOK=0
105+
fi
73106
if ! has limactl; then
74107
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")
75108
depsOK=0
76109
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")
110+
if ! has osascript; then
111+
warnings=$(printf "${warnings}osascript is not in your PATH! - click to get started | color=$STOPPED_VM_COLOR\n")
79112
depsOK=0
80113
fi
81114
if [[ depsOK -eq 0 ]]; then
@@ -101,6 +134,7 @@ function printMenuBarIcon() {
101134
echo '---'
102135
}
103136

137+
# shellcheck disable=SC2059
104138
function printMenu() {
105139
warnIfMissingDependencies
106140

@@ -114,27 +148,104 @@ function printMenu() {
114148
name=$(echo "$raw"| jq -r '.name' )
115149
vmstatus=$(echo "$raw" | jq -r '.status')
116150
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"
151+
echo "$name VM is running | color=$RUNNING_VM_COLOR"
152+
echo "--⛔ Stop $name VM | bash=$XBAR_PLUGIN param1=stop param2=$name terminal=false refresh=true"
153+
echo "-- Images"
154+
for image in $(vmImages)
155+
do
156+
echo "----$image"
157+
echo "------ pull | bash=$XBAR_PLUGIN param1=pull param2=$name param3=$image terminal=false refresh=true"
158+
echo "------ rm | bash=$XBAR_PLUGIN param1=rmImage param2=$name param3=$image terminal=false refresh=true"
159+
done
118160
else
119-
menuItem="$name VM is stopped - ▶️ | color=$STOPPED_VM_COLOR | bash=$XBAR_PLUGIN param1=start param2=$name terminal=false refresh=true"
161+
echo "$name VM is stopped | color=$STOPPED_VM_COLOR"
162+
echo "--▶️ Start $name VM | bash=$XBAR_PLUGIN param1=start param2=$name terminal=false refresh=true"
120163
fi
121-
echo "$menuItem"
122164
done
123165

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

171+
function vmImages() {
172+
# default VM doesn't need to be specified
173+
if [[ "$VM" != 'default' ]]; then
174+
export LIMA_INSTANCE="$VM"
175+
fi
176+
# shellcheck disable=SC2001,SC2046,2005
177+
imageList="[$(echo $(lima nerdctl images --format '{{json .}},') | sed 's/,$//')]"
178+
# Can have spaces in our data, deal by using base64 (ugly)
179+
for row in $(echo "${imageList}" | jq -r '.[] | @base64'); do
180+
_jq() {
181+
echo "${row}" | base64 --decode | jq -r "${1}"
182+
}
183+
echo "$(_jq '.Repository'):$(_jq '.Tag')"
184+
done
185+
}
186+
187+
function pullImage() {
188+
# arg1 = image
189+
# arg2 = VM
190+
local imageName
191+
local VM
192+
imageName="$1"
193+
VM="$2"
194+
if [[ "$VM" != 'default' ]]; then
195+
export LIMA_INSTANCE="$VM"
196+
fi
197+
displayNotification lima "Pulling ${imageName} on ${VM}..."
198+
if lima nerdctl image pull "${imageName}"; then
199+
displayNotification Lima "Pulled ${imageName}"
200+
else
201+
displayAlert Lima "Failed to pull ${imageName} on ${VM}"
202+
fi
203+
}
204+
205+
function rmImage() {
206+
# arg1 = image
207+
# arg2 = VM
208+
local imageName
209+
local VM
210+
imageName="$1"
211+
VM="$2"
212+
if [[ "$VM" != 'default' ]]; then
213+
export LIMA_INSTANCE="$VM"
214+
fi
215+
displayNotification lima "Removing ${imageName} from ${VM}..."
216+
if lima nerdctl image rm "${imageName}"; then
217+
displayNotification Lima "Removed ${imageName}"
218+
else
219+
displayAlert Lima "Failed to remove ${imageName}"
220+
fi
221+
}
222+
129223
function processMenuCommand() {
130224
case "$1" in
225+
images)
226+
vmImages "$2"
227+
;;
228+
pull)
229+
pullImage "$2" "$3" # pull imagename vmname
230+
;;
231+
rmImage)
232+
rmImage "$2" "$3" # pull imagename vmname
233+
;;
131234
start)
132-
logger -t 'limamenu' "Starting $2"
133-
limactl start "$2"
235+
displayNotification 'Lima' "Starting $2 VM"
236+
if limactl start "$2"; then
237+
displayNotification "Lima" "Started $2 VM successfully"
238+
else
239+
displayAlert "Lima" "Failed to start $2 VM"
240+
fi
134241
;;
135242
stop)
136-
logger -t 'limamenu' "Stopping $2"
137-
limactl stop "$2"
243+
displayNotification 'Lima' "Stopping $2 VM"
244+
if limactl stop "$2"; then
245+
displayNotification "Lima" "Stopped $2 VM successfully"
246+
else
247+
displayAlert "Lima" "Failed to stop $2 VM"
248+
fi
138249
;;
139250
esac
140251
}

0 commit comments

Comments
 (0)