Skip to content

Commit

Permalink
Add Terraform version info
Browse files Browse the repository at this point in the history
This commit adds a LanguageStatusItem that shows the discovered Terraform version in the current workspace.
  • Loading branch information
jpogran committed Jul 27, 2022
1 parent 8452576 commit f9a1cdc
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as tfStatus from './status/terraform';
import * as terraform from './terraform';
import * as vscode from 'vscode';
import TelemetryReporter from '@vscode/extension-telemetry';
Expand Down Expand Up @@ -50,6 +51,39 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
// Subscriptions
context.subscriptions.push(
new GenerateBugReportCommand(context),
vscode.commands.registerCommand('terraform.restartLanguageServer', async () => {
vscode.commands.executeCommand('workbench.action.reloadWindow');
}),
vscode.commands.registerCommand('terraform.showLanguageServerLogs', async () => {
outputChannel.show();
}),
vscode.commands.registerCommand('terraform.languageServer.commands', async () => {
await vscode.window
.showQuickPick([
{
label: 'Restart Language Server',
command: 'terraform.restartLanguageServer',
},
{
label: 'Show Language Server Logs',
command: 'terraform.showLanguageServerLogs',
},
{
label: 'Enable Language Server',
command: 'terraform.enableLanguageServer',
},
{
label: 'Disable Language Server',
command: 'terraform.disableLanguageServer',
},
])
.then((option) => {
if (!option || !option.command || option.command.length === 0) {
return;
}
vscode.commands.executeCommand(option.command);
});
}),
vscode.commands.registerCommand('terraform.enableLanguageServer', async () => {
if (config('terraform').get('languageServer.enable') === true) {
return startLanguageServer(context);
Expand Down Expand Up @@ -206,6 +240,16 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
);

await startLanguageServer(context);

/*
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);
}
}

export async function deactivate(): Promise<void> {
Expand Down
27 changes: 27 additions & 0 deletions src/status/terraform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as vscode from 'vscode';

const terraformStatus = vscode.languages.createLanguageStatusItem('terraform.status', [
{ language: 'terraform' },
{ language: 'terraform-vars' },
]);
terraformStatus.name = 'Terraform';
terraformStatus.detail = 'Terraform';
terraformStatus.command = {
command: 'terraform.commands',
title: 'Terraform Commands',
tooltip: 'foo',
};

export function setTerraformState(
detail: string,
busy: boolean,
severity: vscode.LanguageStatusSeverity = vscode.LanguageStatusSeverity.Information,
) {
terraformStatus.busy = busy;
terraformStatus.detail = detail;
terraformStatus.severity = severity;
}

export function setTerraformVersion(version: string) {
terraformStatus.text = version;
}
26 changes: 25 additions & 1 deletion src/terraform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as tfStatus from './status/terraform';
import TelemetryReporter from '@vscode/extension-telemetry';
import * as vscode from 'vscode';
import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClient } from 'vscode-languageclient/node';
import {
ExecuteCommandParams,
ExecuteCommandRequest,
LanguageClient,
WorkDoneProgress,
} from 'vscode-languageclient/node';
import { Utils } from 'vscode-uri';
import { getActiveTextEditor } from './utils/vscode';
import { clientSupportsCommand } from './utils/clientHelpers';
Expand All @@ -10,6 +16,12 @@ export interface ModuleCaller {
uri: string;
}

export interface TerraformInfoResponse {
v: number;
required_version: string;
discovered_version: string;
discovered_path: string;
}
export interface ModuleCallersResponse {
v: number;
callers: ModuleCaller[];
Expand Down Expand Up @@ -44,6 +56,18 @@ interface ModuleProvidersResponse {
}
/* eslint-enable @typescript-eslint/naming-convention */

export async function terraformVersion(
moduleUri: string,
client: LanguageClient,
reporter: TelemetryReporter,
): Promise<TerraformInfoResponse> {
const command = 'terraform-ls.module.terraform';

const response = await execWorkspaceLSCommand<TerraformInfoResponse>(command, moduleUri, client, reporter);

return response;
}

export async function moduleCallers(
moduleUri: string,
client: LanguageClient,
Expand Down

0 comments on commit f9a1cdc

Please sign in to comment.