Skip to content

Commit

Permalink
Add option to run arbitrary lima commands
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
unixorn committed Oct 6, 2021
1 parent 14d9cc8 commit ffe6081
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This plugin is compatible with [xbar](https://xbarapp.com/) and [SwiftBar](https
- start/stop the VM
- stop, start or remove stopped containers
- pull or remove images from the VM
- Run an arbitrary command inside the VM with `lima`

### Screen shots

Expand Down
56 changes: 43 additions & 13 deletions lima-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ RUNNING_VM_COLOR = "#29cc00"
# Stopped VM color (default red)
STOPPED_VM_COLOR = "#ff0033"

VERSION = "1.2.0"
VERSION = "1.3.0"


def logSetup(level: str = "INFO"):
Expand Down Expand Up @@ -90,7 +90,7 @@ def parseCLI():
parser.add_argument("--pull-new-image", action="store_true")
parser.add_argument(
"--vm-action",
choices=["start", "stop"],
choices=["start", "stop", "lima"],
help="Action to perform on vm",
)
cliArgs = parser.parse_args()
Expand Down Expand Up @@ -348,6 +348,28 @@ def imageOps(action: str, image: str, vm: str = "default"):
displayNotification(title="Task complete", message=" ".join(command))


def limaCommand(vm: str):
"""
Run an arbitrary command with lima
:param str vm: Which vm to run the command in
"""
env = prep_environment_for_lima(vm=vm)
user_command = inputDialog(
user_prompt=f"What lima command do you want to run in the {vm} VM?"
)

if user_command != "":
lima_command = ["lima"] + user_command.split()
displayNotification(
title=f"Running '{user_command}'", message=" ".join(lima_command)
)
runCommand(command=lima_command, env=env)
displayNotification(title=f"Ran '{user_command}' in {vm}", message="Completed")
else:
displayAlert(title="Error!", message="No nerdctl command specified")


def vmOps(action: str, vm: str = "default"):
"""
Handle VM operations
Expand All @@ -361,13 +383,19 @@ def vmOps(action: str, vm: str = "default"):

env = prep_environment_for_lima(vm=vm)

command = ["limactl", action, vm]
logging.warning("command: %s", command)
displayNotification(title="Lima VM", message=" ".join(command))
output = runCommand(command=command, env=env)
logging.debug(output)
logging.warning("%s complete", action)
displayNotification(title="Task completed", message=" ".join(command))
if action == "lima":
limaCommand(vm=vm)

if action in ["start", "stop"]:
command = ["limactl", action, vm]
logging.info("command: %s", command)

displayNotification(title="Lima VM", message=" ".join(command))
output = runCommand(command=command, env=env)
logging.debug(output)

logging.info("%s complete", action)
displayNotification(title="Task completed", message=" ".join(command))


def pullNewImage(vm: str = "default"):
Expand Down Expand Up @@ -401,7 +429,7 @@ def xbar_icon(vms: dict = {}):
:param dict vms: Data about Lima VMs
"""
menuBarIcon = f"πŸ‹ β›” | color={STOPPED_VM_COLOR}"
menuBarIcon = f"πŸ‹β— | color={STOPPED_VM_COLOR}"
for vm in vms:
logging.debug("vm: %s", vm)
if vms[vm]["status"] == "Running":
Expand Down Expand Up @@ -432,7 +460,6 @@ def vmContainerSubMenu(vm: str = "default"):
:param str vm:
"""
# plugin_f = __file__.replace(" ", "\ ")
plugin_f = __file__
containers = listContainers(vm=vm)

Expand Down Expand Up @@ -505,12 +532,15 @@ def vmMenu(vmData: dict = {}):
if vmData[vm]["status"] != "Running":
print("%s VM is stopped | color=%s" % (vm, STOPPED_VM_COLOR))
print(
f"""-- β›” Start {vm} VM | shell=\"{plugin_f}\" param1='--vm=default' param2='--vm-action=start' terminal=false refresh=true"""
f"""-- ▢️ Start {vm} VM | shell=\"{plugin_f}\" param1='--vm={vm}' param2='--vm-action=start' terminal=false refresh=true"""
)
else:
print(f"{vm} VM (running) | color={RUNNING_VM_COLOR}")
print(
f"""-- β›” Stop {vm} VM | color={STOPPED_VM_COLOR} bash="{__file__}" shell=\"{plugin_f}\" param1='--vm=default' param2='--vm-action=stop' terminal=false refresh=true"""
f"""-- ❌ Stop {vm} VM | color={STOPPED_VM_COLOR} shell=\"{plugin_f}\" param1='--vm={vm}' param2='--vm-action=stop' terminal=false refresh=true"""
)
print(
f"""-- 🐚 Run lima command | shell=\"{plugin_f}\" param1='--vm={vm}' param2='--vm-action=lima' terminal=false refresh=true"""
)
vmContainerSubMenu(vm=vm)
vmImageSubMenu(vm=vm)
Expand Down

0 comments on commit ffe6081

Please sign in to comment.