autorestic/src/index.ts

136 lines
3.8 KiB
TypeScript
Raw Normal View History

2020-11-16 17:30:32 +01:00
import colors from 'colors'
2020-11-06 23:51:23 +01:00
import { program } from 'commander'
2020-11-13 15:48:40 +01:00
import { setCIMode } from 'clitastic'
2020-11-06 23:51:23 +01:00
2020-12-09 00:07:03 +01:00
import { unlock, readLock, writeLock, lock } from './lock'
2020-11-06 23:51:23 +01:00
import { Config } from './types'
import { init } from './config'
import info from './handlers/info'
import check from './handlers/check'
import backup from './handlers/backup'
import restore from './handlers/restore'
import forget from './handlers/forget'
import { cron } from './handlers/cron'
import exec from './handlers/exec'
import install from './handlers/install'
import { uninstall } from './handlers/uninstall'
import { upgrade } from './handlers/upgrade'
2020-12-19 17:32:13 +01:00
export const VERSION = '0.27'
2020-11-06 23:51:23 +01:00
export const INSTALL_DIR = '/usr/local/bin'
2020-11-14 00:33:52 +01:00
2020-11-13 16:27:19 +01:00
let requireConfig: boolean = true
2020-11-14 00:33:52 +01:00
let error: boolean = false
export function hasError() {
error = true
}
2020-11-06 23:51:23 +01:00
process.on('uncaughtException', (err) => {
console.log(err.message)
unlock()
process.exit(1)
})
2020-11-14 00:33:52 +01:00
let queue: () => Promise<void> = async () => {}
2020-11-06 23:51:23 +01:00
const enqueue = (fn: Function) => (cmd: any) => {
2020-11-14 00:33:52 +01:00
queue = async () => fn(cmd.opts())
2020-11-06 23:51:23 +01:00
}
program.storeOptionsAsProperties()
2020-11-07 12:55:16 +01:00
program.name('autorestic').description('Easy Restic CLI Utility').version(VERSION)
2020-11-06 23:51:23 +01:00
2020-11-13 15:48:40 +01:00
program.option('-c, --config <path>', 'Config file')
program.option('-v, --verbose', 'Verbosity', false)
program.option('--ci', 'CI Mode. Removes interactivity from the shell', false)
2020-11-06 23:51:23 +01:00
program.command('info').action(enqueue(info))
2020-11-07 12:55:16 +01:00
program.on('--help', () => {
console.log('')
console.log(`${'Docs:'.yellow}\t\thttps://autorestic.vercel.app`)
console.log(`${'Examples:'.yellow}\thttps://autorestic.vercel.app/examples`)
})
2020-11-06 23:51:23 +01:00
program
.command('check')
.description('Checks and initializes backend as needed')
.option('-b, --backend <backends...>')
.option('-a, --all')
.action(enqueue(check))
2020-11-14 00:33:52 +01:00
program.command('backup').description('Performs a backup').option('-l, --location <locations...>').option('-a, --all').action(enqueue(backup))
2020-11-06 23:51:23 +01:00
program
.command('restore')
.description('Restores data to a specified folder from a location')
.requiredOption('-l, --location <location>')
.option('--from <backend>')
.requiredOption('--to <path>', 'Path to save the restored data to')
.action(enqueue(restore))
program
.command('forget')
.description('This will prune and remove data according to your policies')
.option('-l, --location <locations...>')
.option('-a, --all')
.option('--dry-run')
.action(enqueue(forget))
program
.command('cron')
.description('Intended to be triggered by an automated system like systemd or crontab.')
.option('-a, --all')
.action(enqueue(cron))
program
.command('exec')
.description('Run any native restic command on desired backends')
.option('-b, --backend <backends...>')
.option('-a, --all')
.action(({ args, all, backend }) => {
2020-11-14 00:33:52 +01:00
queue = async () => exec({ all, backend }, args)
2020-11-06 23:51:23 +01:00
})
program.command('install').description('Installs both restic and autorestic to /usr/local/bin').action(enqueue(install))
program.command('uninstall').description('Uninstalls autorestic from the system').action(enqueue(uninstall))
2020-11-13 16:27:19 +01:00
program
.command('upgrade')
.alias('update')
.description('Checks and installs new autorestic versions')
.action(() => {
requireConfig = false
queue = upgrade
})
2020-11-06 23:51:23 +01:00
2020-11-13 15:48:40 +01:00
const { verbose, config: configFile, ci } = program.parse(process.argv)
2020-11-06 23:51:23 +01:00
export const VERBOSE = verbose
2020-11-13 16:27:19 +01:00
export let config: Config
2020-11-13 15:48:40 +01:00
setCIMode(ci)
2020-11-16 17:30:32 +01:00
if (ci) colors.disable()
async function main() {
2020-11-14 00:33:52 +01:00
try {
2020-11-29 15:27:32 +01:00
if (requireConfig) {
config = init(configFile)
2020-12-09 00:07:03 +01:00
const { running } = readLock()
if (running) {
console.log('An instance of autorestic is already running for this config file'.red)
process.exit(1)
}
lock()
2020-11-29 15:27:32 +01:00
}
2020-11-14 00:33:52 +01:00
await queue()
} catch (e) {
console.error(e.message)
} finally {
2020-11-29 15:27:32 +01:00
if (requireConfig) unlock()
2020-11-14 00:33:52 +01:00
}
2020-12-09 00:07:03 +01:00
if (error) process.exit(1)
2020-11-16 17:30:32 +01:00
}
main()