Skip to content

Commit 08940e7

Browse files
authored
fix(tfc/output): add error handling for no output (#2)
1 parent 3ec6ccd commit 08940e7

File tree

4 files changed

+114
-76
lines changed

4 files changed

+114
-76
lines changed

package-lock.json

+93-70
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"dependencies": {
4242
"command-line-args": "^5.2.0",
43-
"follow-redirects": "^1.14.1"
43+
"follow-redirects": "^1.14.1",
44+
"yargs": "^17.2.0"
4445
}
4546
}

scripts/shared/environment.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { existsSync, readFileSync } = require('fs');
22
const path = require('path');
3+
const yargs = require('yargs');
34

45
const currentEnv = {
56
TFCRC_PATH: undefined,
@@ -21,19 +22,28 @@ const Environment = (render) => {
2122
});
2223
}
2324

25+
const args = yargs(process.argv.slice(4))
26+
.options('workspace', {
27+
default: process.env.TFC_WORKSPACE || TFC_CONFIG.TFC_WORKSPACE || TFC_CONFIG.WORKSPACE_ID
28+
})
29+
.options('token', {
30+
default: process.env.TFC_TOKEN || TFC_CONFIG.TFC_TOKEN
31+
})
32+
.argv;
33+
2434
currentEnv.config = {
2535
// args: process.argv,
2636
paths: {
2737
pwd: process.env.PWD,
2838
home: process.env.HOME,
2939
},
3040
secrets: {
31-
TFC_TOKEN: process.env.TFC_TOKEN || TFC_CONFIG.TFC_TOKEN,
32-
TFC_WORKSPACE: process.env.TFC_WORKSPACE || TFC_CONFIG.TFC_WORKSPACE || TFC_CONFIG.WORKSPACE_ID,
41+
TFC_TOKEN: args.token,
42+
TFC_WORKSPACE: args.workspace,
3343
},
3444
action: process.argv[3] || '',
3545
resource: process.argv[2] || '',
36-
args: process.argv.slice(4)
46+
args
3747
}
3848
}
3949

scripts/terraform/resources/output.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ const OUTPUT = {
3030
const state = await Request('app.terraform.io', 'GET', `/api/v2/workspaces/${env.secrets.TFC_WORKSPACE}/current-state-version`, {
3131
'Authorization': `Bearer ${env.secrets.TFC_TOKEN}`
3232
});
33-
34-
const output_id = JSON.parse(state).data.relationships.outputs.data[0].id;
33+
const stateObj = JSON.parse(state);
34+
if (stateObj.data.relationships.outputs.data.length <= 0) {
35+
console.log('error: No outputs in current workspace');
36+
return;
37+
}
38+
const output_id = stateObj.data.relationships.outputs.data[0].id;
3539

3640
let outputs = await Request('app.terraform.io', 'GET', `/api/v2/state-version-outputs/${output_id}`, {
3741
'Authorization': `Bearer ${env.secrets.TFC_TOKEN}`

0 commit comments

Comments
 (0)