mirror of
https://github.com/actions/runner-container-hooks.git
synced 2025-12-16 09:46:43 +00:00
Initial Commit
This commit is contained in:
56
packages/docker/src/utils.ts
Normal file
56
packages/docker/src/utils.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
/* eslint-disable import/no-commonjs */
|
||||
import * as core from '@actions/core'
|
||||
// Import this way otherwise typescript has errors
|
||||
const exec = require('@actions/exec')
|
||||
|
||||
export interface RunDockerCommandOptions {
|
||||
workingDir?: string
|
||||
input?: Buffer
|
||||
}
|
||||
|
||||
export async function runDockerCommand(
|
||||
args: string[],
|
||||
options?: RunDockerCommandOptions
|
||||
): Promise<string> {
|
||||
const pipes = await exec.getExecOutput('docker', args, options)
|
||||
if (pipes.exitCode !== 0) {
|
||||
core.error(`Docker failed with exit code ${pipes.exitCode}`)
|
||||
return Promise.reject(pipes.stderr)
|
||||
}
|
||||
return Promise.resolve(pipes.stdout)
|
||||
}
|
||||
|
||||
export function sanitize(val: string): string {
|
||||
if (!val || typeof val !== 'string') {
|
||||
return ''
|
||||
}
|
||||
const newNameBuilder: string[] = []
|
||||
for (let i = 0; i < val.length; i++) {
|
||||
const char = val.charAt(i)
|
||||
if (!newNameBuilder.length) {
|
||||
if (isAlpha(char)) {
|
||||
newNameBuilder.push(char)
|
||||
}
|
||||
} else {
|
||||
if (isAlpha(char) || isNumeric(char) || char === '_') {
|
||||
newNameBuilder.push(char)
|
||||
}
|
||||
}
|
||||
}
|
||||
return newNameBuilder.join('')
|
||||
}
|
||||
|
||||
// isAlpha accepts single character and checks if
|
||||
// that character is [a-zA-Z]
|
||||
function isAlpha(val: string): boolean {
|
||||
return (
|
||||
val.length === 1 &&
|
||||
((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'))
|
||||
)
|
||||
}
|
||||
|
||||
function isNumeric(val: string): boolean {
|
||||
return val.length === 1 && val >= '0' && val <= '9'
|
||||
}
|
||||
Reference in New Issue
Block a user