Skip to content

Commit

Permalink
fix: initialize action
Browse files Browse the repository at this point in the history
Signed-off-by: lstocchi <[email protected]>
  • Loading branch information
lstocchi committed Aug 13, 2024
1 parent 11fe2fc commit b256c1f
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import { registerOpenTerminalCommand } from './dev-terminal';
import { commandManager } from './command';
import { registerOpenConsoleCommand } from './crc-console';
import { registerLogInCommands } from './login-commands';
import { defaultLogger } from './logger';
import { pushImageToCrcCluster } from './image-handler';
import type { Preset } from './types';
import { needSetup, setUpCrc } from './crc-setup';

const CRC_PUSH_IMAGE_TO_CLUSTER = 'crc.image.push.to.cluster';
const CRC_PRESET_KEY = 'crc.crcPreset';
Expand Down Expand Up @@ -220,9 +220,7 @@ function registerProviderConnectionFactory(
): void {
connectionFactoryDisposable = provider.setKubernetesProviderConnectionFactory(
{
initialize: async () => {
await createCrcVm(provider, extensionContext, telemetryLogger, defaultLogger);
},
initialize: () => initializeCrc(provider, extensionContext, telemetryLogger),
create: async (params, logger) => {
await presetChanged(provider, extensionContext, telemetryLogger);
await saveConfig(params);
Expand Down Expand Up @@ -254,20 +252,42 @@ async function createCrcVm(
const hasStarted = await startCrc(provider, getLoggerCallback(undefined, logger), telemetryLogger);
if (!connectionDisposable && hasStarted) {
addCommands(telemetryLogger);
presetChanged(provider, extensionContext, telemetryLogger);
await presetChanged(provider, extensionContext, telemetryLogger);
}
}

async function initializeCrc(
provider: extensionApi.Provider,
extensionContext: extensionApi.ExtensionContext,
telemetryLogger: extensionApi.TelemetryLogger,
): Promise<void> {
const hasSetupFinished = await setUpCrc(true);
if (!hasSetupFinished) {
throw new Error(`Failed at initializing ${productName}`);
}

await needSetup();
await connectToCrc();
await presetChanged(provider, extensionContext, telemetryLogger);
addCommands(telemetryLogger);
await syncPreferences(provider, extensionContext, telemetryLogger);
return presetChanged(provider, extensionContext, telemetryLogger);
}

function addCommands(telemetryLogger: extensionApi.TelemetryLogger): void {
registerOpenTerminalCommand();
registerOpenConsoleCommand();
registerLogInCommands();
registerDeleteCommand();

commandManager.addCommand(CRC_PUSH_IMAGE_TO_CLUSTER, image => {
telemetryLogger.logUsage('pushImage');
pushImageToCrcCluster(image);
});
try {
registerOpenTerminalCommand();
registerOpenConsoleCommand();
registerLogInCommands();
registerDeleteCommand();

commandManager.addCommand(CRC_PUSH_IMAGE_TO_CLUSTER, image => {
telemetryLogger.logUsage('pushImage');
pushImageToCrcCluster(image);
});
} catch (e) {
// do nothing
}
}

function deleteCommands(): void {
Expand Down Expand Up @@ -406,6 +426,6 @@ async function presetChanged(
// podman connection
registerPodmanConnection(provider, extensionContext);
} else if (preset === 'openshift' || preset === 'microshift') {
registerOpenShiftLocalCluster(getPresetLabel(preset), provider, extensionContext, telemetryLogger);
await registerOpenShiftLocalCluster(getPresetLabel(preset), provider, extensionContext, telemetryLogger);
}
}

0 comments on commit b256c1f

Please sign in to comment.