|
8 | 8 | import pathlib |
9 | 9 | import docker |
10 | 10 |
|
11 | | -md_iid = "0.5" |
12 | | -md_version = "1.3" |
| 11 | +md_iid = "1.0" |
| 12 | +md_version = "1.4" |
13 | 13 | md_name = "Docker" |
14 | 14 | md_description = "Control your docker instance" |
15 | 15 | md_license = "BSD-3" |
@@ -41,43 +41,55 @@ def initialize(self): |
41 | 41 | if not self.client: |
42 | 42 | raise "Failed to initialize client." |
43 | 43 |
|
| 44 | + def handleGlobalQuery(self, query): |
| 45 | + rank_items = [] |
44 | 46 |
|
45 | | - def handleQuery(self, query): |
46 | 47 | for container in self.client.containers.list(all=True): |
| 48 | + if query.string in container.name: |
| 49 | + # Create dynamic actions |
| 50 | + if container.status == 'running': |
| 51 | + actions = [ |
| 52 | + Action("stop", "Stop container", lambda c=container: c.stop()), |
| 53 | + Action("restart", "Restart container", lambda c=container: c.restart()) |
| 54 | + ] |
| 55 | + else: |
| 56 | + actions = [ |
| 57 | + Action("start", "Start container", lambda c=container: c.start()) |
| 58 | + ] |
| 59 | + actions.extend([ |
| 60 | + Action("logs", "Logs", lambda c=container.id: runTerminal("docker logs -f %s" % c, close_on_exit=False)), |
| 61 | + Action("remove", "Remove (forced, with volumes)", lambda c=container: c.remove(v=True, force=True)), |
| 62 | + Action("copy-id", "Copy id to clipboard", lambda id=container.id: setClipboardText(id)) |
| 63 | + ]) |
47 | 64 |
|
48 | | - # Create dynamic actions |
49 | | - if container.status == 'running': |
50 | | - actions = [ |
51 | | - Action("stop", "Stop container", lambda c=container: c.stop()), |
52 | | - Action("restart", "Restart container", lambda c=container: c.restart()) |
53 | | - ] |
54 | | - else: |
55 | | - actions = [ |
56 | | - Action("start", "Start container", lambda c=container: c.start()) |
57 | | - ] |
58 | | - actions.extend([ |
59 | | - Action("logs", "Logs", lambda c=container.id: runTerminal("docker logs -f %s" % c, close_on_exit=False)), |
60 | | - Action("remove", "Remove (forced, with volumes)", lambda c=container: c.remove(v=True, force=True)), |
61 | | - Action("copy-id", "Copy id to clipboard", lambda id=container.id: setClipboardText(id)) |
62 | | - ]) |
63 | | - |
64 | | - query.add(Item( |
65 | | - id=container.id, |
66 | | - text="%s (%s)" % (container.name, ", ".join(container.image.tags)), |
67 | | - subtext=container.id, |
68 | | - icon=self.icon_running if container.status == 'running' else self.icon_stopped, |
69 | | - actions=actions |
70 | | - )) |
| 65 | + rank_items.append(RankItem( |
| 66 | + item=Item( |
| 67 | + id=container.id, |
| 68 | + text="%s (%s)" % (container.name, ", ".join(container.image.tags)), |
| 69 | + subtext="Container: %s" % container.id, |
| 70 | + icon=self.icon_running if container.status == 'running' else self.icon_stopped, |
| 71 | + actions=actions |
| 72 | + ), |
| 73 | + score=0 # len(query.string)/len(container.name) |
| 74 | + )) |
71 | 75 |
|
72 | 76 | for image in reversed(self.client.images.list()): |
73 | | - query.add(Item( |
74 | | - id=image.short_id, |
75 | | - text=str(image.tags), |
76 | | - subtext=image.id, |
77 | | - icon=self.icon_stopped, |
78 | | - actions=[ |
79 | | - Action("run", "Run with command: %s" % query.string, |
80 | | - lambda i=image, s=query.string: client.containers.run(i, s)), |
81 | | - Action("rmi", "Remove image", lambda i=image: i.remove()) |
82 | | - ] |
83 | | - )) |
| 77 | + if any([query.string in tag for tag in image.tags]): |
| 78 | + rank_items.append(RankItem( |
| 79 | + item=Item( |
| 80 | + id=image.short_id, |
| 81 | + text=", ".join(image.tags), |
| 82 | + subtext="Image: %s" % image.id, |
| 83 | + icon=self.icon_stopped, |
| 84 | + actions=[ |
| 85 | + Action("run", "Run with command: %s" % query.string, |
| 86 | + lambda i=image, s=query.string: client.containers.run(i, s)), |
| 87 | + Action("rmi", "Remove image", lambda i=image: i.remove()) |
| 88 | + ] |
| 89 | + ), |
| 90 | + score=0 |
| 91 | + )) |
| 92 | + |
| 93 | + |
| 94 | + return rank_items |
| 95 | + |
0 commit comments