Skip to content

Commit

Permalink
check for valid exec_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanshire committed Jan 12, 2021
1 parent 7ed69fa commit f19bca5
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const core = require('@actions/core');
const github = require('@actions/github');
const { exec } = require('child_process');
const https = require('https')

try {
Expand All @@ -8,12 +9,17 @@ try {
const name = core.getInput('name')
const org = core.getInput('org')
const desc = core.getInput('desc')
const exec_mode = core.getInput('exec_mode')
const tf_version = core.getInput('tf_version')
var exec_mode = core.getInput('exec_mode')
var tf_version = core.getInput('tf_version')
const env = core.getInput('env')

if (exec_mode == null) {
if (!exec_mode) {
exec_mode = 'remote'
} else {
exec_mode = exec_mode.toLocaleLowerCase()
}
if (exec_mode != 'local' && exec_mode != 'remote') {
throw ('Invalid value for "exec-mode"')
}

// Build JSON Payload with Inputs
Expand All @@ -24,7 +30,7 @@ try {
"description": desc,
"environment": env,
"readme": null,
"execution-mode": exec_mode,
"execution-mode": exec_mode.toLowerCase(),
"auto-apply": false,
"working-directory": "",
"terraform-version": tf_version,
Expand Down Expand Up @@ -66,17 +72,20 @@ try {
console.log(`Unauthorized to created workspace in the "${org}" organization.`)
} else if (res.statusCode == '404') {
console.log(`Organization "${org}" not found in Terraform Cloud.`)
} else if (res.statusCode == '422') {
console.log(`Workspace named "${name}" already exists in the "${org}" organization.`)
} else {
res.on('data', d => {
process.stdout.write(d)
if (d.includes('Name has already been taken')) {
console.log(`Workspace named "${name}" already exists in the "${org}" organization.`)
} else {
process.stdout.write(d)
}
})
}
return res
})
req.write(data)
req.end()

} catch (error) {
console.log(error)
core.setFailed(error.message)
}

0 comments on commit f19bca5

Please sign in to comment.