Skip to content

Commit

Permalink
terraform validation configuration hashicorp#36
Browse files Browse the repository at this point in the history
  • Loading branch information
ranga543 committed Mar 30, 2018
1 parent 9458b37 commit 2c5346c
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 79 deletions.
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,42 @@
"delay": 500
},
"description": "Use `terraform-index` (download from: https://github.com/mauve/terraform-index/releases) to get live syntax errors, rename support, go to symbol, and much more..."
},
"terraform.validate": {
"type": "object",
"properties": {
"checkVariables": {
"type": "boolean",
"default": true,
"description": "If set to true (default), the command will check whether all required variables have been specified"
},
"noColor": {
"type": "boolean",
"default": false,
"description": "If specified, output won't contain any color"
},
"vars": {
"type": "array",
"description": "Set a variable in the Terraform configuration. This flag can be set multiple times",
"items": {
"type": "string"
}
},
"varFiles": {
"type": "array",
"description": "Set variables in the Terraform configuration from a file. If \"terraform.tfvars\" is present, it will be automatically loaded if this flag is not specified",
"items": {
"type": "string"
}
},
"environmentVars": {
"type": "array",
"description": "Set a environment variable in the Terraform configuration. This flag can be set multiple times",
"items": {
"type": "string"
}
}
}
}
}
}
Expand Down
69 changes: 35 additions & 34 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import * as vscode from 'vscode';

interface TerraformIndexConfiguration {
enabled: boolean;
indexerPath: string;
liveIndexing: boolean;
liveIndexingDelay: number;
}

interface TerraformConfiguration {
formatOnSave: boolean;
formatVarsOnSave?: boolean;
path: string;
templateDirectory: string;
lintPath: string;
lintConfig?: string;
indexing: TerraformIndexConfiguration;
}

export function getConfiguration(): TerraformConfiguration {
let raw = vscode.workspace.getConfiguration("terraform");

// needed for conversion
let convertible = {
formatOnSave: raw.formatOnSave,
formatVarsOnSave: raw.formatVarsOnSave,
path: raw.path,
templateDirectory: raw.templateDirectory,
lintPath: raw.lintPath,
lintConfig: raw.lintConfig,
indexing: raw.indexing
};

return <TerraformConfiguration>convertible;
import * as vscode from 'vscode';

interface TerraformIndexConfiguration {
enabled: boolean;
indexerPath: string;
liveIndexing: boolean;
liveIndexingDelay: number;
}

interface TerraformConfiguration {
formatOnSave: boolean;
formatVarsOnSave?: boolean;
path: string;
templateDirectory: string;
lintPath: string;
lintConfig?: string;
indexing: TerraformIndexConfiguration;
}

export function getConfiguration(): TerraformConfiguration {
let raw = vscode.workspace.getConfiguration("terraform");

// needed for conversion
let convertible = {
formatOnSave: raw.formatOnSave,
formatVarsOnSave: raw.formatVarsOnSave,
path: raw.path,
templateDirectory: raw.templateDirectory,
lintPath: raw.lintPath,
lintConfig: raw.lintConfig,
indexing: raw.indexing,
validate: raw.validate
};

return <TerraformConfiguration>convertible;
}
122 changes: 77 additions & 45 deletions src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,77 @@
import * as vscode from 'vscode';
import { exec } from 'child_process';
import { outputChannel } from './extension';

export function validateCommand() {
const configuration = vscode.workspace.getConfiguration("terraform");
const workspaceDir = vscode.workspace.rootPath;

if (workspaceDir === undefined) {
vscode.window.showWarningMessage("terraform.Validate can only be used when opening a folder");
return;
}

validate(configuration["path"], configuration["templateDirectory"], workspaceDir)
.then(() => {
vscode.window.showInformationMessage("Validation succeeded.");
}).catch((error) => {
outputChannel.appendLine("terraform.validate: Failed:");
outputChannel.append(error);
outputChannel.show(true);
vscode.window.showErrorMessage("Validation failed, more information in the output tab.");
});
}

function validate(execPath: string, directory: string, workspaceDir: string): Promise<string> {
if (directory === undefined) {
directory = "";
}

return new Promise<string>((resolve, reject) => {
const commandLine = `${execPath} validate -no-color ${directory}`;

const child = exec(commandLine, {
cwd: workspaceDir,
encoding: 'utf8',
maxBuffer: 1024 * 1024
}, (error, stdout, stderr) => {
if (error) {
reject(stderr);
} else {
resolve(stdout);
}
});
});
}
import * as vscode from 'vscode';
import { exec } from 'child_process';
import { outputChannel } from './extension';

export function validateCommand() {
const configuration = vscode.workspace.getConfiguration("terraform");
const workspaceDir = vscode.workspace.rootPath;

if (workspaceDir === undefined) {
vscode.window.showWarningMessage("terraform.Validate can only be used when opening a folder");
return;
}
let validateCommandArgs = "";
let environmentVarArgs: any = {};
if (configuration["validate"]) {
let validateOptions = configuration["validate"];
if ("checkVariables" in validateOptions) {
validateCommandArgs += `-check-variables=${validateOptions["checkVariables"]}`
} else {
validateCommandArgs += "-check-variables=true"
}
if (validateOptions["noColor"]) {
validateCommandArgs += ` -no-color ${validateOptions["noColor"]}`
}
if (validateOptions["vars"]) {
let vars = validateOptions["vars"] as Array<String>
validateCommandArgs = validateCommandArgs + " " + vars.map(v => `-var '${v}'`).join(" ")
}
if (validateOptions["varFiles"]) {
let varFiles = validateOptions["varFiles"] as Array<String>
validateCommandArgs = validateCommandArgs + " " + varFiles.map(v => `-var-file=${v}`).join(" ")
}
if (validateOptions["environmentVars"]) {
let environmentVars = validateOptions["environmentVars"] as Array<String>
environmentVars.forEach(v => {
let index = v.indexOf("=");
if (index > 0) {
const key = v.substring(0, index);
const value = v.substring(index + 1);
environmentVarArgs[`TF_VAR_${key}`] = value;
}
});
}
}
validate(configuration["path"], configuration["templateDirectory"], workspaceDir, validateCommandArgs.trim(), environmentVarArgs)
.then(() => {
vscode.window.showInformationMessage("Validation succeeded.");
}).catch((error) => {
outputChannel.appendLine("terraform.validate: Failed:");
outputChannel.append(error);
outputChannel.show(true);
vscode.window.showErrorMessage("Validation failed, more information in the output tab.");
});
}

function validate(execPath: string, directory: string, workspaceDir: string, validateArgs: string, envVars: any): Promise<string> {
if (directory === undefined) {
directory = "";
}

return new Promise<string>((resolve, reject) => {
const commandLine = `${execPath} validate ${validateArgs} ${directory}`;

const child = exec(commandLine, {
cwd: workspaceDir,
encoding: 'utf8',
maxBuffer: 1024 * 1024,
env: envVars
}, (error, stdout, stderr) => {
if (error) {
reject(stderr);
} else {
resolve(stdout);
}
});
});
}

0 comments on commit 2c5346c

Please sign in to comment.