Build the Apple Container extension

Turns the empty scaffold into a working Nova extension for Apple Container
and container-compose, in the shape of the Docker Suite extension.

Sidebar (Scripts/Sidebar):
- System, Containers, Images, Volumes and Networks sections
- Containers grouped into compose projects via the com.docker.compose.project
  label container-compose stamps; project rows drive compose up/down/build
  against the matching workspace file
- Lifecycle, shell, logs, inspect, browse and copy commands per resource
- Apple Container publishes no event stream, so the sidebar polls while
  visible and only reloads a section when a fingerprint of its data changes,
  which keeps selection and expansion intact

Hostnames (Scripts/Hostnames.js):
- A container's hostname is <name>.<domain> and the domain lives in Apple
  Container's config.toml, which has no CLI setter, so the Hostname Domain
  preference reads and writes that file directly
- Changing it offers to register the domain with macOS and restart the
  services; the write preserves the file's other tables and its mode

Language support:
- Dockerfile and Compose syntaxes backed by tree-sitter grammars built by
  Tools/build-syntaxes.sh, pinned to revisions that generate ABI 14
- The Compose syntax is named dockercompose because that is the languageId
  docker-language-server recognises compose files by
- docker-language-server is downloaded on first use rather than bundled, at
  roughly 40 MB

