Skip to content

Commit

Permalink
add terraform version change notification
Browse files Browse the repository at this point in the history
  • Loading branch information
jpogran committed Aug 1, 2022
1 parent b9b0fa4 commit 8b62630
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as tfStatus from './status/terraform';
import * as terraform from './terraform';
import * as vscode from 'vscode';
import TelemetryReporter from '@vscode/extension-telemetry';
Expand All @@ -17,7 +16,7 @@ import { GenerateBugReportCommand } from './commands/generateBugReport';
import { ModuleCallsDataProvider } from './providers/moduleCalls';
import { ModuleProvidersDataProvider } from './providers/moduleProviders';
import { ServerPath } from './utils/serverPath';
import { config, deleteSetting, getScope, migrate, warnIfMigrate } from './utils/vscode';
import { config, deleteSetting, getActiveTextEditor, getScope, migrate, warnIfMigrate } from './utils/vscode';
import { TelemetryFeature } from './features/telemetry';
import { ShowReferencesFeature } from './features/showReferences';
import { CustomSemanticTokens } from './features/semanticTokens';
Expand Down Expand Up @@ -212,10 +211,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
In the future, we can hook this to onDidChange or a similar handler, but currently
we only detect Terraform versions at start inside terraform-ls, so it is sufficient to ask once here
*/
const workspaces = vscode.workspace.workspaceFolders;
if (workspaces !== undefined) {
const response = await terraform.terraformVersion(workspaces[0].uri.toString(), client, reporter);
tfStatus.setTerraformVersion(response.discovered_version);
const editor = getActiveTextEditor();
if (editor !== undefined) {
terraform.getTerraformVersion(editor.document.uri, client, reporter);
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/features/terraformVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as terraform from '../terraform';
import * as vscode from 'vscode';
import { ClientCapabilities, ServerCapabilities, StaticFeature } from 'vscode-languageclient';
import { ExperimentalClientCapabilities } from './types';
import TelemetryReporter from '@vscode/extension-telemetry';
import { LanguageClient } from 'vscode-languageclient/node';
import { getActiveTextEditor } from '../utils/vscode';

export const CLIENT_TERRAFORM_VERSION_CMD_ID = 'client.refreshTerraformVersion';

export class TerraformVersionFeature implements StaticFeature {
private disposables: vscode.Disposable[] = [];

constructor(private client: LanguageClient, private reporter: TelemetryReporter) {}

public fillClientCapabilities(capabilities: ClientCapabilities & ExperimentalClientCapabilities): void {
if (!capabilities['experimental']) {
capabilities['experimental'] = {};
}
capabilities['experimental']['refreshTerraformVersionCommandId'] = CLIENT_TERRAFORM_VERSION_CMD_ID;
}

public async initialize(capabilities: ServerCapabilities): Promise<void> {
if (!capabilities.experimental?.refreshTerraformVersion) {
console.log("Server doesn't support client.refreshTerraformVersion");
return;
}

await this.client.onReady();

const d = this.client.onRequest(CLIENT_TERRAFORM_VERSION_CMD_ID, async () => {
const editor = getActiveTextEditor();
if (editor === undefined) {
return;
}

terraform.getTerraformVersion(editor.document.uri, this.client, this.reporter);
});

this.disposables.push(d);
}

public dispose(): void {
this.disposables.forEach((d: vscode.Disposable) => d.dispose());
}
}
1 change: 1 addition & 0 deletions src/features/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export interface ExperimentalClientCapabilities {
showReferencesCommandId?: string;
refreshModuleProvidersCommandId?: string;
refreshModuleCallsCommandId?: string;
refreshTerraformVersionCommandId?: string;
};
}
21 changes: 21 additions & 0 deletions src/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ interface ModuleProvidersResponse {
}
/* eslint-enable @typescript-eslint/naming-convention */

export async function getTerraformVersion(
moduleUri: vscode.Uri,
client: LanguageClient,
reporter: TelemetryReporter,
): Promise<void> {
try {
const moduleDir = Utils.dirname(moduleUri);

const response = await terraformVersion(moduleDir.toString(), client, reporter);
tfStatus.setTerraformVersion(response.discovered_version);
} catch (error) {
let message = 'Error requesting terraform version from terraform-ls';
if (error instanceof Error) {
message = error.message;
} else if (typeof error === 'string') {
message = error;
}

vscode.window.showErrorMessage(message);
}
}
export async function terraformVersion(
moduleUri: string,
client: LanguageClient,
Expand Down

0 comments on commit 8b62630

Please sign in to comment.