Skip to content

Commit 7927625

Browse files
committed
Add option to pull a new image
We can now pull new images, not just pull existing ones. Closes #17 Signed-off-by: Joe Block <[email protected]>
1 parent 4f6735d commit 7927625

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

Diff for: Makefile

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

Diff for: README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222

2323
## Description
2424

25+
This plugin is compatible with [xbar](https://xbarapp.com/) and [SwiftBar](https://github.com/swiftbar/SwiftBar), and provides a menubar app that creates a Lima menubar option with submenus for each Lima VM on your machine. For each VM, you can:
26+
27+
- start/stop the VM
28+
- stop, start or remove stopped containers
29+
- pull or remove images from the VM
30+
2531
### Screen shots
2632

2733
![Screen shot of xbar menu with container submenu for a running vm](https://raw.githubusercontent.com/unixorn/unixorn.github.io/master/images/lima-xbar/containers-submenu.png)
2834

2935
![Screen shot of xbar menu with image submenu for a running vm](https://raw.githubusercontent.com/unixorn/unixorn.github.io/master/images/lima-xbar/images-submenu.png)
3036

3137

32-
This plugin is compatible with [xbar](https://xbarapp.com/) and [SwiftBar](https://github.com/swiftbar/SwiftBar), and provides a menubar app that creates submenus for each Lima VM on your machine. For each VM, you can start/stop the VM, stop (and start or remove stopped containers) containers, and pull or remove images from the VM.
3338

3439
## Installation
3540

Diff for: lima-plugin

+63-10
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.1.1"
37+
VERSION = "1.2.0"
3838

3939

4040
def logSetup(level: str = "INFO"):
@@ -87,6 +87,7 @@ def parseCLI():
8787
parser.add_argument(
8888
"--image-action", choices=["pull", "rm"], help="Action to perform on image"
8989
)
90+
parser.add_argument("--pull-new-image", action="store_true")
9091
parser.add_argument(
9192
"--vm-action",
9293
choices=["start", "stop"],
@@ -121,12 +122,35 @@ def displayNotification(title: str, message: str):
121122
runCommand(command=["osascript", "-e", alertCommand])
122123

123124

125+
def inputDialog(user_prompt: str, icon: str = "note"):
126+
"""
127+
Uses osascript to present a dialog with a prompt and returns the user's answer.
128+
129+
:param str prompt:
130+
:param str icon: note,
131+
132+
:return str:
133+
"""
134+
valid_icons = ["caution", "note", "stop"]
135+
if icon.lower() not in valid_icons:
136+
icon = "note"
137+
138+
applescript = f"""set dialogText to text returned of (display dialog "{user_prompt}" default answer "")
139+
return dialogText
140+
"""
141+
142+
answer = runCommand(command=["osascript", "-e", applescript]).strip()
143+
logging.debug(f"Asked {user_prompt} , got answer: {answer}")
144+
return answer
145+
146+
124147
def runCommand(command: list, env=dict(os.environ)):
125148
"""
126-
Run a command and decode the json output
149+
Run a command and return the decoded output
127150
128151
:param list command:
129-
:return dict:
152+
153+
:return str:
130154
"""
131155
return subprocess.run(command, env=env, stdout=subprocess.PIPE).stdout.decode(
132156
"utf-8"
@@ -243,7 +267,7 @@ def listVMs():
243267
env["PATH"] = newpath
244268

245269
vmRaw = subprocess.run(
246-
["/usr/local/bin/limactl", "list", "--json"], env=env, stdout=subprocess.PIPE
270+
["limactl", "list", "--json"], env=env, stdout=subprocess.PIPE
247271
).stdout.decode("utf-8")
248272

249273
for vm in vmRaw.splitlines():
@@ -346,6 +370,26 @@ def vmOps(action: str, vm: str = "default"):
346370
displayNotification(title="Task completed", message=" ".join(command))
347371

348372

373+
def pullNewImage(vm: str = "default"):
374+
"""
375+
Pulls a new image.
376+
377+
Args:
378+
vm (str, optional): Which VM to pull the new image into. Defaults to 'default'.
379+
"""
380+
env = prep_environment_for_lima(vm=vm)
381+
image = inputDialog(user_prompt=f"What image should we pull into VM {vm}?")
382+
if image != "":
383+
pull_command = ["lima", "nerdctl", "image", "pull", image]
384+
displayNotification(
385+
title=f"Pulling image {image}", message=" ".join(pull_command)
386+
)
387+
runCommand(command=pull_command, env=env)
388+
displayNotification(title=f"Pulling image {image}", message="Completed")
389+
else:
390+
displayAlert(title="Error!", message="No image specified")
391+
392+
349393
# Actual Xbar-compatible output
350394

351395

@@ -434,6 +478,9 @@ def vmImageSubMenu(vm: str = "default"):
434478
logging.debug("images: %s", images)
435479

436480
print("-- Images")
481+
print(
482+
f'---- pull new image| bash="{plugin_f}" param1=--vm param2={vm} param3=--pull-new-image terminal=false refresh=true'
483+
)
437484
for image in images:
438485
print("---- %s" % image)
439486
print(
@@ -484,30 +531,36 @@ def main():
484531
"""
485532
Main program driver
486533
"""
487-
logSetup(level="DEBUG")
488-
489-
logging.debug("plugin path: %s" % __file__)
490534

491535
cli = parseCLI()
492-
logging.warning("VERSON: %s", VERSION)
536+
logSetup(level=cli.log_level)
537+
538+
logging.debug("plugin path: %s" % __file__)
539+
logging.debug("VERSON: %s", VERSION)
493540

494541
logging.debug("cli: %s" % cli)
495542

496-
logging.warning("argv[0] %s" % sys.argv[0])
543+
logging.info("argv[0] %s" % sys.argv[0])
544+
545+
xbarMenu()
497546

498547
if cli.container_action:
499548
logging.info("container action: %s", cli.container_action)
500549
containerOps(vm=cli.vm, action=cli.container_action, container=cli.target)
550+
sys.exit()
501551

502552
if cli.image_action:
503553
logging.info("image action: %s", cli.image_action)
504554
imageOps(action=cli.image_action, image=cli.target, vm=cli.vm)
555+
sys.exit()
505556

506557
if cli.vm_action:
507558
logging.info("vm action: %s", cli.vm_action)
508559
vmOps(action=cli.vm_action, vm=cli.vm)
560+
sys.exit()
509561

510-
xbarMenu()
562+
if cli.pull_new_image:
563+
pullNewImage(vm=cli.vm)
511564

512565

513566
if __name__ == "__main__":

0 commit comments

Comments
 (0)