Skip to content

Commit 4a79483

Browse files
authored
Merge pull request #121 from gtardif/docs_stream_lines
Update documentation about exec streaming, mention splitOutputLines and add docker events sample
2 parents 7d70557 + 66b2ec5 commit 4a79483

File tree

5 files changed

+78
-25
lines changed

5 files changed

+78
-25
lines changed

docs/dev/api/backend.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Communication with the extension backend
22

3-
The `window.ddClient.extension.vm` object can be used to communicate with the backend defined in the [vm section](../../extensions/METADATA.md#vm-section) in the extensions metadata.
3+
The `ddClient.extension.vm` object can be used to communicate with the backend defined in the [vm section](../../extensions/METADATA.md#vm-section) in the extensions metadata.
44

55
### get
66

@@ -9,7 +9,7 @@ The `window.ddClient.extension.vm` object can be used to communicate with the ba
99
Performs an HTTP GET request to a backend service.
1010

1111
```typescript
12-
window.ddClient.extension.vm.service
12+
ddClient.extension.vm.service
1313
.get("/some/service")
1414
.then((value: any) => console.log(value)
1515
```
@@ -65,23 +65,27 @@ Executes a command in the backend container.
6565
Example: Execute the command `ls -l` inside the **backend container**:
6666
6767
```typescript
68-
await window.ddClient.extension.vm.cli.exec("ls", ["-l"]);
68+
await ddClient.extension.vm.cli.exec("ls", ["-l"]);
6969
```
7070
7171
Streams the output of the command executed in the backend container.
7272
7373
Example: Spawn the command `ls -l` inside the **backend container**:
7474
7575
```typescript linenums="1"
76-
await window.ddClient.extension.vm.cli.exec("ls", ["-l"], {
76+
await ddClient.extension.vm.cli.exec("ls", ["-l"], {
7777
stream: {
78-
onOutput(data: { stdout: string } | { stderr: string }): void {
79-
console.error(data.stdout);
78+
onOutput(data) {
79+
if (data.stdout) {
80+
console.error(data.stdout);
81+
} else {
82+
console.log(data.stderr);
83+
}
8084
},
81-
onError(error: any): void {
85+
onError(error) {
8286
console.error(error);
8387
},
84-
onClose(exitCode: number): void {
88+
onClose(exitCode) {
8589
console.log("onClose with exit code " + exitCode);
8690
},
8791
},
@@ -114,16 +118,20 @@ in the exension metadata.
114118
Example: Execute the shipped binary `kubectl -h` command in the **host**:
115119
116120
```typescript
117-
await window.ddClient.extension.host.cli.exec("kubectl", ["-h"]);
121+
await ddClient.extension.host.cli.exec("kubectl", ["-h"]);
118122
```
119123
120124
Example: Provided the `kubectl` binary is shipped as part of your extension, you can spawn the `kubectl -h` command in the **host** and get the output stream:
121125
122126
```typescript linenums="1"
123-
await window.ddClient.extension.host.cli.exec("kubectl", ["-h"], {
127+
await ddClient.extension.host.cli.exec("kubectl", ["-h"], {
124128
stream: {
125129
onOutput(data: { stdout: string } | { stderr: string }): void {
126-
console.error(data.stdout);
130+
if (data.stdout) {
131+
console.error(data.stdout);
132+
} else {
133+
console.log(data.stderr);
134+
}
127135
},
128136
onError(error: any): void {
129137
console.error(error);

docs/dev/api/dashboard-routes-navigation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Dashboard Routes Navigation
22

3-
`window.ddClient.desktopUI.navigation` allow to navigate to specific screens of the Docker Desktop Dashboard like containers view, images view, a specific container logs, a specific image details, volumes, dev environments.
3+
`ddClient.desktopUI.navigation` allow to navigate to specific screens of the Docker Desktop Dashboard like containers view, images view, a specific container logs, a specific image details, volumes, dev environments.
44

55
Example: navigate to a given container logs
66

77
```typescript
8-
await window.ddClient.desktopUI.navigation.viewContainerLogs(id);
8+
await ddClient.desktopUI.navigation.viewContainerLogs(id);
99
```
1010

1111
#### Parameters

docs/dev/api/dashboard.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ shouldn't interrupt the user experience, they don't require user input to disapp
1010
Display a toast message of type success.
1111

1212
```typescript
13-
window.ddClient.desktopUI.toast.success("message");
13+
ddClient.desktopUI.toast.success("message");
1414
```
1515

1616
### warning
@@ -20,7 +20,7 @@ window.ddClient.desktopUI.toast.success("message");
2020
Display a toast message of type warning.
2121

2222
```typescript
23-
window.ddClient.desktopUI.toast.warning("message");
23+
ddClient.desktopUI.toast.warning("message");
2424
```
2525

2626
### error
@@ -30,7 +30,7 @@ window.ddClient.desktopUI.toast.warning("message");
3030
Display a toast message of type error.
3131

3232
```typescript
33-
window.ddClient.desktopUI.toast.error("message");
33+
ddClient.desktopUI.toast.error("message");
3434
```
3535

3636
More details about method parameters and return types are available in the [Toast API reference](reference/interfaces/Toast.md).
@@ -56,7 +56,7 @@ This function opens an external URL with the system default browser.
5656
Opens an external URL with the system default browser.
5757

5858
```typescript
59-
window.ddClient.host.openExternal("https://docker.com");
59+
ddClient.host.openExternal("https://docker.com");
6060
```
6161

6262
!!! note

docs/dev/api/docker.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
Get the list of containers
66

77
```typescript
8-
const containers = await window.ddClient.docker.listContainers();
8+
const containers = await ddClient.docker.listContainers();
99
```
1010

1111
**listImages**(`options?`): `Promise`<`unknown`\>
1212

1313
Get the list of local container images
1414

1515
```typescript
16-
const images = await window.ddClient.docker.listImages();
16+
const images = await ddClient.docker.listImages();
1717
```
1818

1919
Use the [Docker API reference](reference/interfaces/Docker.md) for details about these methods
@@ -37,7 +37,7 @@ Extensions can also directly execute the `docker` command line.
3737
**exec**(`cmd`, `args`): `Promise`<[`ExecResult`](reference/interfaces/ExecResult.md)\>
3838

3939
```typescript
40-
const result = await window.ddClient.docker.cli.exec("info", [
40+
const result = await ddClient.docker.cli.exec("info", [
4141
"--format",
4242
'"{{ json . }}"',
4343
]);
@@ -65,21 +65,51 @@ Streams the output as a result of the execution of a docker command.
6565
Useful when you need to get the output as a stream or the output of the command is too long.
6666

6767
```typescript linenums="1"
68-
await window.ddClient.docker.cli.exec("logs", ["-f", "..."], {
68+
await ddClient.docker.cli.exec("logs", ["-f", "..."], {
6969
stream: {
70-
onOutput(data: { stdout: string } | { stderr: string }): void {
71-
console.log(data.stdout);
70+
onOutput(data) {
71+
if (data.stdout) {
72+
console.error(data.stdout);
73+
} else {
74+
console.log(data.stderr);
75+
}
7276
},
73-
onError(error: any): void {
77+
onError(error) {
7478
console.error(error);
7579
},
76-
onClose(exitCode: number): void {
80+
onClose(exitCode) {
7781
console.log("onClose with exit code " + exitCode);
7882
},
83+
splitOutputLines: true,
7984
},
8085
});
8186
```
8287

88+
This can also be useful to listen to docker events:
89+
90+
```typescript linenums="1"
91+
await ddClient.docker.cli.exec(
92+
"events",
93+
["--format", "{{ json . }}", "--filter", "container=my-container"],
94+
{
95+
stream: {
96+
onOutput(data) {
97+
if (data.stdout) {
98+
const event = JSON.parse(data.stdout);
99+
console.log(event);
100+
} else {
101+
console.log(data.stderr);
102+
}
103+
},
104+
onClose(exitCode) {
105+
console.log("onClose with exit code " + exitCode);
106+
},
107+
splitOutputLines: true,
108+
},
109+
}
110+
);
111+
```
112+
83113
Use the [Exec API reference](reference/interfaces/Exec.md) for details about these methods
84114

85115
### Deprecated execution of Docker commands

docs/dev/api/overview.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ electron or nodejs APIs.
66
The extension UI API provides a way for the frontend to perform different actions
77
and communicate with the Docker Desktop dashboard or the underlying system.
88

9+
JavaScript API libraries are available (with Typescript support) in order to get all the API definition in your extension code.
10+
11+
- [@docker/extension-api-client](https://www.npmjs.com/package/@docker/extension-api-client) gives access to the extension API entrypoint `DockerDesktopCLient`
12+
- [@docker/extension-api-client-types](https://www.npmjs.com/package/@docker/extension-api-client-types) can be added as a dev dependency in order to get types auto-completion in your IDE.
13+
14+
```Typescript
15+
import { createDockerDesktopClient } from '@docker/extension-api-client';
16+
17+
export function App() {
18+
//obtain Docker Desktop client
19+
const ddClient = createDockerDesktopClient();
20+
```
21+
22+
The `ddClient` object gives access to various APIs, organized in different categories:
23+
924
- [Extension Backend](./backend.md)
1025
- [Docker](./docker.md)
1126
- [Dashboard](./dashboard.md)

0 commit comments

Comments
 (0)