diff --git a/src/autorestic.ts b/src/autorestic.ts index 0b0a576..aa8ba67 100644 --- a/src/autorestic.ts +++ b/src/autorestic.ts @@ -3,6 +3,7 @@ import minimist from 'minimist' import { init } from './config' import handlers, { error, help } from './handlers' +import { Config } from './types' @@ -25,14 +26,19 @@ export const { _: commands, ...flags } = minimist(process.argv.slice(2), { string: ['l', 'b'], }) -export const VERSION = '0.16' +export const VERSION = '0.17' export const INSTALL_DIR = '/usr/local/bin' export const VERBOSE = flags.verbose -export const config = init() +export let config: Config async function main() { + config = await init() + + // For dev + // return await handlers['check']([], { ...flags, all: true }) + if (commands.length < 1 || commands[0] === 'help') return help() const command: string = commands[0] diff --git a/src/backend.ts b/src/backend.ts index 4f6bcb6..4b2e436 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -2,7 +2,7 @@ import { Writer } from 'clitastic' import { config, VERBOSE } from './autorestic' import { Backend, Backends, Locations } from './types' -import { exec, ConfigError, pathRelativeToConfigFile } from './utils' +import { exec, pathRelativeToConfigFile } from './utils' @@ -60,10 +60,8 @@ export const checkAndConfigureBackend = (name: string, backend: Backend) => { } export const checkAndConfigureBackends = (backends?: Backends) => { - if (!backends) { - if (!config) throw ConfigError + if (!backends) backends = config.backends - } console.log('\nConfiguring Backends'.grey.underline) for (const [name, backend] of Object.entries(backends)) diff --git a/src/backup.ts b/src/backup.ts index ff5b4e5..a29880e 100644 --- a/src/backup.ts +++ b/src/backup.ts @@ -7,7 +7,6 @@ import { LocationFromPrefixes } from './config' import { Locations, Location, Backend } from './types' import { exec, - ConfigError, pathRelativeToConfigFile, getFlagsFromLocation, makeArrayIfIsNot, @@ -54,7 +53,6 @@ export const backupFromVolume = (volume: string, location: Location, backend: Ba } export const backupSingle = (name: string, to: string, location: Location) => { - if (!config) throw ConfigError const delta = new MeasureDuration() const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳') @@ -104,10 +102,8 @@ export const backupLocation = (name: string, location: Location) => { } export const backupAll = (locations?: Locations) => { - if (!locations) { - if (!config) throw ConfigError + if (!locations) locations = config.locations - } console.log('\nBacking Up'.underline.grey) for (const [name, location] of Object.entries(locations)) diff --git a/src/config.ts b/src/config.ts index faebdb3..61dc7f6 100644 --- a/src/config.ts +++ b/src/config.ts @@ -39,16 +39,16 @@ export const normalizeAndCheckBackends = (config: Config) => { } } -export const normalizeAndCheckBackups = (config: Config) => { +export const normalizeAndCheckLocations = async (config: Config) => { config.locations = makeObjectKeysLowercase(config.locations) const backends = Object.keys(config.backends) - const checkDestination = (backend: string, backup: string) => { + const checkDestination = (backend: string, location: string) => { if (!backends.includes(backend)) - throw new Error(`Cannot find the backend "${backend}" for "${backup}"`) + throw new Error(`Cannot find the backend "${backend}" for "${location}"`) } - for (const [name, { from, to, ...rest }] of Object.entries( + for (const [name, { from, to, cron, ...rest }] of Object.entries( config.locations, )) { if (!from || !to) @@ -61,7 +61,7 @@ export const normalizeAndCheckBackups = (config: Config) => { } } -const findConfigFile = (): string | undefined => { +const findConfigFile = (): string => { const config = '.autorestic.yml' const paths = [ resolve(flags.config || ''), @@ -75,14 +75,14 @@ const findConfigFile = (): string | undefined => { } catch (e) { } } + throw new Error('Config file not found') } export let CONFIG_FILE: string = '' -export const init = (): Config | undefined => { +export const init = async (): Promise => { const file = findConfigFile() - if (file) CONFIG_FILE = file - else return + CONFIG_FILE = file const raw: Config = makeObjectKeysLowercase( yaml.safeLoad(readFileSync(CONFIG_FILE).toString()), @@ -91,7 +91,7 @@ export const init = (): Config | undefined => { const current = JSON.stringify(raw) normalizeAndCheckBackends(raw) - normalizeAndCheckBackups(raw) + await normalizeAndCheckLocations(raw) const changed = JSON.stringify(raw) !== current diff --git a/src/forget.ts b/src/forget.ts index 5dedd8b..b4d47cc 100644 --- a/src/forget.ts +++ b/src/forget.ts @@ -6,7 +6,6 @@ import { LocationFromPrefixes } from './config' import { Locations, Location, Flags } from './types' import { exec, - ConfigError, pathRelativeToConfigFile, getFlagsFromLocation, makeArrayIfIsNot, @@ -16,7 +15,6 @@ import { export const forgetSingle = (name: string, to: string, location: Location, dryRun: boolean) => { - if (!config) throw ConfigError const base = name + to.blue + ' : ' const writer = new Writer(base + 'Removing old snapshots… ⏳') @@ -64,7 +62,6 @@ export const forgetLocation = (name: string, backup: Location, dryRun: boolean) } export const forgetAll = (backups?: Locations, flags?: Flags) => { - if (!config) throw ConfigError if (!backups) { backups = config.locations } diff --git a/src/handlers.ts b/src/handlers.ts index 36e05a0..6fdd09e 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -18,7 +18,7 @@ import { downloadFile, exec, filterObjectByKey, - ConfigError, makeArrayIfIsNot, + makeArrayIfIsNot, } from './utils' @@ -28,7 +28,6 @@ export type Handlers = { } const parseBackend = (flags: Flags): Backends => { - if (!config) throw ConfigError if (!flags.all && !flags.backend) throw new Error( 'No backends specified.'.red + @@ -46,7 +45,6 @@ const parseBackend = (flags: Flags): Backends => { } const parseLocations = (flags: Flags): Locations => { - if (!config) throw ConfigError if (!flags.all && !flags.location) throw new Error( 'No locations specified.'.red + @@ -72,7 +70,6 @@ const handlers: Handlers = { checkAndConfigureBackends(backends) }, backup(args, flags) { - if (!config) throw ConfigError checkIfResticIsAvailable() const locations: Locations = parseLocations(flags) @@ -84,7 +81,6 @@ const handlers: Handlers = { console.log('\nFinished!'.underline + ' 🎉') }, restore(args, flags) { - if (!config) throw ConfigError checkIfResticIsAvailable() const locations = parseLocations(flags) @@ -95,7 +91,6 @@ const handlers: Handlers = { restoreSingle(keys[0], flags.from, flags.to) }, forget(args, flags) { - if (!config) throw ConfigError checkIfResticIsAvailable() const locations: Locations = parseLocations(flags) diff --git a/src/info.ts b/src/info.ts index 159729c..63fa25a 100644 --- a/src/info.ts +++ b/src/info.ts @@ -1,11 +1,9 @@ import { config } from './autorestic' -import { ConfigError, fill, treeToString } from './utils' +import { fill, treeToString } from './utils' const showAll = () => { - if (!config) throw ConfigError - console.log('\n\n' + fill(32, '_') + 'LOCATIONS:'.underline) for (const [key, data] of Object.entries(config.locations)) { console.log(`\n${key.blue.underline}:`) diff --git a/src/restore.ts b/src/restore.ts index dfaafb9..a776764 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -7,7 +7,6 @@ import { LocationFromPrefixes } from './config' import { Backend } from './types' import { checkIfDockerVolumeExistsOrFail, - ConfigError, decodeLocationFromPrefix, exec, execPlain, @@ -43,8 +42,6 @@ export const restoreToVolume = (volume: string, backend: Backend) => { } export const restoreSingle = (locationName: string, from: string, to?: string) => { - if (!config) throw ConfigError - const location = config.locations[locationName] const baseText = locationName.green + '\t\t' diff --git a/src/types.ts b/src/types.ts index c66a7fd..d7290e1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -71,6 +71,7 @@ export type Backends = { [name: string]: Backend } export type Location = { from: string to: StringOrArray + cron?: string hooks?: { before?: StringOrArray after?: StringOrArray diff --git a/src/utils.ts b/src/utils.ts index 9195e47..12ff36e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -103,8 +103,6 @@ export const resolveTildePath = (path: string): string | null => ? null : join(homedir(), path.slice(1)) -export const ConfigError = new Error('Config file not found') - export const getFlagsFromLocation = (location: Location, command?: string): string[] => { if (!location.options) return [] @@ -197,4 +195,3 @@ export const checkIfDockerVolumeExistsOrFail = (volume: string) => { if (cmd.err.length > 0) throw new Error('Volume not found') } -