-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker_containers.1m.py
executable file
·83 lines (63 loc) · 2.04 KB
/
docker_containers.1m.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
# <swiftbar.hideDisablePlugin>true</swiftbar.hideDisablePlugin>
# <swiftbar.hideSwiftBar>true</swiftbar.hideSwiftBar>
import json
import subprocess
from dataclasses import dataclass
import plugin
PLUGIN_ICON = "🐋"
DOCKER_PATH = "/usr/local/bin/docker"
MONOSPACED_FONT = "SFMono-Regular"
@dataclass(frozen=True)
class Context:
name: str
current: bool
@dataclass(frozen=True)
class Container:
name: str
status: str
def main() -> None:
plugin.print_menu_item(PLUGIN_ICON)
plugin.print_menu_separator()
plugin.print_menu_item("Context")
cmd = subprocess.run(
[DOCKER_PATH, "context", "list", "--format=json"],
check=True,
text=True,
capture_output=True,
)
objects = list(json.loads(cmd.stdout))
contexts = [Context(obj["Name"], obj["Current"]) for obj in objects]
for ctx in contexts:
plugin.print_menu_action(
ctx.name,
[DOCKER_PATH, "context", "use", ctx.name],
refresh=True,
checked=ctx.current,
)
plugin.print_menu_separator()
plugin.print_menu_item("Containers")
try:
cmd = subprocess.run(
[DOCKER_PATH, "ps", "--format={{.Names}}\t{{.Status}}"],
check=True,
text=True,
capture_output=True,
)
except subprocess.CalledProcessError:
plugin.print_menu_item("docker daemon is not running", color="red")
return
containers = [Container(*line.split("\t")) for line in cmd.stdout.splitlines()]
if len(containers) == 0:
return
longest_name_length = max(len(ctn.name) for ctn in containers)
for ctn in containers:
plugin.print_menu_action(
f"{ctn.name:<{longest_name_length}} {ctn.status}",
[DOCKER_PATH, "logs", ctn.name],
open_terminal=True,
font=MONOSPACED_FONT, # use a monospaced font for a proper alignment
)
if __name__ == "__main__":
main()