Skip to content

Add option to run arbitrary lima commands #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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