mirror of
https://git.mirrors.martin98.com/https://github.com/actions/toolkit
synced 2026-05-04 18:58:07 +08:00
Be more lenient in accepting command inputs (#405)
* Be more lenient in accepting command inputs
This commit is contained in:
@@ -2,8 +2,11 @@ import * as os from 'os'
|
||||
|
||||
// For internal use, subject to change.
|
||||
|
||||
// We use any as a valid input type
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
interface CommandProperties {
|
||||
[key: string]: string
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -19,7 +22,7 @@ interface CommandProperties {
|
||||
export function issueCommand(
|
||||
command: string,
|
||||
properties: CommandProperties,
|
||||
message: string
|
||||
message: any
|
||||
): void {
|
||||
const cmd = new Command(command, properties, message)
|
||||
process.stdout.write(cmd.toString() + os.EOL)
|
||||
@@ -73,15 +76,28 @@ class Command {
|
||||
}
|
||||
}
|
||||
|
||||
function escapeData(s: string): string {
|
||||
return (s || '')
|
||||
/**
|
||||
* Sanatizes an input into a string so it can be passed into issueCommand safely
|
||||
* @param input input to sanitize into a string
|
||||
*/
|
||||
export function toCommandValue(input: any): string {
|
||||
if (input === null || input === undefined) {
|
||||
return ''
|
||||
} else if (typeof input === 'string' || input instanceof String) {
|
||||
return input as string
|
||||
}
|
||||
return JSON.stringify(input)
|
||||
}
|
||||
|
||||
function escapeData(s: any): string {
|
||||
return toCommandValue(s)
|
||||
.replace(/%/g, '%25')
|
||||
.replace(/\r/g, '%0D')
|
||||
.replace(/\n/g, '%0A')
|
||||
}
|
||||
|
||||
function escapeProperty(s: string): string {
|
||||
return (s || '')
|
||||
function escapeProperty(s: any): string {
|
||||
return toCommandValue(s)
|
||||
.replace(/%/g, '%25')
|
||||
.replace(/\r/g, '%0D')
|
||||
.replace(/\n/g, '%0A')
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {issue, issueCommand} from './command'
|
||||
import {issue, issueCommand, toCommandValue} from './command'
|
||||
|
||||
import * as os from 'os'
|
||||
import * as path from 'path'
|
||||
@@ -33,11 +33,13 @@ export enum ExitCode {
|
||||
/**
|
||||
* 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
|
||||
* @param val the value of the variable. Non-string values will be converted to a string via JSON.stringify
|
||||
*/
|
||||
export function exportVariable(name: string, val: string): void {
|
||||
process.env[name] = val
|
||||
issueCommand('set-env', {name}, val)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function exportVariable(name: string, val: any): void {
|
||||
const convertedVal = toCommandValue(val)
|
||||
process.env[name] = convertedVal
|
||||
issueCommand('set-env', {name}, convertedVal)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,9 +80,10 @@ export function getInput(name: string, options?: InputOptions): string {
|
||||
* Sets the value of an output.
|
||||
*
|
||||
* @param name name of the output to set
|
||||
* @param value value to store
|
||||
* @param value value to store. Non-string values will be converted to a string via JSON.stringify
|
||||
*/
|
||||
export function setOutput(name: string, value: string): void {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function setOutput(name: string, value: any): void {
|
||||
issueCommand('set-output', {name}, value)
|
||||
}
|
||||
|
||||
@@ -93,8 +96,9 @@ export function setOutput(name: string, value: string): void {
|
||||
* When the action exits it will be with an exit code of 1
|
||||
* @param message add error issue message
|
||||
*/
|
||||
export function setFailed(message: string): void {
|
||||
export function setFailed(message: string | Error): void {
|
||||
process.exitCode = ExitCode.Failure
|
||||
|
||||
error(message)
|
||||
}
|
||||
|
||||
@@ -119,18 +123,18 @@ export function debug(message: string): void {
|
||||
|
||||
/**
|
||||
* Adds an error issue
|
||||
* @param message error issue message
|
||||
* @param message error issue message. Errors will be converted to string via toString()
|
||||
*/
|
||||
export function error(message: string): void {
|
||||
issue('error', message)
|
||||
export function error(message: string | Error): void {
|
||||
issue('error', message instanceof Error ? message.toString() : message)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an warning issue
|
||||
* @param message warning issue message
|
||||
* @param message warning issue message. Errors will be converted to string via toString()
|
||||
*/
|
||||
export function warning(message: string): void {
|
||||
issue('warning', message)
|
||||
export function warning(message: string | Error): void {
|
||||
issue('warning', message instanceof Error ? message.toString() : message)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,9 +193,10 @@ export async function group<T>(name: string, fn: () => Promise<T>): Promise<T> {
|
||||
* Saves state for current action, the state can only be retrieved by this action's post job execution.
|
||||
*
|
||||
* @param name name of the state to store
|
||||
* @param value value to store
|
||||
* @param value value to store. Non-string values will be converted to a string via JSON.stringify
|
||||
*/
|
||||
export function saveState(name: string, value: string): void {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function saveState(name: string, value: any): void {
|
||||
issueCommand('save-state', {name}, value)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user