Skip to content

Commit 6960d7e

Browse files
committed
Tidy up, send notifications to Notification Manager
Some of our operations like VM starts or image pulls can take a while. Use the notification manager to display notifications before & after we do a lima operation. Signed-off-by: Joe Block <[email protected]>
1 parent e6b3896 commit 6960d7e

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

Diff for: lima-plugin

+50-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,31 @@ def parseCLI():
9696
return cliArgs
9797

9898

99+
# fun with osascript
100+
101+
102+
def displayAlert(title: str, message: str):
103+
"""
104+
Display an alert using osascript. Blocking.
105+
106+
:param str title:
107+
:param str message:
108+
"""
109+
alertCommand = f'display alert "{title}" message "{message}"'
110+
runCommand(command=["osascript", "-e", alertCommand])
111+
112+
113+
def displayNotification(title: str, message: str):
114+
"""
115+
Publish a notification to the notification manager.
116+
117+
:param str title:
118+
:param str message:
119+
"""
120+
alertCommand = f'display notification "{message}" with title "{title}" '
121+
runCommand(command=["osascript", "-e", alertCommand])
122+
123+
99124
def runCommand(command: list, env=dict(os.environ)):
100125
"""
101126
Run a command and decode the json output
@@ -237,6 +262,9 @@ def prep_environment_for_lima(vm: str = "default", env: dict = dict(os.environ))
237262
Also adds /usr/local/bin to $PATH
238263
239264
:param str vm: VM to work in
265+
:param dict env: Environment variables to base returned environment on
266+
267+
:return dict: Environment dictionary, with /usr/local/bin added to $PATH
240268
"""
241269
newpath = "%s:/usr/local/bin" % env["PATH"]
242270
env["PATH"] = newpath
@@ -249,6 +277,10 @@ def prep_environment_for_lima(vm: str = "default", env: dict = dict(os.environ))
249277
def containerOps(action: str, container: str, vm: str = "default"):
250278
"""
251279
Handle container operations
280+
281+
:param str action: What container op to do
282+
:param str container: What container to do the action on
283+
:param str vm: Which VM is the container in?
252284
"""
253285
logging.warning("containerOps")
254286
logging.debug("action: %s" % action)
@@ -259,15 +291,21 @@ def containerOps(action: str, container: str, vm: str = "default"):
259291

260292
command = ["lima", "nerdctl", "container", action, container]
261293
logging.warning("containerops command: %s", command)
294+
displayNotification(title="Lima VM", message=" ".join(command))
262295

263296
output = runCommand(command=command, env=env)
264297
logging.warning(output)
265298
logging.warning("%s complete", action)
299+
displayNotification(title="Task complete", message=" ".join(command))
266300

267301

268302
def imageOps(action: str, image: str, vm: str = "default"):
269303
"""
270304
Handle VM operations
305+
306+
:param str action: What image op to do
307+
:param str image: What image to do the action on
308+
:param str vm: Which VM is the image in?
271309
"""
272310
logging.critical("imageOps")
273311
logging.info("action: %s" % action)
@@ -279,14 +317,19 @@ def imageOps(action: str, image: str, vm: str = "default"):
279317
command = ["lima", "nerdctl", "image", action, image]
280318
logging.warning("command: %s", command)
281319
logging.warning("PATH: %s", env["PATH"])
320+
displayNotification(title="Lima VM", message=" ".join(command))
282321
output = runCommand(command=command, env=env)
283322
logging.debug(output)
284323
logging.warning("%s complete", action)
324+
displayNotification(title="Task complete", message=" ".join(command))
285325

286326

287327
def vmOps(action: str, vm: str = "default"):
288328
"""
289329
Handle VM operations
330+
331+
:param str action: What action to run - should be start or stop
332+
:param str vm: Name of VM to act on
290333
"""
291334
logging.critical("vmOps")
292335
logging.debug("action: %s" % action)
@@ -296,17 +339,23 @@ def vmOps(action: str, vm: str = "default"):
296339

297340
command = ["limactl", action, vm]
298341
logging.warning("command: %s", command)
342+
displayNotification(title="Lima VM", message=" ".join(command))
299343
output = runCommand(command=command, env=env)
300344
logging.debug(output)
301345
logging.warning("%s complete", action)
346+
displayNotification(title="Task completed", message=" ".join(command))
302347

303348

304349
# Actual Xbar-compatible output
305350

306351

307352
def xbar_icon(vms: dict = {}):
308353
"""
309-
Determine icon to display in menubar
354+
Determine icon to display in menubar.
355+
356+
We display a running menubar icon if at least one VM is running.
357+
358+
:param dict vms: Data about Lima VMs
310359
"""
311360
menuBarIcon = f"🐋 ⛔ | color={STOPPED_VM_COLOR}"
312361
for vm in vms:

0 commit comments

Comments
 (0)