import { Command } from 'commander-ts'; import { BuildParameters, CloudRunner, ImageTag, Input } from '..'; import * as core from '@actions/core'; import { ActionYamlReader } from '../input-readers/action-yaml'; import CloudRunnerLogger from '../cloud-runner/services/cloud-runner-logger'; import { CliFunction, GetAllCliModes, GetCliFunctions } from './cli-decorator'; import CloudRunnerQueryOverride from '../cloud-runner/services/cloud-runner-query-override'; export class CLI { public static options; static get cliMode() { return CLI.options !== undefined && CLI.options.mode !== undefined && CLI.options.mode !== ''; } public static query(key, alternativeKey) { if (CLI.options && CLI.options[key] !== undefined) { return CLI.options[key]; } if (CLI.options && alternativeKey && CLI.options[alternativeKey] !== undefined) { return CLI.options[alternativeKey]; } return; } public static InitCliMode() { const program = new Command(); program.version('0.0.1'); const properties = Object.getOwnPropertyNames(Input); const actionYamlReader: ActionYamlReader = new ActionYamlReader(); for (const element of properties) { program.option(`--${element} <${element}>`, actionYamlReader.GetActionYamlValue(element)); } program.option( '-m, --mode ', GetAllCliModes() .map((x) => `${x.key} (${x.description})`) .join(` | `), ); program.option('--populateOverride ', 'should use override query to pull input false by default'); program.option('--cachePushFrom ', 'cache push from source folder'); program.option('--cachePushTo ', 'cache push to caching folder'); program.option('--artifactName ', 'caching artifact name'); program.parse(process.argv); CLI.options = program.opts(); return CLI.cliMode; } static async RunCli(): Promise { Input.githubInputEnabled = false; if (CLI.options['populateOverride'] === `true`) { await CloudRunnerQueryOverride.PopulateQueryOverrideInput(); } CLI.logInput(); const results = GetCliFunctions(CLI.options.mode); CloudRunnerLogger.log(`Entrypoint: ${results.key}`); CLI.options.versioning = 'None'; return await results.target[results.propertyKey](); } @CliFunction(`print-input`, `prints all input`) private static logInput() { core.info(`\n`); core.info(`INPUT:`); const properties = Object.getOwnPropertyNames(Input); for (const element of properties) { if ( Input[element] !== undefined && Input[element] !== '' && typeof Input[element] !== `function` && element !== 'length' && element !== 'cliOptions' && element !== 'prototype' ) { core.info(`${element} ${Input[element]}`); } } core.info(`\n`); } @CliFunction(`cli`, `runs a cloud runner build`) public static async CLIBuild(): Promise { const buildParameter = await BuildParameters.create(); const baseImage = new ImageTag(buildParameter); return await CloudRunner.run(buildParameter, baseImage.toString()); } }