Icons are generated by Tools/make-icons.py so they can be regenerated rather
than hand-maintained.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UnSBcR5Lywz5Fbj5FmVbfc
This commit is contained in:
2026-08-18 22:45:14 -03:00
co-authored by Claude Opus 5
parent 0879e5a163
commit 0506f44db3
80 changed files with 5201 additions and 3 deletions
+388
View File
@@ -0,0 +1,388 @@
//
// Containers section, grouped by compose project.
//
const { Controller } = require("./Controller");
const { ContainerTreeItem } = require("../TreeItems/ContainerTreeItem");
const { ProjectTreeItem } = require("../TreeItems/ProjectTreeItem");
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
const { ComposeCLI } = require("../../Interfaces/ComposeCLI");
const { Terminal } = require("../../Interfaces/Terminal");
const containers = require("../Models/Container");
const notify = require("../../Utilities/Notify");
const prompt = require("../../Utilities/Prompt");
class ContainersController extends Controller {
constructor(sidebar) {
super("apple_container.containers");
this.sidebar = sidebar;
this.registerCommands();
}
async fetch() {
let list = await ContainerCLI.listContainers();
let group = nova.config.get("apple_container.containers.group", "boolean") !== false;
let elements = containers.build(list, { group: group });
// Attach the compose file to each project so the compose commands and
// the tooltip have something to work with.
for (let element of elements) {
if (element.compose) {
element.file = this.composeFileFor(element.name);
}
}
return elements;
}
getTreeItem(element) {
return element instanceof containers.ProjectModel
? new ProjectTreeItem(element)
: new ContainerTreeItem(element);
}
// Containers in the current selection, expanding any selected projects.
selectedContainers() {
let selected = [];
for (let element of this.selection) {
if (element instanceof containers.ProjectModel) {
selected = selected.concat(element.children);
} else {
selected.push(element);
}
}
return selected;
}
selectedProjects() {
return this.selection.filter((element) => element instanceof containers.ProjectModel);
}
// Finds the workspace compose file whose project name matches, which is how
// a sidebar row is tied back to a file on disk.
composeFileFor(name) {
for (let file of ComposeCLI.workspaceFiles()) {
if (ComposeCLI.projectName(file) === name) {
return file;
}
}
let fallback = ComposeCLI.defaultFile();
if (fallback && ComposeCLI.projectName(fallback) === name) {
return fallback;
}
return null;
}
// `build` receives the resolved compose file: the arguments cannot be built
// before the file is known.
async runCompose(project, build, title) {
let file = project.file || this.composeFileFor(project.name);
if (!file) {
notify.failure(
`No compose file for “${project.name}`,
"Open the project's workspace, or set the compose file in workspace settings.",
"apple-container-compose-missing"
);
return;
}
let directory = nova.path.dirname(file);
let args = build(file);
if (nova.config.get("apple_container.compose.terminal", "boolean") !== false) {
await Terminal.run(Terminal.command([ComposeCLI.path()].concat(args)), directory);
return;
}
try {
await ComposeCLI.run(args, { cwd: directory });
} catch (error) {
notify.failure(title, error, "apple-container-compose");
} finally {
this.reload();
}
}
registerCommands() {
this.register("apple_container.containers.reload", () => this.reload());
this.register("apple_container.containers.start", async () => {
let ids = this.selectedContainers().map((container) => container.id);
try {
await ContainerCLI.startContainers(ids);
} catch (error) {
notify.failure("Unable to start container", error, "apple-container-start");
}
this.reload();
});
this.register("apple_container.containers.stop", () =>
this.runOnSelection((ids) => ContainerCLI.stopContainers(ids), "Unable to stop container")
);
this.register("apple_container.containers.restart", () =>
this.runOnSelection((ids) => ContainerCLI.restartContainers(ids), "Unable to restart container")
);
this.register("apple_container.containers.kill", () =>
this.runOnSelection((ids) => ContainerCLI.killContainers(ids), "Unable to kill container")
);
this.register("apple_container.containers.startGroup", async () => {
let ids = this.selectedProjects()
.reduce((all, project) => all.concat(project.children), [])
.filter((container) => !container.running)
.map((container) => container.id);
try {
await ContainerCLI.startContainers(ids);
} catch (error) {
notify.failure("Unable to start containers", error, "apple-container-start");
}
this.reload();
});
this.register("apple_container.containers.stopGroup", async () => {
let ids = this.selectedProjects()
.reduce((all, project) => all.concat(project.children), [])
.filter((container) => container.running)
.map((container) => container.id);
if (ids.length === 0) {
return;
}
try {
await ContainerCLI.stopContainers(ids);
} catch (error) {
notify.failure("Unable to stop containers", error, "apple-container-stop");
}
this.reload();
});
this.register("apple_container.containers.delete", async () => {
let selected = this.selectedContainers();
if (selected.length === 0) {
return;
}
let running = selected.some((container) => container.running);
let names = selected.map((container) => container.name).join(", ");
let message = running
? `Delete ${names}? Running containers will be stopped first.`
: `Delete ${names}?`;
if (!(await prompt.confirm(message))) {
return;
}
try {
await ContainerCLI.deleteContainers(selected.map((container) => container.id), running);
} catch (error) {
notify.failure("Unable to delete container", error, "apple-container-delete");
}
this.reload();
});
this.register("apple_container.containers.prune", async () => {
if (!(await prompt.confirm("Remove all stopped containers?", "Remove"))) {
return;
}
try {
await ContainerCLI.pruneContainers();
} catch (error) {
notify.failure("Unable to prune containers", error, "apple-container-prune");
}
this.reload();
});
this.register("apple_container.containers.shell", async () => {
let shell = nova.config.get("apple_container.cli.shell", "string") || "/bin/sh";
for (let container of this.selectedContainers()) {
await Terminal.run(
Terminal.command([ContainerCLI.path(), "exec", "--interactive", "--tty", container.id, shell])
);
}
});
this.register("apple_container.containers.follow", async () => {
for (let container of this.selectedContainers()) {
await Terminal.run(Terminal.command([ContainerCLI.path(), "logs", "--follow", container.id]));
}
});
this.register("apple_container.containers.logs", () => this.openLogs([]));
this.register("apple_container.containers.bootLogs", () => this.openLogs(["--boot"]));
this.register("apple_container.containers.browse", async () => {
let url = await this.chooseURL();
if (url) {
nova.openURL(url);
}
});
this.register("apple_container.containers.inspect", async () => {
for (let container of this.selectedContainers()) {
try {
let output = await ContainerCLI.inspectContainer(container.id);
nova.workspace.openNewTextDocument({ content: output, syntax: "json" });
} catch (error) {
notify.failure("Unable to inspect container", error, "apple-container-inspect");
}
}
});
this.register("apple_container.containers.copy", async () => {
let container = this.selectedContainers()[0];
if (!container) {
return;
}
let fields = [["Name", container.name], ["Image", container.image]];
if (container.address) {
fields.push(["Address", container.address]);
}
if (container.hostname) {
fields.push(["Hostname", container.hostname]);
}
if (container.url) {
fields.push(["URL", container.url]);
}
let choice = await prompt.choose(
fields.map(([label, value]) => `${label}: ${value}`),
{ placeholder: "Copy" }
);
if (choice) {
nova.clipboard.writeText(choice.slice(choice.indexOf(": ") + 2));
}
});
this.register("apple_container.compose.upProject", async () => {
for (let project of this.selectedProjects()) {
await this.runCompose(project, (file) => ComposeCLI.upArgs(file), "Compose up failed");
}
});
this.register("apple_container.compose.downProject", async () => {
for (let project of this.selectedProjects()) {
await this.runCompose(project, (file) => ComposeCLI.downArgs(file), "Compose down failed");
}
});
this.register("apple_container.compose.buildProject", async () => {
for (let project of this.selectedProjects()) {
await this.runCompose(project, (file) => ComposeCLI.buildArgs(file), "Compose build failed");
}
});
this.register("apple_container.compose.openFile", () => {
for (let project of this.selectedProjects()) {
let file = project.file || this.composeFileFor(project.name);
if (file) {
nova.workspace.openFile(file);
}
}
});
for (let key of [
"apple_container.containers.group",
"apple_container.containers.label",
"apple_container.containers.icons",
"apple_container.containers.desc.image",
"apple_container.containers.desc.ports",
"apple_container.containers.desc.address",
"apple_container.containers.desc.state"
]) {
this.watch(key, () => this.reload());
}
}
async runOnSelection(action, message) {
let ids = this.selectedContainers().map((container) => container.id);
if (ids.length === 0) {
return;
}
try {
await action(ids);
} catch (error) {
notify.failure(message, error, "apple-container-action");
}
this.reload();
}
// Picks the address to open: straight through when there is only one, and a
// palette when a container publishes several ports.
async chooseURL() {
let container = this.selectedContainers()[0];
if (!container) {
return null;
}
let urls = container.urls;
if (urls.length === 0) {
notify.info(
`Nothing to open for ${container.name}`,
container.running
? "The container publishes no ports and has no address yet."
: "The container is not running.",
"apple-container-browse"
);
return null;
}
if (urls.length === 1) {
return urls[0].url;
}
let choice = await prompt.choose(urls.map((entry) => entry.label), {
placeholder: `Open ${container.name}`
});
if (!choice) {
return null;
}
let picked = urls.find((entry) => entry.label === choice);
return picked ? picked.url : null;
}
async openLogs(flags) {
for (let container of this.selectedContainers()) {
try {
let result = await ContainerCLI.run(["logs"].concat(flags, [container.id]));
let output = result.stdout || result.stderr || "No log output.";
nova.workspace.openNewTextDocument({ content: output });
} catch (error) {
notify.failure("Unable to read logs", error, "apple-container-logs");
}
}
}
}
module.exports = { ContainersController };
+151
View File
@@ -0,0 +1,151 @@
//
// Base class for the sidebar's tree controllers.
//
// Apple Container has no event stream, so the sidebar polls. Reloading a
// TreeView drops the user's selection, so each refresh compares a fingerprint
// of the fetched data and only reloads when something actually changed.
//
class Controller {
constructor(id) {
this.id = id;
this.elements = [];
this.fingerprint = null;
// Collapsed rather than expanded, so groups keep their default of open.
this.collapsed = new Set();
this.disposables = new CompositeDisposable();
this.tree = new TreeView(id, { dataProvider: this });
this.disposables.add(this.tree);
this.disposables.add(
this.tree.onDidExpandElement((element) => {
this.collapsed.delete(element.identifier);
element.expanded = true;
})
);
this.disposables.add(
this.tree.onDidCollapseElement((element) => {
this.collapsed.add(element.identifier);
element.expanded = false;
})
);
}
// Subclasses return the elements to display, or null to leave the tree as
// it is (used when the backing service is unavailable).
async fetch() {
return [];
}
// A stable string describing the current data. When it does not change,
// the tree is left alone.
signature(elements) {
let describe = (element) => [element.identifier, element.revision];
return JSON.stringify(
elements.map((element) => describe(element).concat([(element.children || []).map(describe)]))
);
}
get visible() {
// TreeView.visible is only meaningful once the sidebar has been shown.
return this.tree.visible !== false;
}
async refresh(options = {}) {
let elements;
try {
elements = await this.fetch();
} catch (error) {
if (nova.inDevMode()) {
console.warn(`${this.id}: ${error.message}`);
}
elements = [];
}
if (elements === null) {
return;
}
this.restoreExpansion(elements);
let signature = this.signature(elements);
if (!options.force && signature === this.fingerprint) {
return;
}
this.fingerprint = signature;
this.elements = elements;
this.tree.reload();
}
// Rebuilt models are fresh objects, so expansion has to be carried over.
restoreExpansion(elements) {
for (let element of elements) {
if (!element.children || element.children.length === 0) {
continue;
}
if (this.collapsed.has(element.identifier)) {
element.expanded = false;
}
}
}
// Empties the tree, for when the backing service goes away.
clear() {
if (this.elements.length === 0) {
return;
}
this.elements = [];
this.fingerprint = null;
this.tree.reload();
}
// Invalidates the fingerprint so the next refresh always reloads.
invalidate() {
this.fingerprint = null;
}
reload() {
this.invalidate();
return this.refresh({ force: true });
}
get selection() {
return this.tree.selection || [];
}
getChildren(element) {
if (!element) {
return this.elements;
}
return element.children || [];
}
getParent(element) {
return element.parent;
}
getTreeItem() {
throw new Error("getTreeItem must be implemented by the controller.");
}
register(command, handler) {
this.disposables.add(nova.commands.register(command, handler));
}
watch(key, handler) {
this.disposables.add(nova.config.onDidChange(key, handler));
}
dispose() {
this.disposables.dispose();
}
}
module.exports = { Controller };
+198
View File
@@ -0,0 +1,198 @@
//
// Images section, optionally grouped by repository.
//
const { Controller } = require("./Controller");
const { ImageTreeItem, RepositoryTreeItem } = require("../TreeItems/ImageTreeItem");
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
const { Terminal } = require("../../Interfaces/Terminal");
const images = require("../Models/Image");
const notify = require("../../Utilities/Notify");
const prompt = require("../../Utilities/Prompt");
class ImagesController extends Controller {
constructor(sidebar) {
super("apple_container.images");
this.sidebar = sidebar;
this.registerCommands();
}
async fetch() {
let list = await ContainerCLI.listImages();
return images.build(list, {
group: nova.config.get("apple_container.images.group", "boolean") !== false,
registry: nova.config.get("apple_container.images.registry", "boolean") === true,
infrastructure: nova.config.get("apple_container.images.infra", "boolean") === true
});
}
getTreeItem(element) {
if (element instanceof images.RepositoryModel) {
return new RepositoryTreeItem(element);
}
return new ImageTreeItem(element, { grouped: element.parent !== null });
}
// References for the current selection, expanding repositories into tags.
selectedReferences() {
let selected = [];
for (let element of this.selection) {
if (element instanceof images.RepositoryModel) {
selected = selected.concat(element.children.map((image) => image.reference));
} else {
selected.push(element.reference);
}
}
return selected;
}
registerCommands() {
this.register("apple_container.images.reload", () => this.reload());
this.register("apple_container.images.run", async () => {
let reference = this.selectedReferences()[0];
if (!reference) {
return;
}
await runImage(reference);
this.sidebar.reloadContainers();
});
this.register("apple_container.images.pull", async () => {
for (let reference of this.selectedReferences()) {
try {
await ContainerCLI.pullImage(reference);
} catch (error) {
notify.failure(`Unable to pull ${reference}`, error, "apple-container-pull");
}
}
this.reload();
});
this.register("apple_container.images.push", async () => {
for (let reference of this.selectedReferences()) {
try {
await ContainerCLI.pushImage(reference);
notify.info("Push complete", reference, "apple-container-push");
} catch (error) {
notify.failure(`Unable to push ${reference}`, error, "apple-container-push");
}
}
});
this.register("apple_container.images.tag", async () => {
let reference = this.selectedReferences()[0];
if (!reference) {
return;
}
let target = await prompt.input("New tag", { placeholder: reference, value: reference });
if (!target) {
return;
}
try {
await ContainerCLI.tagImage(reference, target);
} catch (error) {
notify.failure("Unable to tag image", error, "apple-container-tag");
}
this.reload();
});
this.register("apple_container.images.inspect", async () => {
for (let reference of this.selectedReferences()) {
try {
let output = await ContainerCLI.inspectImage(reference);
nova.workspace.openNewTextDocument({ content: output, syntax: "json" });
} catch (error) {
notify.failure("Unable to inspect image", error, "apple-container-inspect");
}
}
});
this.register("apple_container.images.copy", () => {
let reference = this.selectedReferences()[0];
if (reference) {
nova.clipboard.writeText(reference);
}
});
this.register("apple_container.images.delete", async () => {
let references = this.selectedReferences();
if (references.length === 0) {
return;
}
if (!(await prompt.confirm(`Delete ${references.join(", ")}?`))) {
return;
}
try {
await ContainerCLI.deleteImages(references);
} catch (error) {
notify.failure("Unable to delete image", error, "apple-container-delete-image");
}
this.reload();
});
this.register("apple_container.images.prune", async () => {
if (!(await prompt.confirm("Remove images not referenced by a container?", "Remove"))) {
return;
}
try {
await ContainerCLI.pruneImages();
} catch (error) {
notify.failure("Unable to prune images", error, "apple-container-prune-image");
}
this.reload();
});
for (let key of [
"apple_container.images.group",
"apple_container.images.registry",
"apple_container.images.infra"
]) {
this.watch(key, () => this.reload());
}
}
}
// Runs an image in the terminal, so its output is visible and an interactive
// image still works. Arguments are offered because `container run` takes no
// defaults worth guessing.
async function runImage(reference) {
let options = await prompt.input("Arguments for container run", {
placeholder: "--detach --name my-container --publish 8080:80"
});
if (options === null) {
return null;
}
let parts = [ContainerCLI.path(), "run"];
if (options) {
parts = parts.concat(options.split(/\s+/));
}
parts.push(reference);
return Terminal.run(Terminal.command(parts));
}
module.exports = { ImagesController, runImage };
+114
View File
@@ -0,0 +1,114 @@
//
// Networks section.
//
const { Controller } = require("./Controller");
const { NetworkTreeItem } = require("../TreeItems/NetworkTreeItem");
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
const networks = require("../Models/Network");
const notify = require("../../Utilities/Notify");
const prompt = require("../../Utilities/Prompt");
class NetworksController extends Controller {
constructor(sidebar) {
super("apple_container.networks");
this.sidebar = sidebar;
this.registerCommands();
}
async fetch() {
return networks.build(await ContainerCLI.listNetworks());
}
getTreeItem(element) {
return new NetworkTreeItem(element);
}
registerCommands() {
this.register("apple_container.networks.reload", () => this.reload());
this.register("apple_container.networks.create", async () => {
// A name on its own is the common case, but `container network
// create` also takes --subnet, --internal and friends, so the whole
// argument line is accepted here.
let answer = await prompt.input("Network name, and any options", {
placeholder: "backend --subnet 192.168.70.0/24"
});
if (!answer) {
return;
}
try {
await ContainerCLI.createNetwork(answer.split(/\s+/));
} catch (error) {
notify.failure("Unable to create network", error, "apple-container-network");
}
this.reload();
});
this.register("apple_container.networks.inspect", async () => {
for (let network of this.selection) {
try {
let output = await ContainerCLI.inspectNetwork(network.name);
nova.workspace.openNewTextDocument({ content: output, syntax: "json" });
} catch (error) {
notify.failure("Unable to inspect network", error, "apple-container-inspect");
}
}
});
this.register("apple_container.networks.copy", () => {
let network = this.selection[0];
if (network) {
nova.clipboard.writeText(network.subnet || network.name);
}
});
this.register("apple_container.networks.delete", async () => {
let selected = this.selection;
let builtin = selected.filter((network) => network.builtin);
if (builtin.length > 0) {
notify.info(
"Built-in network",
`${builtin.map((network) => network.name).join(", ")} is managed by Apple Container and cannot be deleted.`,
"apple-container-network-builtin"
);
return;
}
let names = selected.map((network) => network.name);
if (names.length === 0 || !(await prompt.confirm(`Delete ${names.join(", ")}?`))) {
return;
}
try {
await ContainerCLI.deleteNetworks(names);
} catch (error) {
notify.failure("Unable to delete network", error, "apple-container-delete-network");
}
this.reload();
});
this.register("apple_container.networks.prune", async () => {
if (!(await prompt.confirm("Remove networks with no connected containers?", "Remove"))) {
return;
}
try {
await ContainerCLI.pruneNetworks();
} catch (error) {
notify.failure("Unable to prune networks", error, "apple-container-prune-network");
}
this.reload();
});
}
}
module.exports = { NetworksController };
+221
View File
@@ -0,0 +1,221 @@
//
// System section: the container services, the builder, disk usage and any
// local DNS domains.
//
const { Controller } = require("./Controller");
const { SystemTreeItem } = require("../TreeItems/SystemTreeItem");
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
const systemConfig = require("../../Utilities/SystemConfig");
const format = require("../../Utilities/Format");
const { Terminal } = require("../../Interfaces/Terminal");
const notify = require("../../Utilities/Notify");
const prompt = require("../../Utilities/Prompt");
class SystemController extends Controller {
constructor(sidebar) {
super("apple_container.system");
this.sidebar = sidebar;
this.running = false;
this.registerCommands();
}
async fetch() {
let status = await ContainerCLI.systemStatus();
let running = status.status === "running";
let rows = [];
this.running = running;
rows.push({
identifier: "system.services",
label: "Services",
running: running,
contextValue: running ? "system.services.running" : "system.services.stopped",
description: running ? version(status.apiServerVersion) : "stopped",
tooltip: statusTooltip(status),
copy: status.apiServerVersion || "",
children: []
});
if (!running) {
return rows;
}
let [builder, usage, domains] = await Promise.all([
ContainerCLI.builderRunning(),
ContainerCLI.systemDiskUsage(),
ContainerCLI.dnsDomains()
]);
rows.push({
identifier: "system.builder",
label: "Builder",
running: builder,
contextValue: builder ? "system.builder.running" : "system.builder.stopped",
description: builder ? "running" : "stopped",
tooltip: "The builder container backing `container build`.",
copy: "builder",
children: []
});
if (usage) {
rows.push({
identifier: "system.disk",
label: "Disk Usage",
contextValue: "system.disk",
image: "icons/system.png",
description: usageSummary(usage),
tooltip: usageTooltip(usage),
copy: usageSummary(usage),
children: []
});
}
// The domain containers are named under, which is separate from the
// domains macOS has been told to resolve.
let hostnameDomain = systemConfig.defaultDomain();
let resolved = hostnameDomain !== null && domains.includes(hostnameDomain);
rows.push({
identifier: "system.dns.default",
label: "Hostname Domain",
contextValue: "system.dns.default",
image: hostnameDomain && !resolved ? "icons/status/warning.png" : "icons/network.png",
description: hostnameDomain ? `.${hostnameDomain}` : "not set",
tooltip: hostnameTooltip(hostnameDomain, resolved),
copy: hostnameDomain || "",
children: []
});
for (let domain of domains) {
rows.push({
identifier: `system.dns.${domain}`,
label: domain,
contextValue: "system.dns",
image: "icons/network.png",
description: "resolved by macOS",
tooltip: `macOS resolves *.${domain} through Apple Container's DNS service.`,
copy: domain,
children: []
});
}
return rows;
}
signature(rows) {
return JSON.stringify(rows.map((row) => [row.identifier, row.description, row.running]));
}
getTreeItem(element) {
return new SystemTreeItem(element);
}
registerCommands() {
this.register("apple_container.system.df", async () => {
try {
let result = await ContainerCLI.run(["system", "df", "--format", "json"]);
nova.workspace.openNewTextDocument({ content: result.stdout, syntax: "json" });
} catch (error) {
notify.failure("Unable to read disk usage", error, "apple-container-df");
}
});
this.register("apple_container.dns.unregister", async () => {
let row = this.selection[0];
if (!row || !row.copy) {
return;
}
if (
!(await prompt.confirm(
`Stop macOS resolving *.${row.copy}? This runs sudo in your terminal.`,
"Unregister"
))
) {
return;
}
await Terminal.run(
`sudo ${Terminal.command([ContainerCLI.path(), "system", "dns", "delete", row.copy])}`
);
});
this.register("apple_container.system.copy", () => {
let row = this.selection[0];
if (row && row.copy) {
nova.clipboard.writeText(row.copy);
}
});
}
}
function hostnameTooltip(domain, resolved) {
if (!domain) {
return [
"Containers have unqualified hostnames.",
"Set a domain to reach them as <name>.<domain>."
].join("\n");
}
if (resolved) {
return `Containers are reachable as <name>.${domain} from this Mac.`;
}
return [
`Containers are named <name>.${domain}, but macOS does not resolve that domain yet.`,
"Use Set Up Container Hostnames to register it."
].join("\n");
}
function version(text) {
if (!text) {
return "running";
}
let match = String(text).match(/version\s+(\S+)/);
return match ? `running ${match[1]}` : "running";
}
function statusTooltip(status) {
if (status.status !== "running") {
return "Container services are not running.\nUse Start Services to launch them.";
}
return [
`Status:\t${status.status}`,
status.apiServerVersion ? `Version:\t${status.apiServerVersion}` : "",
status.appRoot ? `App root:\t${status.appRoot}` : "",
status.installRoot ? `Install:\t${status.installRoot}` : ""
]
.filter((line) => line.length > 0)
.join("\n");
}
function usageSummary(usage) {
let total = ["images", "containers", "volumes"].reduce(
(sum, key) => sum + ((usage[key] && usage[key].sizeInBytes) || 0),
0
);
let reclaimable = ["images", "containers", "volumes"].reduce(
(sum, key) => sum + ((usage[key] && usage[key].reclaimable) || 0),
0
);
return `${format.bytes(total)} ${format.bytes(reclaimable)} reclaimable`;
}
function usageTooltip(usage) {
return ["images", "containers", "volumes"]
.map((key) => {
let entry = usage[key] || {};
let name = key.charAt(0).toUpperCase() + key.slice(1);
return `${name}:\t${entry.total || 0}\t${format.bytes(entry.sizeInBytes || 0)}`;
})
.join("\n");
}
module.exports = { SystemController };
+116
View File
@@ -0,0 +1,116 @@
//
// Volumes section.
//
const { Controller } = require("./Controller");
const { VolumeTreeItem } = require("../TreeItems/VolumeTreeItem");
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
const { exec } = require("../../Utilities/Process");
const volumes = require("../Models/Volume");
const notify = require("../../Utilities/Notify");
const prompt = require("../../Utilities/Prompt");
class VolumesController extends Controller {
constructor(sidebar) {
super("apple_container.volumes");
this.sidebar = sidebar;
this.registerCommands();
}
async fetch() {
return volumes.build(await ContainerCLI.listVolumes());
}
getTreeItem(element) {
return new VolumeTreeItem(element);
}
registerCommands() {
this.register("apple_container.volumes.reload", () => this.reload());
this.register("apple_container.volumes.create", async () => {
let name = await prompt.input("Volume name", { placeholder: "database-data" });
if (!name) {
return;
}
try {
await ContainerCLI.createVolume(name);
} catch (error) {
notify.failure("Unable to create volume", error, "apple-container-volume");
}
this.reload();
});
this.register("apple_container.volumes.inspect", async () => {
for (let volume of this.selection) {
try {
let output = await ContainerCLI.inspectVolume(volume.name);
nova.workspace.openNewTextDocument({ content: output, syntax: "json" });
} catch (error) {
notify.failure("Unable to inspect volume", error, "apple-container-inspect");
}
}
});
this.register("apple_container.volumes.reveal", async () => {
for (let volume of this.selection) {
if (!volume.source) {
continue;
}
try {
await exec("/usr/bin/open", ["-R", volume.source]);
} catch (error) {
notify.failure("Unable to reveal volume", error, "apple-container-reveal");
}
}
});
this.register("apple_container.volumes.copy", () => {
let volume = this.selection[0];
if (volume) {
nova.clipboard.writeText(volume.name);
}
});
this.register("apple_container.volumes.delete", async () => {
let names = this.selection.map((volume) => volume.name);
if (names.length === 0) {
return;
}
if (!(await prompt.confirm(`Delete ${names.join(", ")}? Their contents are lost.`))) {
return;
}
try {
await ContainerCLI.deleteVolumes(names);
} catch (error) {
notify.failure("Unable to delete volume", error, "apple-container-delete-volume");
}
this.reload();
});
this.register("apple_container.volumes.prune", async () => {
if (!(await prompt.confirm("Remove volumes no container references?", "Remove"))) {
return;
}
try {
await ContainerCLI.pruneVolumes();
} catch (error) {
notify.failure("Unable to prune volumes", error, "apple-container-prune-volume");
}
this.reload();
});
}
}
module.exports = { VolumesController };