mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-30 06:54:15 -07:00
refactor(logging): Centralize all console messaging to a shared logger (part 1) (#11537)
This commit is contained in:
@@ -8,6 +8,7 @@ import { type CommandModule } from 'yargs';
|
||||
import { disableExtension } from '../../config/extension.js';
|
||||
import { SettingScope } from '../../config/settings.js';
|
||||
import { getErrorMessage } from '../../utils/errors.js';
|
||||
import { debugLogger } from '@google/gemini-cli-core';
|
||||
|
||||
interface DisableArgs {
|
||||
name: string;
|
||||
@@ -21,11 +22,11 @@ export function handleDisable(args: DisableArgs) {
|
||||
} else {
|
||||
disableExtension(args.name, SettingScope.User);
|
||||
}
|
||||
console.log(
|
||||
debugLogger.log(
|
||||
`Extension "${args.name}" successfully disabled for scope "${args.scope}".`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(getErrorMessage(error));
|
||||
debugLogger.error(getErrorMessage(error));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,10 @@ import {
|
||||
installOrUpdateExtension,
|
||||
requestConsentNonInteractive,
|
||||
} from '../../config/extension.js';
|
||||
import type { ExtensionInstallMetadata } from '@google/gemini-cli-core';
|
||||
import {
|
||||
debugLogger,
|
||||
type ExtensionInstallMetadata,
|
||||
} from '@google/gemini-cli-core';
|
||||
import { getErrorMessage } from '../../utils/errors.js';
|
||||
import { stat } from 'node:fs/promises';
|
||||
|
||||
@@ -60,16 +63,16 @@ export async function handleInstall(args: InstallArgs) {
|
||||
? () => Promise.resolve(true)
|
||||
: requestConsentNonInteractive;
|
||||
if (args.consent) {
|
||||
console.log('You have consented to the following:');
|
||||
console.log(INSTALL_WARNING_MESSAGE);
|
||||
debugLogger.log('You have consented to the following:');
|
||||
debugLogger.log(INSTALL_WARNING_MESSAGE);
|
||||
}
|
||||
const name = await installOrUpdateExtension(
|
||||
installMetadata,
|
||||
requestConsent,
|
||||
);
|
||||
console.log(`Extension "${name}" installed successfully and enabled.`);
|
||||
debugLogger.log(`Extension "${name}" installed successfully and enabled.`);
|
||||
} catch (error) {
|
||||
console.error(getErrorMessage(error));
|
||||
debugLogger.error(getErrorMessage(error));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,10 @@ import {
|
||||
installOrUpdateExtension,
|
||||
requestConsentNonInteractive,
|
||||
} from '../../config/extension.js';
|
||||
import type { ExtensionInstallMetadata } from '@google/gemini-cli-core';
|
||||
import {
|
||||
debugLogger,
|
||||
type ExtensionInstallMetadata,
|
||||
} from '@google/gemini-cli-core';
|
||||
|
||||
import { getErrorMessage } from '../../utils/errors.js';
|
||||
|
||||
@@ -27,11 +30,11 @@ export async function handleLink(args: InstallArgs) {
|
||||
installMetadata,
|
||||
requestConsentNonInteractive,
|
||||
);
|
||||
console.log(
|
||||
debugLogger.log(
|
||||
`Extension "${extensionName}" linked successfully and enabled.`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(getErrorMessage(error));
|
||||
debugLogger.error(getErrorMessage(error));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import type { CommandModule } from 'yargs';
|
||||
import { loadExtensions, toOutputString } from '../../config/extension.js';
|
||||
import { getErrorMessage } from '../../utils/errors.js';
|
||||
import { ExtensionEnablementManager } from '../../config/extensions/extensionEnablement.js';
|
||||
import { debugLogger } from '@google/gemini-cli-core';
|
||||
|
||||
export async function handleList() {
|
||||
try {
|
||||
@@ -16,16 +17,16 @@ export async function handleList() {
|
||||
process.cwd(),
|
||||
);
|
||||
if (extensions.length === 0) {
|
||||
console.log('No extensions installed.');
|
||||
debugLogger.log('No extensions installed.');
|
||||
return;
|
||||
}
|
||||
console.log(
|
||||
debugLogger.log(
|
||||
extensions
|
||||
.map((extension, _): string => toOutputString(extension, process.cwd()))
|
||||
.join('\n\n'),
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(getErrorMessage(error));
|
||||
debugLogger.error(getErrorMessage(error));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import { access, cp, mkdir, readdir, writeFile } from 'node:fs/promises';
|
||||
import { join, dirname, basename } from 'node:path';
|
||||
import type { CommandModule } from 'yargs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { getErrorMessage } from '../../utils/errors.js';
|
||||
import { debugLogger } from '@google/gemini-cli-core';
|
||||
|
||||
interface NewArgs {
|
||||
path: string;
|
||||
@@ -49,32 +49,27 @@ async function copyDirectory(template: string, path: string) {
|
||||
}
|
||||
|
||||
async function handleNew(args: NewArgs) {
|
||||
try {
|
||||
if (args.template) {
|
||||
await copyDirectory(args.template, args.path);
|
||||
console.log(
|
||||
`Successfully created new extension from template "${args.template}" at ${args.path}.`,
|
||||
);
|
||||
} else {
|
||||
await createDirectory(args.path);
|
||||
const extensionName = basename(args.path);
|
||||
const manifest = {
|
||||
name: extensionName,
|
||||
version: '1.0.0',
|
||||
};
|
||||
await writeFile(
|
||||
join(args.path, 'gemini-extension.json'),
|
||||
JSON.stringify(manifest, null, 2),
|
||||
);
|
||||
console.log(`Successfully created new extension at ${args.path}.`);
|
||||
}
|
||||
console.log(
|
||||
`You can install this using "gemini extensions link ${args.path}" to test it out.`,
|
||||
if (args.template) {
|
||||
await copyDirectory(args.template, args.path);
|
||||
debugLogger.log(
|
||||
`Successfully created new extension from template "${args.template}" at ${args.path}.`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(getErrorMessage(error));
|
||||
throw error;
|
||||
} else {
|
||||
await createDirectory(args.path);
|
||||
const extensionName = basename(args.path);
|
||||
const manifest = {
|
||||
name: extensionName,
|
||||
version: '1.0.0',
|
||||
};
|
||||
await writeFile(
|
||||
join(args.path, 'gemini-extension.json'),
|
||||
JSON.stringify(manifest, null, 2),
|
||||
);
|
||||
debugLogger.log(`Successfully created new extension at ${args.path}.`);
|
||||
}
|
||||
debugLogger.log(
|
||||
`You can install this using "gemini extensions link ${args.path}" to test it out.`,
|
||||
);
|
||||
}
|
||||
|
||||
async function getBoilerplateChoices() {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
import type { CommandModule } from 'yargs';
|
||||
import { uninstallExtension } from '../../config/extension.js';
|
||||
import { getErrorMessage } from '../../utils/errors.js';
|
||||
import { debugLogger } from '@google/gemini-cli-core';
|
||||
|
||||
interface UninstallArgs {
|
||||
name: string; // can be extension name or source URL.
|
||||
@@ -15,9 +16,9 @@ interface UninstallArgs {
|
||||
export async function handleUninstall(args: UninstallArgs) {
|
||||
try {
|
||||
await uninstallExtension(args.name, false);
|
||||
console.log(`Extension "${args.name}" successfully uninstalled.`);
|
||||
debugLogger.log(`Extension "${args.name}" successfully uninstalled.`);
|
||||
} catch (error) {
|
||||
console.error(getErrorMessage(error));
|
||||
debugLogger.error(getErrorMessage(error));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import { checkForExtensionUpdate } from '../../config/extensions/github.js';
|
||||
import { getErrorMessage } from '../../utils/errors.js';
|
||||
import { ExtensionUpdateState } from '../../ui/state/extensions.js';
|
||||
import { ExtensionEnablementManager } from '../../config/extensions/extensionEnablement.js';
|
||||
import { debugLogger } from '@google/gemini-cli-core';
|
||||
|
||||
interface UpdateArgs {
|
||||
name?: string;
|
||||
@@ -48,18 +49,18 @@ export async function handleUpdate(args: UpdateArgs) {
|
||||
(extension) => extension.name === args.name,
|
||||
);
|
||||
if (!extension) {
|
||||
console.log(`Extension "${args.name}" not found.`);
|
||||
debugLogger.log(`Extension "${args.name}" not found.`);
|
||||
return;
|
||||
}
|
||||
if (!extension.installMetadata) {
|
||||
console.log(
|
||||
debugLogger.log(
|
||||
`Unable to install extension "${args.name}" due to missing install metadata`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
const updateState = await checkForExtensionUpdate(extension);
|
||||
if (updateState !== ExtensionUpdateState.UPDATE_AVAILABLE) {
|
||||
console.log(`Extension "${args.name}" is already up to date.`);
|
||||
debugLogger.log(`Extension "${args.name}" is already up to date.`);
|
||||
return;
|
||||
}
|
||||
// TODO(chrstnb): we should list extensions if the requested extension is not installed.
|
||||
@@ -74,14 +75,14 @@ export async function handleUpdate(args: UpdateArgs) {
|
||||
updatedExtensionInfo.originalVersion !==
|
||||
updatedExtensionInfo.updatedVersion
|
||||
) {
|
||||
console.log(
|
||||
debugLogger.log(
|
||||
`Extension "${args.name}" successfully updated: ${updatedExtensionInfo.originalVersion} → ${updatedExtensionInfo.updatedVersion}.`,
|
||||
);
|
||||
} else {
|
||||
console.log(`Extension "${args.name}" is already up to date.`);
|
||||
debugLogger.log(`Extension "${args.name}" is already up to date.`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(getErrorMessage(error));
|
||||
debugLogger.error(getErrorMessage(error));
|
||||
}
|
||||
}
|
||||
if (args.all) {
|
||||
@@ -109,12 +110,12 @@ export async function handleUpdate(args: UpdateArgs) {
|
||||
(info) => info.originalVersion !== info.updatedVersion,
|
||||
);
|
||||
if (updateInfos.length === 0) {
|
||||
console.log('No extensions to update.');
|
||||
debugLogger.log('No extensions to update.');
|
||||
return;
|
||||
}
|
||||
console.log(updateInfos.map((info) => updateOutput(info)).join('\n'));
|
||||
debugLogger.log(updateInfos.map((info) => updateOutput(info)).join('\n'));
|
||||
} catch (error) {
|
||||
console.error(getErrorMessage(error));
|
||||
debugLogger.error(getErrorMessage(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
// File for 'gemini mcp add' command
|
||||
import type { CommandModule } from 'yargs';
|
||||
import { loadSettings, SettingScope } from '../../config/settings.js';
|
||||
import type { MCPServerConfig } from '@google/gemini-cli-core';
|
||||
import { debugLogger, type MCPServerConfig } from '@google/gemini-cli-core';
|
||||
|
||||
async function addMcpServer(
|
||||
name: string,
|
||||
@@ -41,7 +41,7 @@ async function addMcpServer(
|
||||
const inHome = settings.workspace.path === settings.user.path;
|
||||
|
||||
if (scope === 'project' && inHome) {
|
||||
console.error(
|
||||
debugLogger.error(
|
||||
'Error: Please use --scope user to edit settings in the home directory.',
|
||||
);
|
||||
process.exit(1);
|
||||
@@ -116,7 +116,7 @@ async function addMcpServer(
|
||||
|
||||
const isExistingServer = !!mcpServers[name];
|
||||
if (isExistingServer) {
|
||||
console.log(
|
||||
debugLogger.log(
|
||||
`MCP server "${name}" is already configured within ${scope} settings.`,
|
||||
);
|
||||
}
|
||||
@@ -126,9 +126,9 @@ async function addMcpServer(
|
||||
settings.setValue(settingsScope, 'mcpServers', mcpServers);
|
||||
|
||||
if (isExistingServer) {
|
||||
console.log(`MCP server "${name}" updated in ${scope} settings.`);
|
||||
debugLogger.log(`MCP server "${name}" updated in ${scope} settings.`);
|
||||
} else {
|
||||
console.log(
|
||||
debugLogger.log(
|
||||
`MCP server "${name}" added to ${scope} settings. (${transport})`,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user