forked from hashicorp/vscode-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
terraform validation configuration hashicorp#36
- Loading branch information
Showing
3 changed files
with
148 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}); | ||
} |