dockerhub-description/src/input-helper.ts

69 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-09-23 08:58:52 +02:00
import * as core from '@actions/core'
const README_FILEPATH_DEFAULT = './README.md'
interface Inputs {
2020-09-25 04:13:16 +02:00
username: string
password: string
repository: string
2020-09-23 08:58:52 +02:00
readmeFilepath: string
}
export function getInputs(): Inputs {
const inputs = {
2020-09-25 04:13:16 +02:00
username: core.getInput('username'),
password: core.getInput('password'),
repository: core.getInput('repository'),
2020-09-23 08:58:52 +02:00
readmeFilepath: core.getInput('readme-filepath')
}
// Environment variable input alternatives and their aliases
2020-09-25 04:13:16 +02:00
if (!inputs.username && process.env['DOCKERHUB_USERNAME']) {
inputs.username = process.env['DOCKERHUB_USERNAME']
2020-09-23 08:58:52 +02:00
}
2020-09-25 04:13:16 +02:00
if (!inputs.username && process.env['DOCKER_USERNAME']) {
inputs.username = process.env['DOCKER_USERNAME']
2020-09-23 08:58:52 +02:00
}
2020-09-25 04:13:16 +02:00
if (!inputs.password && process.env['DOCKERHUB_PASSWORD']) {
inputs.password = process.env['DOCKERHUB_PASSWORD']
2020-09-23 08:58:52 +02:00
}
2020-09-25 04:13:16 +02:00
if (!inputs.password && process.env['DOCKER_PASSWORD']) {
inputs.password = process.env['DOCKER_PASSWORD']
2020-09-23 08:58:52 +02:00
}
2020-09-25 04:13:16 +02:00
if (!inputs.repository && process.env['DOCKERHUB_REPOSITORY']) {
inputs.repository = process.env['DOCKERHUB_REPOSITORY']
2020-09-23 08:58:52 +02:00
}
2020-09-25 04:13:16 +02:00
if (!inputs.repository && process.env['DOCKER_REPOSITORY']) {
inputs.repository = process.env['DOCKER_REPOSITORY']
2020-09-23 08:58:52 +02:00
}
2020-09-25 04:13:16 +02:00
if (!inputs.repository && process.env['GITHUB_REPOSITORY']) {
inputs.repository = process.env['GITHUB_REPOSITORY']
2020-09-23 08:58:52 +02:00
}
if (!inputs.readmeFilepath && process.env['README_FILEPATH']) {
inputs.readmeFilepath = process.env['README_FILEPATH']
}
// Set defaults
if (!inputs.readmeFilepath) {
inputs.readmeFilepath = README_FILEPATH_DEFAULT
}
return inputs
}
function checkRequiredInput(input: string, name: string): void {
if (!input) {
throw new Error(`Required input '${name}' is missing.`)
}
}
export function validateInputs(inputs: Inputs): void {
2020-09-25 04:13:16 +02:00
checkRequiredInput(inputs.username, 'username')
checkRequiredInput(inputs.password, 'password')
checkRequiredInput(inputs.repository, 'repository')
2020-09-23 08:58:52 +02:00
}