Skip to content

Commit ea8f54e

Browse files
authored
Merge pull request #17 from stackql/feature/updates
added dry run
2 parents b29e0cb + 3bd4e3e commit ea8f54e

File tree

8 files changed

+66
-25
lines changed

8 files changed

+66
-25
lines changed

.github/workflows/stackql-exec-test.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: 'stackql query tests'
1+
name: 'stackql-exec tests'
22

33
on:
44
push:
@@ -114,6 +114,21 @@ jobs:
114114
GOOGLE_PROJECT: ${{ vars.GOOGLE_PROJECT }}
115115
GOOGLE_ZONE: ${{ vars.GOOGLE_ZONE }}
116116

117+
#
118+
# run a query using the `query_file_path`, `data_file_path` and `vars` inputs (dry-run)
119+
#
120+
- name: google query example with query file and data file using vars (dry-run)
121+
id: stackql-query-file-with-data-file-and-vars-dry-run
122+
uses: ./
123+
with:
124+
query_file_path: './stackql_scripts/google-instances-by-status-with-external-data-file.iql'
125+
data_file_path: './stackql_scripts/google-instances-by-status-with-external-data-file.jsonnet'
126+
vars: GOOGLE_PROJECT=${{ env.GOOGLE_PROJECT }},GOOGLE_ZONE=${{ env.GOOGLE_ZONE }}
127+
dry_run: true
128+
env:
129+
GOOGLE_PROJECT: ${{ vars.GOOGLE_PROJECT }}
130+
GOOGLE_ZONE: ${{ vars.GOOGLE_ZONE }}
131+
117132
#
118133
# run a query using the `query_file_path`, `data_file_path` and `vars` inputs
119134
#

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@ Github Action as a wrapper for executing a single command in stackql, maps all s
77
Authentication to StackQL providers is done via environment variables source from GitHub Actions Secrets. To learn more about authentication, see the setup instructions for your provider or providers at the [StackQL Provider Registry Docs](https://stackql.io/registry).
88

99
## Inputs
10-
- `query` - stackql query to execute **(need to supply either `query` or `query_file_path`)**
11-
- `query_file_path` - stackql query file to execute **(need to supply either `query` or `query_file_path`)**
12-
- `data_file_path` - (optional) path to data file to pass to the stackql query preprocessor (`json` or `jsonnet`)
13-
- `vars` - (optional) comma delimited list of variables to pass to the stackql query preprocessor (supported with `jsonnet` config blocks or `jsonnet` data files only), accepts `var1=val1,var2=val2`, can be used to source environment variables into stackql queries
14-
- `query_output` - (optional) output format of the stackql exec result, accepts `table`, `csv`, `json` and `text`, defaults to `json`
15-
- `auth_obj_path` - (optional) the path of json file that stores stackql AUTH string **(only required when using non-standard environment variable names)**
16-
- `auth_str` - (optional) stackql AUTH string **(only required when using non-standard environment variable names)**
17-
- `is_command` - (optional defaults to 'false') set to true if the stackql execution is a command that does not return data
10+
- **`query`** - stackql query to execute *(need to supply either __`query`__ or __`query_file_path`__)*
11+
- **`query_file_path`** - stackql query file to execute *(need to supply either __`query`__ or __`query_file_path`__)*
12+
- **`data_file_path`** - (optional) path to data file to pass to the stackql query preprocessor (`json` or `jsonnet`)
13+
- **`dry_run`** - (optional) set to `true` to print the query that would be executed without actually executing it (default is `false`)
14+
- **`vars`** - (optional) comma delimited list of variables to pass to the stackql query preprocessor (supported with `jsonnet` config blocks or `jsonnet` data files only), accepts `var1=val1,var2=val2`, can be used to source environment variables into stackql queries
15+
- **`query_output`** - (optional) output format of the stackql exec result, accepts `table`, `csv`, `json` and `text`, defaults to `json`
16+
- **`auth_obj_path`** - (optional) the path of json file that stores stackql AUTH string *(only required when using non-standard environment variable names)*
17+
- **`auth_str`** - (optional) stackql AUTH string *(only required when using non-standard environment variable names)*
18+
- **`is_command`** - (optional) set to `true` if the stackql execution is a command that does not return data (defaults to `false`)
19+
- **`on_failure`** - (optional) behavior on a failure in query, supported values are `exit` (default) and `continue`
1820

1921
## Outputs
2022
This action uses [setup-stackql](https://github.com/marketplace/actions/stackql-studio-setup-stackql), with use_wrapper set
2123
to `true`, `stdout` and `stderr` are set to `exec-result` and `exec-error`
2224

23-
- `exec-result` - The STDOUT stream of the call to the `stackql` binary.
24-
- `exec-error` - The STDERR stream of the call to the `stackql` binary.
25+
- **`stackql-query-results`** - results from a stackql query (in the format specified)
26+
- **`stackql-command-output`** - text output from a stackql command (a query that does not return data)
27+
- **`stackql-query-error`** - error from a stackql query
2528

2629
## Examples
2730

action.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ inputs:
1111
data_file_path:
1212
description: jsonnet or json data file to be passed to query preprocessor
1313
required: false
14+
dry_run:
15+
description: set to true to print the query that would be executed without actually executing it (default is false)
16+
required: false
17+
default: 'false'
1418
vars:
1519
description: comma delimited list of vars to be passed to query preprocessor (supported with jsonnet config blocks or jsonnet data files only)
1620
required: false
@@ -91,6 +95,7 @@ runs:
9195
QUERY_FILE_PATH: ${{ inputs.query_file_path }}
9296
QUERY: ${{inputs.query}}
9397
DATA_FILE_PATH: ${{inputs.data_file_path}}
98+
DRY_RUN: ${{inputs.dry_run}}
9499
OUTPUT: ${{inputs.query_output}}
95100
VARS: ${{inputs.vars}}
96101

@@ -103,9 +108,10 @@ runs:
103108
const utilsPath = path.join(process.env.GITHUB_ACTION_PATH, 'lib', 'utils.js')
104109
const {execStackQLQuery} = require(utilsPath);
105110
const onFailure = process.env.ON_FAILURE || 'exit'; // default to 'exit' if not specified
106-
await execStackQLQuery(core, process.env.STACKQL_COMMAND, process.env.IS_COMMAND === 'true', onFailure);
111+
await execStackQLQuery(core, process.env.STACKQL_COMMAND, process.env.IS_COMMAND === 'true', onFailure, process.env.DRY_RUN === 'true');
107112
env:
108113
IS_COMMAND: ${{ inputs.is_command }}
114+
DRY_RUN: ${{ inputs.dry_run }}
109115
ON_FAILURE: ${{ inputs.on_failure }}
110116

111117
branding:

lib/tests/failed-result.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/tests/success-result.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/tests/test-auth.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/tests/utils.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ describe("Utils Functions Tests", () => {
44
let core;
55

66
beforeEach(() => {
7+
78
core = {
9+
debug: jest.fn().mockImplementation((message) => console.log(`DEBUG: ${message}`)),
810
setFailed: jest.fn().mockImplementation((message) => console.error(message)),
911
info: jest.fn().mockImplementation((message) => console.log(message)),
1012
exportVariable: jest.fn(),

lib/utils.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ function setupAuth(core) {
3838

3939
async function getStackqlCommand(core) {
4040

41-
const [query, queryFilePath, auth, output = "json", vars, dataFilePath] = [
41+
const [query, queryFilePath, dry_run, auth, output = "json", vars, dataFilePath] = [
4242
process.env.QUERY,
4343
process.env.QUERY_FILE_PATH,
44+
process.env.DRY_RUN,
4445
process.env.AUTH,
4546
process.env.OUTPUT,
4647
process.env.VARS,
@@ -94,6 +95,12 @@ async function getStackqlCommand(core) {
9495

9596
args.push(`--output ${output}`);
9697

98+
if (checkEnvVarValid(dry_run)) {
99+
if(dry_run === "true"){
100+
args.push(`--dryrun`);
101+
}
102+
}
103+
97104
if (checkEnvVarValid(auth)) {
98105
args.push(`--auth "${auth}"`);
99106
}
@@ -107,7 +114,7 @@ async function getStackqlCommand(core) {
107114
try {
108115
const stackQLCommand = `${stackQLExecutable} ${args.join(" ")}`;
109116
core.exportVariable('STACKQL_COMMAND', `${stackQLCommand}`);
110-
core.info(`STACKQL_COMMAND: ${stackQLCommand}`);
117+
core.debug(`STACKQL_COMMAND: ${stackQLCommand}`);
111118
} catch (error) {
112119
core.error(error);
113120
core.setFailed("error when executing stackql");
@@ -124,16 +131,22 @@ const checkEnvVarValid = (variable) => {
124131
* @param {string} command - The StackQL command to be executed.
125132
* @param {boolean} isCommand - Indicates if the operation is a command (true) or query (false).
126133
* @param {string} onFailure - The action to take if the command fails. Either 'exit' or 'continue'.
134+
* @param {boolean} dryRun - Indicates if the operation is a dry run only.
127135
*/
128-
async function execStackQLQuery(core, command, isCommand, onFailure) {
129-
130-
if (onFailure !== 'exit' && onFailure !== 'continue') {
131-
core.setFailed(`onFailure must be 'exit' or 'continue'. Received: ${onFailure}`);
132-
return;
136+
async function execStackQLQuery(core, command, isCommand, onFailure, dryRun) {
137+
138+
if (dryRun) {
139+
// dry run
140+
core.info(`dry-run enabled, skipping stackql query execution`);
141+
} else {
142+
// real query
143+
if (onFailure !== 'exit' && onFailure !== 'continue') {
144+
core.setFailed(`onFailure must be 'exit' or 'continue'. Received: ${onFailure}`);
145+
return;
146+
}
147+
core.info(`executing stackql query (isCommand: ${isCommand}): ${command}`);
133148
}
134149

135-
core.info(`executing stackql query (isCommand: ${isCommand}): ${command}`);
136-
137150
try {
138151

139152
let { stdout, stderr } = await execAsync(command);
@@ -156,7 +169,12 @@ async function execStackQLQuery(core, command, isCommand, onFailure) {
156169
core.debug(`STDOUT: ${stdout}`);
157170
if (!isCommand) {
158171
core.setOutput('stackql-query-results', stdout);
159-
core.info(`query output:\n${stdout}`);
172+
if(dryRun){
173+
const json = JSON.parse(stdout);
174+
core.info(`dry-run query:\n${json[0].query}`);
175+
} else {
176+
core.info(`query output:\n${stdout}`);
177+
}
160178
}
161179
} else {
162180
core.debug('STDOUT: <empty>');

0 commit comments

Comments
 (0)