From bc23475e2d55c85081258019c2cd87b606edbf63 Mon Sep 17 00:00:00 2001 From: Noahnc Date: Mon, 11 Sep 2023 08:58:47 +0200 Subject: [PATCH] feat(first_run_wizzard): Add first run wizzard asking user to install terraform version --- CHANGELOG.md | 4 ++++ package.json | 7 +++++- src/extension.ts | 40 ++++++++++++++++++++++------------- src/utils/helper_functions.ts | 36 +++++++++++++++++++------------ 4 files changed, 57 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f9a711..63cac55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/package.json b/package.json index a31b5c7..c8e5aa3 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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, diff --git a/src/extension.ts b/src/extension.ts index da4d226..43c79d7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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(); @@ -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( @@ -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"); diff --git a/src/utils/helper_functions.ts b/src/utils/helper_functions.ts index e8fda70..34fddf1 100644 --- a/src/utils/helper_functions.ts +++ b/src/utils/helper_functions.ts @@ -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"; @@ -60,21 +58,31 @@ export async function getUserDecision(placeHolderMsg: string, values: T[], la return choice; } -export async function downloadFile(url: string, filePath: string): Promise { - 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 { + 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) {