From a97380e90dadcf56ed35a6d638002c420507f0c0 Mon Sep 17 00:00:00 2001 From: Bryan MacFarlane Date: Fri, 17 May 2019 10:23:01 -0400 Subject: [PATCH] update inputs and variables --- packages/core/README.md | 10 ++++++++-- packages/core/src/core.ts | 35 ++++++++++++++++++++++++--------- packages/core/src/interfaces.ts | 8 ++++---- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/packages/core/README.md b/packages/core/README.md index 6a1807fb..8baaab5f 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -5,18 +5,24 @@ ## Usage ``` +//----------------------------------------------------------------------- +// Variables, Inputs and Outputs +//----------------------------------------------------------------------- + /** * sets env variable for this action and future actions in the job * @param name the name of the variable to set * @param val the value of the variable */ -export function exportVariable(name: string, val: string, options?:im.ExportOptions); +export function exportVariable(name: string, val: string); /** * registers a secret which will get masked from logs * @param val value of the secret */ -export function setSecret(val: string); +export function setSecret(name: string, val: string); + +// TODO: follow up and see if we need anything for outputs //----------------------------------------------------------------------- // Results diff --git a/packages/core/src/core.ts b/packages/core/src/core.ts index 87910a0c..013a9a28 100644 --- a/packages/core/src/core.ts +++ b/packages/core/src/core.ts @@ -2,25 +2,46 @@ import im = require('./interfaces'); import intm = require('./internal'); import process = require('process'); +//----------------------------------------------------------------------- +// Variables +//----------------------------------------------------------------------- + /** * sets env variable for this action and future actions in the job * @param name the name of the variable to set * @param val the value of the variable */ -export function exportVariable(name: string, val: string, options?:im.ExportOptions) { +export function exportVariable(name: string, val: string) { process.env[name] = val; - let props = {'name': name, 'isSecret': options? options.isSecret : false}; - intm._issueCommand('set-variable', props, val); + intm._issueCommand('set-variable', {'name': name}, val); } /** - * registers a secret which will get masked from logs + * exports the variable and registers a secret which will get masked from logs + * @param name the name of the variable to set * @param val value of the secret */ -export function setSecret(val: string) { +export function setSecret(name: string, val: string) { + exportVariable(name, val); intm._issueCommand('set-secret', {}, val); } +/** + * Gets the value of an input. The value is also trimmed. + * + * @param name name of the input to get + * @param options optional. See InputOptions. + * @returns string + */ +export function getInput(name: string, options?: im.InputOptions): string { + let val:string = process.env['INPUT_' + name]; + if (options && options.required && !val) { + throw new Error(`Input required and not supplied: ${name}`); + } + + return val; +} + //----------------------------------------------------------------------- // Results //----------------------------------------------------------------------- @@ -44,10 +65,6 @@ export function setFailed(message: string) { //----------------------------------------------------------------------- // Logging Commands -// -// error and warning issues do not take FileDetails because while possible, -// that's typically reserved for the agent and the problem matchers. -// //----------------------------------------------------------------------- /** diff --git a/packages/core/src/interfaces.ts b/packages/core/src/interfaces.ts index 16696ed9..bca96a40 100644 --- a/packages/core/src/interfaces.ts +++ b/packages/core/src/interfaces.ts @@ -19,9 +19,9 @@ export enum ExitCode { } /** - * Interface for exportVariable options + * Interface for getInput options */ -export interface ExportOptions { - /** Optional. Whether the variable should be marked as secret (will be masked from logs). Defaults to false */ - isSecret?: boolean; +export interface InputOptions { + /** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */ + required?: boolean; } \ No newline at end of file