Skip to content

Commit 96cf9d7

Browse files
dgolovinaxel7083
andauthored
feat: add podman extension api package and use it in podman extension (podman-desktop#8891)
Signed-off-by: Denis Golovin <[email protected]> Co-authored-by: axel7083 <[email protected]>
1 parent 6778fe8 commit 96cf9d7

File tree

8 files changed

+160
-2
lines changed

8 files changed

+160
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@podman-desktop/podman-extension-api",
3+
"version": "0.0.1",
4+
"description": "API for Podman Desktop Podman extension",
5+
"publishConfig": {
6+
"access": "public"
7+
},
8+
"license": "Apache-2.0",
9+
"types": "./src/podman-extension-api.d.ts",
10+
"files": [
11+
"src"
12+
],
13+
"scripts": {
14+
"prepare": "",
15+
"clean": "rimraf lib *.tsbuildinfo",
16+
"build": "",
17+
"watch": "",
18+
"publish:next": "pnpm publish --registry=https://registry.npmjs.org/ --no-git-tag-version --new-version 0.0.1-\"$(date +%s)\""
19+
},
20+
"dependencies": {
21+
"@podman-desktop/api": "workspace:*"
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**********************************************************************
2+
* Copyright (C) 2024 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
import type { ProviderContainerConnection, RunOptions, RunResult } from '@podman-desktop/api';
20+
21+
export interface PodmanRunOptions extends RunOptions {
22+
connection?: ProviderContainerConnection;
23+
}
24+
25+
export interface PodmanExtensionApi {
26+
exec(args: string[], options?: PodmanRunOptions): Promise<RunResult>;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "@tsconfig/strictest/tsconfig.json",
3+
"compilerOptions": {
4+
"target": "esnext",
5+
"module": "node16",
6+
"baseUrl": ".",
7+
"types": ["node"],
8+
"strict": true,
9+
"esModuleInterop": true,
10+
"forceConsistentCasingInFileNames": true
11+
},
12+
"include": ["src/**/*.d.ts"]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**********************************************************************
2+
* Copyright (C) 2024 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
/* eslint-env node */
19+
import { join } from 'path';
20+
import { defineConfig } from 'vite';
21+
22+
const PACKAGE_ROOT = __dirname;
23+
24+
// https://vitejs.dev/config/
25+
export default defineConfig({
26+
mode: process.env.MODE,
27+
root: PACKAGE_ROOT,
28+
base: '',
29+
server: {
30+
fs: {
31+
strict: true,
32+
},
33+
},
34+
build: {
35+
sourcemap: true,
36+
outDir: 'dist',
37+
assetsDir: '.',
38+
emptyOutDir: true,
39+
reportCompressedSize: false,
40+
},
41+
});

extensions/podman/packages/extension/src/extension.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import * as extension from './extension';
3737
import type { InstalledPodman } from './podman-cli';
3838
import * as podmanCli from './podman-cli';
3939
import { PodmanConfiguration } from './podman-configuration';
40+
import type { UpdateCheck } from './podman-install';
4041
import { PodmanInstall } from './podman-install';
4142
import { getAssetsFolder, isLinux, isMac, isWindows, LIBKRUN_LABEL, LoggerDelegator, VMTYPE } from './util';
4243

@@ -181,9 +182,24 @@ vi.mock('@podman-desktop/api', async () => {
181182
provider: {
182183
onDidRegisterContainerConnection: vi.fn(),
183184
onDidUnregisterContainerConnection: vi.fn(),
185+
createProvider: (): extensionApi.Provider =>
186+
({
187+
updateWarnings: vi.fn(),
188+
onDidUpdateVersion: vi.fn(),
189+
registerAutostart: vi.fn(),
190+
}) as unknown as extensionApi.Provider,
191+
},
192+
registry: {
193+
registerRegistryProvider: vi.fn(),
194+
onDidRegisterRegistry: vi.fn(),
195+
onDidUnregisterRegistry: vi.fn(),
196+
onDidUpdateRegistry: vi.fn(),
184197
},
185198
proxy: {
186199
isEnabled: (): boolean => false,
200+
onDidUpdateProxy: vi.fn(),
201+
onDidStateChange: vi.fn(),
202+
getProxySettings: vi.fn(),
187203
},
188204
window: {
189205
showErrorMessage: vi.fn(),
@@ -2285,3 +2301,21 @@ test('if a machine stopped is successfully reporting an error in telemetry', asy
22852301

22862302
expect(spyExecPromise).toBeCalledWith(podmanCli.getPodmanCli(), ['machine', 'stop', 'name'], expect.anything());
22872303
});
2304+
2305+
test('activate function returns an api implementation', async () => {
2306+
vi.spyOn(PodmanInstall.prototype, 'checkForUpdate').mockResolvedValue({
2307+
hasUpdate: false,
2308+
} as unknown as UpdateCheck);
2309+
const contextMock = {
2310+
subscriptions: [],
2311+
secrets: {
2312+
delete: vi.fn(),
2313+
get: vi.fn(),
2314+
onDidChange: vi.fn(),
2315+
store: vi.fn(),
2316+
},
2317+
} as unknown as extensionApi.ExtensionContext;
2318+
const api = await extension.activate(contextMock);
2319+
expect(api).toBeDefined();
2320+
expect(typeof api.exec).toBe('function');
2321+
});

extensions/podman/packages/extension/src/extension.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type { ContainerEngineInfo, RunError } from '@podman-desktop/api';
2626
import * as extensionApi from '@podman-desktop/api';
2727
import { compareVersions } from 'compare-versions';
2828

29+
import type { PodmanExtensionApi, PodmanRunOptions } from '../../api/src/podman-extension-api';
2930
import { getSocketCompatibility } from './compatibility-mode';
3031
import { getDetectionChecks } from './detection-checks';
3132
import { KrunkitHelper } from './krunkit-helper';
@@ -1246,7 +1247,11 @@ export function registerOnboardingRemoveUnsupportedMachinesCommand(): extensionA
12461247
});
12471248
}
12481249

1249-
export async function activate(extensionContext: extensionApi.ExtensionContext): Promise<void> {
1250+
async function exec(args: string[], options?: PodmanRunOptions): Promise<extensionApi.RunResult> {
1251+
return execPodman(args, options?.connection?.connection.vmTypeDisplayName, options);
1252+
}
1253+
1254+
export async function activate(extensionContext: extensionApi.ExtensionContext): Promise<PodmanExtensionApi> {
12501255
initExtensionContext(extensionContext);
12511256

12521257
initTelemetryLogger();
@@ -1658,6 +1663,10 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
16581663

16591664
const podmanRemoteConnections = new PodmanRemoteConnections(extensionContext, provider);
16601665
podmanRemoteConnections.start();
1666+
1667+
return {
1668+
exec,
1669+
};
16611670
}
16621671

16631672
export async function calcPodmanMachineSetting(podmanConfiguration: PodmanConfiguration): Promise<void> {

extensions/podman/packages/extension/vite.config.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ const config = {
4545
formats: ['cjs'],
4646
},
4747
rollupOptions: {
48-
external: ['@podman-desktop/api', 'ssh2', ...builtinModules.flatMap(p => [p, `node:${p}`])],
48+
external: [
49+
'@podman-desktop/api',
50+
'ssh2',
51+
'@podman-desktop/podman-extension-api',
52+
...builtinModules.flatMap(p => [p, `node:${p}`]),
53+
],
4954
output: {
5055
entryFileNames: '[name].cjs',
5156
},

pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)