2025-08-25 17:02:10 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { CommandModule } from 'yargs';
|
2025-09-25 10:57:59 -07:00
|
|
|
import {
|
2025-10-16 11:01:17 -04:00
|
|
|
INSTALL_WARNING_MESSAGE,
|
2025-10-10 14:28:13 -07:00
|
|
|
installOrUpdateExtension,
|
2025-09-25 10:57:59 -07:00
|
|
|
requestConsentNonInteractive,
|
|
|
|
|
} from '../../config/extension.js';
|
2025-10-20 18:16:47 -04:00
|
|
|
import {
|
|
|
|
|
debugLogger,
|
|
|
|
|
type ExtensionInstallMetadata,
|
|
|
|
|
} from '@google/gemini-cli-core';
|
2025-08-26 14:36:55 +00:00
|
|
|
import { getErrorMessage } from '../../utils/errors.js';
|
2025-10-07 12:01:45 -04:00
|
|
|
import { stat } from 'node:fs/promises';
|
2025-08-26 14:36:55 +00:00
|
|
|
|
2025-08-25 17:02:10 +00:00
|
|
|
interface InstallArgs {
|
2025-10-07 12:01:45 -04:00
|
|
|
source: string;
|
2025-09-10 09:35:48 -07:00
|
|
|
ref?: string;
|
2025-09-18 14:49:47 -07:00
|
|
|
autoUpdate?: boolean;
|
2025-10-08 14:26:12 -07:00
|
|
|
allowPreRelease?: boolean;
|
2025-10-16 11:01:17 -04:00
|
|
|
consent?: boolean;
|
2025-08-25 17:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function handleInstall(args: InstallArgs) {
|
|
|
|
|
try {
|
2025-09-02 08:15:47 -07:00
|
|
|
let installMetadata: ExtensionInstallMetadata;
|
2025-10-07 12:01:45 -04:00
|
|
|
const { source } = args;
|
|
|
|
|
if (
|
|
|
|
|
source.startsWith('http://') ||
|
|
|
|
|
source.startsWith('https://') ||
|
|
|
|
|
source.startsWith('git@') ||
|
|
|
|
|
source.startsWith('sso://')
|
|
|
|
|
) {
|
2025-09-02 08:15:47 -07:00
|
|
|
installMetadata = {
|
2025-10-07 12:01:45 -04:00
|
|
|
source,
|
|
|
|
|
type: 'git',
|
|
|
|
|
ref: args.ref,
|
2025-09-18 14:49:47 -07:00
|
|
|
autoUpdate: args.autoUpdate,
|
2025-10-08 14:26:12 -07:00
|
|
|
allowPreRelease: args.allowPreRelease,
|
2025-09-02 08:15:47 -07:00
|
|
|
};
|
|
|
|
|
} else {
|
2025-10-07 12:01:45 -04:00
|
|
|
if (args.ref || args.autoUpdate) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'--ref and --auto-update are not applicable for local extensions.',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await stat(source);
|
|
|
|
|
installMetadata = {
|
|
|
|
|
source,
|
|
|
|
|
type: 'local',
|
|
|
|
|
};
|
|
|
|
|
} catch {
|
|
|
|
|
throw new Error('Install source not found.');
|
|
|
|
|
}
|
2025-09-02 08:15:47 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 11:01:17 -04:00
|
|
|
const requestConsent = args.consent
|
|
|
|
|
? () => Promise.resolve(true)
|
|
|
|
|
: requestConsentNonInteractive;
|
|
|
|
|
if (args.consent) {
|
2025-10-20 18:16:47 -04:00
|
|
|
debugLogger.log('You have consented to the following:');
|
|
|
|
|
debugLogger.log(INSTALL_WARNING_MESSAGE);
|
2025-10-16 11:01:17 -04:00
|
|
|
}
|
2025-10-10 14:28:13 -07:00
|
|
|
const name = await installOrUpdateExtension(
|
2025-09-25 10:57:59 -07:00
|
|
|
installMetadata,
|
2025-10-16 11:01:17 -04:00
|
|
|
requestConsent,
|
2025-09-25 10:57:59 -07:00
|
|
|
);
|
2025-10-20 18:16:47 -04:00
|
|
|
debugLogger.log(`Extension "${name}" installed successfully and enabled.`);
|
2025-08-25 17:02:10 +00:00
|
|
|
} catch (error) {
|
2025-10-20 18:16:47 -04:00
|
|
|
debugLogger.error(getErrorMessage(error));
|
2025-08-25 17:02:10 +00:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const installCommand: CommandModule = {
|
2025-10-08 14:26:12 -07:00
|
|
|
command: 'install <source> [--auto-update] [--pre-release]',
|
2025-09-08 21:59:45 -07:00
|
|
|
describe: 'Installs an extension from a git repository URL or a local path.',
|
2025-08-25 17:02:10 +00:00
|
|
|
builder: (yargs) =>
|
|
|
|
|
yargs
|
2025-09-05 11:44:41 -07:00
|
|
|
.positional('source', {
|
2025-10-07 12:01:45 -04:00
|
|
|
describe: 'The github URL or local path of the extension to install.',
|
2025-08-25 17:02:10 +00:00
|
|
|
type: 'string',
|
2025-10-07 12:01:45 -04:00
|
|
|
demandOption: true,
|
2025-08-25 17:02:10 +00:00
|
|
|
})
|
2025-09-10 09:35:48 -07:00
|
|
|
.option('ref', {
|
|
|
|
|
describe: 'The git ref to install from.',
|
|
|
|
|
type: 'string',
|
|
|
|
|
})
|
2025-09-18 14:49:47 -07:00
|
|
|
.option('auto-update', {
|
|
|
|
|
describe: 'Enable auto-update for this extension.',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
})
|
2025-10-08 14:26:12 -07:00
|
|
|
.option('pre-release', {
|
|
|
|
|
describe: 'Enable pre-release versions for this extension.',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
})
|
2025-10-16 11:01:17 -04:00
|
|
|
.option('consent', {
|
|
|
|
|
describe:
|
|
|
|
|
'Acknowledge the security risks of installing an extension and skip the confirmation prompt.',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
default: false,
|
|
|
|
|
})
|
2025-08-25 17:02:10 +00:00
|
|
|
.check((argv) => {
|
2025-10-07 12:01:45 -04:00
|
|
|
if (!argv.source) {
|
|
|
|
|
throw new Error('The source argument must be provided.');
|
2025-08-25 17:02:10 +00:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}),
|
|
|
|
|
handler: async (argv) => {
|
|
|
|
|
await handleInstall({
|
2025-10-07 12:01:45 -04:00
|
|
|
source: argv['source'] as string,
|
2025-09-10 09:35:48 -07:00
|
|
|
ref: argv['ref'] as string | undefined,
|
2025-09-18 14:49:47 -07:00
|
|
|
autoUpdate: argv['auto-update'] as boolean | undefined,
|
2025-10-08 14:26:12 -07:00
|
|
|
allowPreRelease: argv['pre-release'] as boolean | undefined,
|
2025-10-16 11:01:17 -04:00
|
|
|
consent: argv['consent'] as boolean | undefined,
|
2025-08-25 17:02:10 +00:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|