diff --git a/README.md b/README.md index c047830..006f827 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ The `currents cache` command allows you to archive files from specified location To cache files, run `npx currents cache set --key --id --path `. -To download files, run `npx currents cache set --key --preset last-run`. +To download files, run `npx currents cache get --key --id `. For more examples and usage options, run `npx currents cache --help`. diff --git a/packages/cmd/README.md b/packages/cmd/README.md index 79caa62..fdfe6ba 100644 --- a/packages/cmd/README.md +++ b/packages/cmd/README.md @@ -82,7 +82,7 @@ npx currents cache set --key --id --path --preset last-run +npx currents cache get --key --id ``` For more examples and usage options, run `npx currents cache --help`. diff --git a/packages/cmd/src/commands/cache/get.ts b/packages/cmd/src/commands/cache/get.ts index d9c3c3a..f45d95d 100644 --- a/packages/cmd/src/commands/cache/get.ts +++ b/packages/cmd/src/commands/cache/get.ts @@ -15,15 +15,21 @@ export type CacheGetCommandOpts = ReturnType< >; export async function getCacheGetHandler(options: CacheGetCommandOpts) { - await commandHandler(async (opts) => { - setCacheGetCommandConfig(cacheGetCommandOptsToConfig(opts)); - const config = getCacheCommandConfig(); + await commandHandler( + async (opts) => { + setCacheGetCommandConfig(cacheGetCommandOptsToConfig(opts)); + const config = getCacheCommandConfig(); - debug('Config: %o', { - ...config.values, - recordKey: config.values?.recordKey ? '*****' : undefined, - }); + debug('Config: %o', { + ...config.values, + recordKey: config.values?.recordKey ? '*****' : undefined, + }); - await handleGetCache(); - }, options); + await handleGetCache(); + }, + options, + { + failOnError: options.fail, + } + ); } diff --git a/packages/cmd/src/commands/cache/index.ts b/packages/cmd/src/commands/cache/index.ts index d18858e..762b8d2 100644 --- a/packages/cmd/src/commands/cache/index.ts +++ b/packages/cmd/src/commands/cache/index.ts @@ -7,6 +7,7 @@ import { idOption, matrixIndexOption, matrixTotalOption, + noFailOption, outputDirOption, pathOption, presetOption, @@ -62,6 +63,7 @@ export const getCacheSetCommand = () => { .addOption(pwOutputDirOption) .addOption(matrixIndexOption) .addOption(matrixTotalOption) + .addOption(noFailOption) .action(getCacheSetHandler); return command; @@ -79,6 +81,7 @@ export const getCacheGetCommand = () => { .addOption(debugOption) .addOption(matrixIndexOption) .addOption(matrixTotalOption) + .addOption(noFailOption) .action(getCacheGetHandler); return command; diff --git a/packages/cmd/src/commands/cache/options.ts b/packages/cmd/src/commands/cache/options.ts index a73508c..6d53172 100644 --- a/packages/cmd/src/commands/cache/options.ts +++ b/packages/cmd/src/commands/cache/options.ts @@ -72,3 +72,8 @@ function validatePositiveInteger(value: string) { } return parsedValue; } + +export const noFailOption = new Option( + '--no-fail', + 'Do not fail the process if the command fails' +); diff --git a/packages/cmd/src/commands/cache/set.ts b/packages/cmd/src/commands/cache/set.ts index c118af5..775ed25 100644 --- a/packages/cmd/src/commands/cache/set.ts +++ b/packages/cmd/src/commands/cache/set.ts @@ -15,15 +15,21 @@ export type CacheSetCommandOpts = ReturnType< >; export async function getCacheSetHandler(options: CacheSetCommandOpts) { - await commandHandler(async (opts) => { - setCacheSetCommandConfig(cacheSetCommandOptsToConfig(opts)); - const config = getCacheCommandConfig(); + await commandHandler( + async (opts) => { + setCacheSetCommandConfig(cacheSetCommandOptsToConfig(opts)); + const config = getCacheCommandConfig(); - debug('Config: %o', { - ...config.values, - recordKey: config.values?.recordKey ? '*****' : undefined, - }); + debug('Config: %o', { + ...config.values, + recordKey: config.values?.recordKey ? '*****' : undefined, + }); - await handleSetCache(); - }, options); + await handleSetCache(); + }, + options, + { + failOnError: options.fail, + } + ); } diff --git a/packages/cmd/src/commands/utils.ts b/packages/cmd/src/commands/utils.ts index 58de178..06da5e3 100644 --- a/packages/cmd/src/commands/utils.ts +++ b/packages/cmd/src/commands/utils.ts @@ -1,5 +1,5 @@ import { CommanderError } from '@commander-js/extra-typings'; -import { ValidationError, Warning } from '@lib'; +import { ValidationError } from '@lib'; import { error, warnWithNoTrace } from '@logger'; import { isAxiosError } from 'axios'; import { enableDebug } from '../debug'; @@ -16,36 +16,36 @@ export function parseCommaSeparatedList( export async function commandHandler>( action: (options: T) => Promise, - options: T + commandOptions: T, + options?: { + failOnError?: boolean; + } ) { try { - if (options.debug) { + if (commandOptions.debug) { enableDebug(); } - await action(options); + await action(commandOptions); process.exit(0); } catch (e) { - if (e instanceof CommanderError) { - error(e.message); - process.exit(e.exitCode); - } - - if (e instanceof ValidationError) { - error(e.message); - process.exit(1); - } - - if (e instanceof Warning) { - warnWithNoTrace(e.message); - process.exit(1); - } + const failOnError = options?.failOnError ?? true; + let exitCode = e instanceof CommanderError ? e.exitCode : 1; - if (isAxiosError(e)) { - error(e.message); - process.exit(1); + if ( + e instanceof CommanderError || + e instanceof ValidationError || + isAxiosError(e) + ) { + if (failOnError) { + error(e.message); + } else { + warnWithNoTrace(e.message); + exitCode = 0; + } + } else { + error('Script execution failed: %o', e); } - error('Script execution failed: %o', e); - process.exit(1); + process.exit(exitCode); } } diff --git a/packages/cmd/src/lib/error.ts b/packages/cmd/src/lib/error.ts index 6ee3b88..9595a16 100644 --- a/packages/cmd/src/lib/error.ts +++ b/packages/cmd/src/lib/error.ts @@ -1,3 +1 @@ export class ValidationError extends Error {} - -export class Warning extends Error {} diff --git a/packages/cmd/src/services/cache/get.ts b/packages/cmd/src/services/cache/get.ts index 008d7f8..a960212 100644 --- a/packages/cmd/src/services/cache/get.ts +++ b/packages/cmd/src/services/cache/get.ts @@ -4,7 +4,6 @@ import { retrieveCache } from '../../api'; import { PRESETS } from '../../commands/cache/options'; import { getCacheCommandConfig } from '../../config/cache'; import { getCI } from '../../env/ciProvider'; -import { Warning } from '../../lib'; import { unzipBuffer } from './fs'; import { MetaFile } from './lib'; import { download } from './network'; @@ -52,7 +51,7 @@ export async function handleGetCache() { e.response?.status && (e.response?.status === 403 || e.response?.status === 404) ) { - throw new Warning(`Cache with ID "${result.cacheId}" not found`); + throw new Error(`Cache with ID "${result.cacheId}" not found`); } }