process end in exit

This commit is contained in:
cupcakearmy 2020-11-14 00:33:52 +01:00
parent 6b4277b57b
commit f43e73ce41
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
3 changed files with 33 additions and 23 deletions

View File

@ -1,3 +1,3 @@
## 0.23 ## 0.24
- Don't require config for update command - Exit code on failure

View File

@ -1,7 +1,7 @@
import { Writer } from 'clitastic' import { Writer } from 'clitastic'
import { mkdirSync } from 'fs' import { mkdirSync } from 'fs'
import { config, VERBOSE } from './' import { config, hasError, VERBOSE } from './'
import { getEnvFromBackend } from './backend' import { getEnvFromBackend } from './backend'
import { LocationFromPrefixes } from './config' import { LocationFromPrefixes } from './config'
import { Locations, Location, Backend } from './types' 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)})`) writer.done(`${name}${to.blue} : ${'Done ✓'.green} (${delta.finished(true)})`)
} catch (e) { } catch (e) {
hasError()
writer.done(`${name}${to.blue} : ${'Failed!'.red} (${delta.finished(true)}) ${e.message}`) writer.done(`${name}${to.blue} : ${'Failed!'.red} (${delta.finished(true)}) ${e.message}`)
} }
} }

View File

@ -17,9 +17,16 @@ import install from './handlers/install'
import { uninstall } from './handlers/uninstall' import { uninstall } from './handlers/uninstall'
import { upgrade } from './handlers/upgrade' import { upgrade } from './handlers/upgrade'
export const VERSION = '0.23' export const VERSION = '0.24'
export const INSTALL_DIR = '/usr/local/bin' export const INSTALL_DIR = '/usr/local/bin'
let requireConfig: boolean = true let requireConfig: boolean = true
let error: boolean = false
export function hasError() {
console.log('ERROR!')
error = true
}
process.on('uncaughtException', (err) => { process.on('uncaughtException', (err) => {
console.log(err.message) console.log(err.message)
@ -27,9 +34,9 @@ process.on('uncaughtException', (err) => {
process.exit(1) process.exit(1)
}) })
let queue: Function = () => {} let queue: () => Promise<void> = async () => {}
const enqueue = (fn: Function) => (cmd: any) => { const enqueue = (fn: Function) => (cmd: any) => {
queue = () => fn(cmd.opts()) queue = async () => fn(cmd.opts())
} }
program.storeOptionsAsProperties() program.storeOptionsAsProperties()
@ -54,7 +61,7 @@ program
.option('-a, --all') .option('-a, --all')
.action(enqueue(check)) .action(enqueue(check))
program.command('backup').description('Performs a backup').option('-b, --backend <backends...>').option('-a, --all').action(enqueue(backup)) program.command('backup').description('Performs a backup').option('-l, --location <locations...>').option('-a, --all').action(enqueue(backup))
program program
.command('restore') .command('restore')
@ -84,7 +91,7 @@ program
.option('-b, --backend <backends...>') .option('-b, --backend <backends...>')
.option('-a, --all') .option('-a, --all')
.action(({ args, all, backend }) => { .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)) 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 const VERBOSE = verbose
export let config: Config export let config: Config
setCIMode(ci) 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 { writeLock({
const lock = readLock() ...lock,
if (lock.running) throw new Error('An instance of autorestic is already running for this config file'.red) running: true,
})
writeLock({ if (requireConfig) config = init(configFile)
...lock, await queue()
running: true, if (error) process.exit(1)
}) } catch (e) {
console.error(e.message)
if (requireConfig) config = init(configFile) } finally {
queue() unlock()
} catch (e) { }
console.error(e.message) })()
} finally {
unlock()
}