From f43e73ce41fe8e47c82390a6b24b73e752185106 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sat, 14 Nov 2020 00:33:52 +0100 Subject: [PATCH] process end in exit --- CHANGELOG.md | 4 ++-- src/backup.ts | 3 ++- src/index.ts | 49 +++++++++++++++++++++++++++++-------------------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5a6ab6..a8645c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,3 @@ -## 0.23 +## 0.24 -- Don't require config for update command +- Exit code on failure diff --git a/src/backup.ts b/src/backup.ts index 47631f2..7b2d5ce 100644 --- a/src/backup.ts +++ b/src/backup.ts @@ -1,7 +1,7 @@ import { Writer } from 'clitastic' import { mkdirSync } from 'fs' -import { config, VERBOSE } from './' +import { config, hasError, VERBOSE } from './' import { getEnvFromBackend } from './backend' import { LocationFromPrefixes } from './config' import { Locations, Location, Backend } from './types' @@ -68,6 +68,7 @@ export const backupSingle = (name: string, to: string, location: Location) => { writer.done(`${name}${to.blue} : ${'Done ✓'.green} (${delta.finished(true)})`) } catch (e) { + hasError() writer.done(`${name}${to.blue} : ${'Failed!'.red} (${delta.finished(true)}) ${e.message}`) } } diff --git a/src/index.ts b/src/index.ts index ca3a3d3..fc7d586 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,9 +17,16 @@ import install from './handlers/install' import { uninstall } from './handlers/uninstall' import { upgrade } from './handlers/upgrade' -export const VERSION = '0.23' +export const VERSION = '0.24' export const INSTALL_DIR = '/usr/local/bin' + let requireConfig: boolean = true +let error: boolean = false + +export function hasError() { + console.log('ERROR!') + error = true +} process.on('uncaughtException', (err) => { console.log(err.message) @@ -27,9 +34,9 @@ process.on('uncaughtException', (err) => { process.exit(1) }) -let queue: Function = () => {} +let queue: () => Promise = async () => {} const enqueue = (fn: Function) => (cmd: any) => { - queue = () => fn(cmd.opts()) + queue = async () => fn(cmd.opts()) } program.storeOptionsAsProperties() @@ -54,7 +61,7 @@ program .option('-a, --all') .action(enqueue(check)) -program.command('backup').description('Performs a backup').option('-b, --backend ').option('-a, --all').action(enqueue(backup)) +program.command('backup').description('Performs a backup').option('-l, --location ').option('-a, --all').action(enqueue(backup)) program .command('restore') @@ -84,7 +91,7 @@ program .option('-b, --backend ') .option('-a, --all') .action(({ args, all, backend }) => { - queue = () => exec({ all, backend }, args) + queue = async () => exec({ all, backend }, args) }) program.command('install').description('Installs both restic and autorestic to /usr/local/bin').action(enqueue(install)) @@ -105,20 +112,22 @@ const { verbose, config: configFile, ci } = program.parse(process.argv) export const VERBOSE = verbose export let config: Config setCIMode(ci) +;(async () => { + try { + const lock = readLock() + if (lock.running) throw new Error('An instance of autorestic is already running for this config file'.red) -try { - const lock = readLock() - if (lock.running) throw new Error('An instance of autorestic is already running for this config file'.red) + writeLock({ + ...lock, + running: true, + }) - writeLock({ - ...lock, - running: true, - }) - - if (requireConfig) config = init(configFile) - queue() -} catch (e) { - console.error(e.message) -} finally { - unlock() -} + if (requireConfig) config = init(configFile) + await queue() + if (error) process.exit(1) + } catch (e) { + console.error(e.message) + } finally { + unlock() + } +})()