Skip to content

Commit ffe6081

Browse files
committed
Add option to run arbitrary lima commands
- Add option to run arbitrary commands via `lima` - Fix some places where vm was set to default and not the actual vm - Icon tweaks in submenus Closes #21 Signed-off-by: Joe Block <[email protected]>
1 parent 14d9cc8 commit ffe6081

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This plugin is compatible with [xbar](https://xbarapp.com/) and [SwiftBar](https
2727
- start/stop the VM
2828
- stop, start or remove stopped containers
2929
- pull or remove images from the VM
30+
- Run an arbitrary command inside the VM with `lima`
3031

3132
### Screen shots
3233

Diff for: lima-plugin

+43-13
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ RUNNING_VM_COLOR = "#29cc00"
3434
# Stopped VM color (default red)
3535
STOPPED_VM_COLOR = "#ff0033"
3636

37-
VERSION = "1.2.0"
37+
VERSION = "1.3.0"
3838

3939

4040
def logSetup(level: str = "INFO"):
@@ -90,7 +90,7 @@ def parseCLI():
9090
parser.add_argument("--pull-new-image", action="store_true")
9191
parser.add_argument(
9292
"--vm-action",
93-
choices=["start", "stop"],
93+
choices=["start", "stop", "lima"],
9494
help="Action to perform on vm",
9595
)
9696
cliArgs = parser.parse_args()
@@ -348,6 +348,28 @@ def imageOps(action: str, image: str, vm: str = "default"):
348348
displayNotification(title="Task complete", message=" ".join(command))
349349

350350

351+
def limaCommand(vm: str):
352+
"""
353+
Run an arbitrary command with lima
354+
355+
:param str vm: Which vm to run the command in
356+
"""
357+
env = prep_environment_for_lima(vm=vm)
358+
user_command = inputDialog(
359+
user_prompt=f"What lima command do you want to run in the {vm} VM?"
360+
)
361+
362+
if user_command != "":
363+
lima_command = ["lima"] + user_command.split()
364+
displayNotification(
365+
title=f"Running '{user_command}'", message=" ".join(lima_command)
366+
)
367+
runCommand(command=lima_command, env=env)
368+
displayNotification(title=f"Ran '{user_command}' in {vm}", message="Completed")
369+
else:
370+
displayAlert(title="Error!", message="No nerdctl command specified")
371+
372+
351373
def vmOps(action: str, vm: str = "default"):
352374
"""
353375
Handle VM operations
@@ -361,13 +383,19 @@ def vmOps(action: str, vm: str = "default"):
361383

362384
env = prep_environment_for_lima(vm=vm)
363385

364-
command = ["limactl", action, vm]
365-
logging.warning("command: %s", command)
366-
displayNotification(title="Lima VM", message=" ".join(command))
367-
output = runCommand(command=command, env=env)
368-
logging.debug(output)
369-
logging.warning("%s complete", action)
370-
displayNotification(title="Task completed", message=" ".join(command))
386+
if action == "lima":
387+
limaCommand(vm=vm)
388+
389+
if action in ["start", "stop"]:
390+
command = ["limactl", action, vm]
391+
logging.info("command: %s", command)
392+
393+
displayNotification(title="Lima VM", message=" ".join(command))
394+
output = runCommand(command=command, env=env)
395+
logging.debug(output)
396+
397+
logging.info("%s complete", action)
398+
displayNotification(title="Task completed", message=" ".join(command))
371399

372400

373401
def pullNewImage(vm: str = "default"):
@@ -401,7 +429,7 @@ def xbar_icon(vms: dict = {}):
401429
402430
:param dict vms: Data about Lima VMs
403431
"""
404-
menuBarIcon = f"🐋 | color={STOPPED_VM_COLOR}"
432+
menuBarIcon = f"🐋 | color={STOPPED_VM_COLOR}"
405433
for vm in vms:
406434
logging.debug("vm: %s", vm)
407435
if vms[vm]["status"] == "Running":
@@ -432,7 +460,6 @@ def vmContainerSubMenu(vm: str = "default"):
432460
433461
:param str vm:
434462
"""
435-
# plugin_f = __file__.replace(" ", "\ ")
436463
plugin_f = __file__
437464
containers = listContainers(vm=vm)
438465

@@ -505,12 +532,15 @@ def vmMenu(vmData: dict = {}):
505532
if vmData[vm]["status"] != "Running":
506533
print("%s VM is stopped | color=%s" % (vm, STOPPED_VM_COLOR))
507534
print(
508-
f"""-- Start {vm} VM | shell=\"{plugin_f}\" param1='--vm=default' param2='--vm-action=start' terminal=false refresh=true"""
535+
f"""-- ▶️ Start {vm} VM | shell=\"{plugin_f}\" param1='--vm={vm}' param2='--vm-action=start' terminal=false refresh=true"""
509536
)
510537
else:
511538
print(f"{vm} VM (running) | color={RUNNING_VM_COLOR}")
512539
print(
513-
f"""-- ⛔ Stop {vm} VM | color={STOPPED_VM_COLOR} bash="{__file__}" shell=\"{plugin_f}\" param1='--vm=default' param2='--vm-action=stop' terminal=false refresh=true"""
540+
f"""-- ❌ Stop {vm} VM | color={STOPPED_VM_COLOR} shell=\"{plugin_f}\" param1='--vm={vm}' param2='--vm-action=stop' terminal=false refresh=true"""
541+
)
542+
print(
543+
f"""-- 🐚 Run lima command | shell=\"{plugin_f}\" param1='--vm={vm}' param2='--vm-action=lima' terminal=false refresh=true"""
514544
)
515545
vmContainerSubMenu(vm=vm)
516546
vmImageSubMenu(vm=vm)

0 commit comments

Comments
 (0)