Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search for input relative to the active window #30

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ Search for "Open Policy Agent" in the Extensions (Shift ⌘ X) panel and then in

## Tips

### Set the `input` document by creating `input.json` at the root of your workspace.
### Set the `input` document by creating `input.json`

The extension will look for a file called `input.json` in the root of the workspace to specify for the `input` document when you evaluate policies. If you modify this file and re-run evaluation you will see the affect of the changes.
The extension will look for a file called `input.json` in the current directory of the policy file being evaluated, or at the root of the workspace, and will use it as the `input` document when evaluating policies. If you modify this file and re-run evaluation you will see the affect of the changes.

### Bind keyboard shortcuts for frequently used commands.

Expand Down
43 changes: 26 additions & 17 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,28 @@ function setFileCoverage(result: any) {
});
}

function setEvalOutput(provider: JSONProvider, uri: vscode.Uri, error: string, result: any) {
function setEvalOutput(provider: JSONProvider, uri: vscode.Uri, error: string, result: any, inputPath: string) {
if (error !== '') {
opaOutputShowError(error);
} else {
opaOutputHide();
let inputMessage: string
if (inputPath === '') {
inputMessage = 'no input file'
} else {
inputMessage = inputPath.replace('file://','');
inputMessage = vscode.workspace.asRelativePath(inputMessage);
}
if (result.result === undefined) {
provider.set(outputUri, `// No results found. Took ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}.`, undefined);
provider.set(outputUri, `// No results found. Took ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}. Used ${inputMessage} as input.`, undefined);
} else {
let output: any;
if (result.result[0].bindings === undefined) {
output = result.result.map((x: any) => x.expressions.map((x: any) => x.value));
} else {
output = result.result.map((x: any) => x.bindings);
}
provider.set(uri, `// Found ${result.result.length} result${result.result.length === 1 ? "" : "s"} in ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}.`, output);
provider.set(uri, `// Found ${result.result.length} result${result.result.length === 1 ? "" : "s"} in ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)} using ${inputMessage} as input.`, output);
}
}
}
Expand Down Expand Up @@ -253,19 +260,12 @@ function activateEvalPackage(context: vscode.ExtensionContext) {
let inputPath = getInputPath();
if (existsSync(inputPath)) {
args.push('--input', inputPath);
} else {
inputPath = '';
}

opa.run('opa', args, 'data.' + pkg, (error: string, result: any) => {
if (error !== '') {
opaOutputShowError(error);
} else {
opaOutputHide();
if (result.result === undefined) {
provider.set(outputUri, `// No results found. Took ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}.`, undefined);
} else {
provider.set(outputUri, `// Evaluated package in ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}.`, result.result[0].expressions[0].value);
}
}
setEvalOutput(provider, outputUri, error, result, inputPath);
});
}, (error: string) => {
opaOutputShowError(error);
Expand Down Expand Up @@ -293,6 +293,8 @@ function activateEvalSelection(context: vscode.ExtensionContext) {
let inputPath = getInputPath();
if (existsSync(inputPath)) {
args.push('--input', inputPath);
} else {
inputPath = '';
}

imports.forEach((x: string) => {
Expand All @@ -302,7 +304,7 @@ function activateEvalSelection(context: vscode.ExtensionContext) {
let text = editor.document.getText(editor.selection);

opa.run('opa', args, text, (error: string, result: any) => {
setEvalOutput(provider, outputUri, error, result);
setEvalOutput(provider, outputUri, error, result, inputPath);
});
}, (error: string) => {
opaOutputShowError(error);
Expand Down Expand Up @@ -342,6 +344,8 @@ function activateEvalCoverage(context: vscode.ExtensionContext) {
let inputPath = getInputPath();
if (existsSync(inputPath)) {
args.push('--input', inputPath);
} else {
inputPath = '';
}

imports.forEach((x: string) => {
Expand All @@ -351,7 +355,7 @@ function activateEvalCoverage(context: vscode.ExtensionContext) {
let text = editor.document.getText(editor.selection);

opa.run('opa', args, text, (error: string, result: any) => {
setEvalOutput(provider, outputUri, error, result);
setEvalOutput(provider, outputUri, error, result, inputPath);
setFileCoverage(result.coverage);
showCoverageForWindow();
});
Expand Down Expand Up @@ -642,11 +646,16 @@ function existsSync(path: string): boolean {
}

function getInputPath(): string {

// look for input.json at the active editor's directory, or the workspace directory
let parsed = vscode.workspace.workspaceFolders![0].uri;
const activeDir = path.dirname(vscode.window.activeTextEditor!.document.uri.fsPath)
if (fs.existsSync(path.join(activeDir,'/input.json'))) {
parsed = vscode.Uri.parse(activeDir)
}

// If the rootDir is a file:// URL then just append /input.json onto the
// end. Otherwise use the path.join function to get a platform-specific file
// path returned.
const parsed = vscode.workspace.workspaceFolders![0].uri;
let rootDir = opa.getDataDir(parsed);

if (parsed.scheme === 'file') {
Expand Down