Skip to content

Commit

Permalink
feat(first_run_wizzard): Add first run wizzard asking user to install…
Browse files Browse the repository at this point in the history
… terraform version
  • Loading branch information
Noahnc committed Sep 11, 2023
1 parent b99064c commit bc23475
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the terraform-toolbox extension will be documented in this file.

## [0.2.2]

- (feat): Added a welcome message asking the user if he wants to install a terraform version.

## [0.2.1]

- (tests) Added additional tests.
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/Noahnc/vscode-terraform-toolbox"
},
"description": "VSCode extension adding a bunch of featurees regarding Terraform and Spacelift.",
"version": "0.2.1",
"version": "0.2.2",
"icon": "Images/terraform_toolbox_icon.png",
"engines": {
"vscode": "^1.81.0"
Expand Down Expand Up @@ -102,6 +102,11 @@
"default": true,
"description": "Disable startup waring if spacectl is not installed and authenticated."
},
"tftoolbox.spacelift.showNoTerraformVersionInstalledMsg": {
"type": "boolean",
"default": true,
"description": "Shows an informtion msg on startup if no terraform version is installed by the extension."
},
"tftoolbox.spacelift.stackPendingConfirmationStatusItemUpdateTimeSeconds": {
"type": "number",
"default": 30,
Expand Down
40 changes: 25 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { TfActiveWorkspaceItem } from "./view/statusbar/terraform_workspace_item
import { TerraformVersionProvieder } from "./utils/terraform/terraform_version_provider";
import { VersionManager } from "./utils/version_manager";
import { Cli } from "./utils/cli";
import * as helpers from "./utils/helper_functions";

export async function activate(context: vscode.ExtensionContext) {
const settings = new Settings();
Expand Down Expand Up @@ -85,21 +86,18 @@ export async function activate(context: vscode.ExtensionContext) {
})
.catch((error) => {
getLogger().error("Failed to initialize spacectl: " + error);
if (vscode.workspace.getConfiguration().get("tftoolbox.spacelift.showSpaceliftInitErrorOnStart")) {
vscode.window
.showWarningMessage(
"Failed to initialize spacectl. Please make sure spacectl is installed and a profile is configured. Somme features will be disabled until spacectl is configured.",
"Open spacectl documentation",
"Don't show again"
)
.then((selection) => {
if (selection === "Open spacectl documentation") {
vscode.env.openExternal(vscode.Uri.parse("https://github.com/spacelift-io/spacectl"));
} else if (selection === "Don't show again") {
vscode.workspace.getConfiguration().update("tftoolbox.spacelift.showSpaceliftInitErrorOnStart", false, vscode.ConfigurationTarget.Global);
}
});
}
helpers
.showNotificationWithDecisions(
"Failed to initialize spacectl. Some features will be disabled until spacectl is configured.",
"tftoolbox.spacelift.showSpaceliftInitErrorOnStart",
"Open spacectl documentation",
"warning"
)
.then((result) => {
if (result) {
vscode.env.openExternal(vscode.Uri.parse("https://github.com/spacelift-io/spacectl"));
}
});
});
// Terraform version management commands
const setTFVersionBasedOnProjectCommand = new SetTerraformVersionBasedOnProjectRequirementsCommand(
Expand Down Expand Up @@ -132,6 +130,18 @@ export async function activate(context: vscode.ExtensionContext) {
});
}

if (tfVersionManager.getActiveVersion() === undefined) {
if (
await helpers.showNotificationWithDecisions(
"No Terraform version installed by this extension yet. Do you want to select a version to install now?",
"tftoolbox.spacelift.showNoTerraformVersionInstalledMsg",
"Show versions",
"information"
)
) {
await vscode.commands.executeCommand(cst.COMMAND_SET_TERRAFORM_VERSION);
}
}
// Init all terraform projects if setting is enabled
if (settings.autoInitAllProjects) {
getLogger().info("Auto initializing all projects in the currently open workspaces");
Expand Down
36 changes: 22 additions & 14 deletions src/utils/helper_functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import axios from "axios";
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import { getLogger } from "./logger";
Expand Down Expand Up @@ -60,21 +58,31 @@ export async function getUserDecision<T>(placeHolderMsg: string, values: T[], la
return choice;
}

export async function downloadFile(url: string, filePath: string): Promise<boolean> {
try {
const response = await axios.get(url, { responseType: "arraybuffer" });
const fileData = Buffer.from(response.data, "binary");
fs.writeFileSync(filePath, fileData);
if (!fs.existsSync(filePath)) {
getLogger().debug("Error saving file to: " + filePath);
return false;
}
getLogger().debug("File: " + filePath + " has been downloaded");
type MessageTypes = "information" | "warning" | "error";
export async function showNotificationWithDecisions(message: string, settingKey: string, decision: string, type: MessageTypes): Promise<boolean> {
if (vscode.workspace.getConfiguration().get(settingKey) === false) {
getLogger().trace("User has disabled notification: " + settingKey);
return false;
}
let messageFunction;
if (type === "information") {
messageFunction = vscode.window.showInformationMessage;
} else if (type === "warning") {
messageFunction = vscode.window.showWarningMessage;
} else if (type === "error") {
messageFunction = vscode.window.showErrorMessage;
} else {
throw new Error("Unknown notification type: " + type);
}

const selection = await messageFunction(message, decision, "Don't show again");
if (selection === decision) {
return true;
} catch (err) {
getLogger().debug("Error downloading file: " + filePath + " from url: " + url + " error: " + err);
} else if (selection === "Don't show again") {
vscode.workspace.getConfiguration().update(settingKey, false, vscode.ConfigurationTarget.Global);
return false;
}
return false;
}

export function showInformation(message: string, silent = false) {
Expand Down

0 comments on commit bc23475

Please sign in to comment.