Skip to content

Commit

Permalink
Prioritize with over env for inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammathur committed Oct 5, 2020
1 parent c8d4af0 commit 6df9dcc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
11 changes: 9 additions & 2 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ async function cleanup(path: string): Promise<void> {
}

describe('Utils tests', () => {
it('checking readEnv', async () => {
process.env['test'] = 'setup-php';
expect(await utils.readEnv('test')).toBe('setup-php');
expect(await utils.readEnv('undefined')).toBe('');
});

it('checking getInput', async () => {
process.env['test'] = 'setup-php';
process.env['undefined'] = '';
expect(await utils.getInput('test', false)).toBe('setup-php');
expect(await utils.getInput('undefined', false)).toBe('');
expect(await utils.getInput('setup-php', false)).toBe('setup-php');
expect(await utils.getInput('DoesNotExist', false)).toBe('');
expect(async () => {
await utils.getInput('DoesNotExist', true);
}).rejects.toThrow('Input required and not supplied: DoesNotExist');
});

it('checking parseVersion', async () => {
Expand Down
33 changes: 26 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1097,24 +1097,43 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.customPackage = exports.scriptExtension = exports.joins = exports.getCommand = exports.getUnsupportedLog = exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readScript = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.parseVersion = exports.getInput = void 0;
exports.customPackage = exports.scriptExtension = exports.joins = exports.getCommand = exports.getUnsupportedLog = exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readScript = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.parseVersion = exports.getInput = exports.readEnv = void 0;
const fs = __importStar(__webpack_require__(747));
const path = __importStar(__webpack_require__(622));
const core = __importStar(__webpack_require__(470));
/**
* Function to read environment variable and return a string value.
*
* @param property
*/
async function readEnv(property) {
const value = process.env[property];
switch (value) {
case undefined:
return '';
default:
return value;
}
}
exports.readEnv = readEnv;
/**
* Function to get inputs from both with and env annotations.
*
* @param name
* @param mandatory
*/
async function getInput(name, mandatory) {
const input = process.env[name];
switch (input) {
case '':
case undefined:
return core.getInput(name, { required: mandatory });
default:
const input = core.getInput(name);
const env_input = await readEnv(name);
switch (true) {
case input != '':
return input;
case input == '' && env_input != '':
return env_input;
case input == '' && env_input == '' && mandatory:
throw new Error(`Input required and not supplied: ${name}`);
default:
return '';
}
}
exports.getInput = getInput;
Expand Down
31 changes: 25 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import * as fs from 'fs';
import * as path from 'path';
import * as core from '@actions/core';

/**
* Function to read environment variable and return a string value.
*
* @param property
*/
export async function readEnv(property: string): Promise<string> {
const value = process.env[property];
switch (value) {
case undefined:
return '';
default:
return value;
}
}

/**
* Function to get inputs from both with and env annotations.
*
Expand All @@ -12,13 +27,17 @@ export async function getInput(
name: string,
mandatory: boolean
): Promise<string> {
const input = process.env[name];
switch (input) {
case '':
case undefined:
return core.getInput(name, {required: mandatory});
default:
const input = core.getInput(name);
const env_input = await readEnv(name);
switch (true) {
case input != '':
return input;
case input == '' && env_input != '':
return env_input;
case input == '' && env_input == '' && mandatory:
throw new Error(`Input required and not supplied: ${name}`);
default:
return '';
}
}

Expand Down

0 comments on commit 6df9dcc

Please sign in to comment.