From 993fe072e239d605e6914697d0c219dde3b93d19 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sat, 26 Oct 2019 20:07:36 +0200 Subject: [PATCH] also check for default file in the current directory --- src/config.ts | 110 +++++++++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 42 deletions(-) diff --git a/src/config.ts b/src/config.ts index 4901a4a..69f9a5b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,58 +1,84 @@ -import { readFileSync, writeFileSync } from 'fs' +import { readFileSync, writeFileSync, statSync } from 'fs' +import { resolve } from 'path' import yaml from 'js-yaml' -import { CONFIG_FILE } from './autorestic' +import { flags } from './autorestic' import { Backend, Config } from './types' import { makeObjectKeysLowercase, rand } from './utils' - +import { homedir } from 'os' export const normalizeAndCheckBackends = (config: Config) => { - config.backends = makeObjectKeysLowercase(config.backends) - - for (const [name, { type, path, key, ...rest }] of Object.entries(config.backends)) { - - if (!type || !path) throw new Error(`The backend "${name}" is missing some required attributes`) - - const tmp: any = { - type, - path, - key: key || rand(128), - } - for (const [key, value] of Object.entries(rest)) - tmp[key.toUpperCase()] = value - - config.backends[name] = tmp as Backend - } + config.backends = makeObjectKeysLowercase(config.backends) + + for (const [name, { type, path, key, ...rest }] of Object.entries( + config.backends + )) { + if (!type || !path) + throw new Error( + `The backend "${name}" is missing some required attributes` + ) + + const tmp: any = { + type, + path, + key: key || rand(128), + } + for (const [key, value] of Object.entries(rest)) + tmp[key.toUpperCase()] = value + + config.backends[name] = tmp as Backend + } } - export const normalizeAndCheckBackups = (config: Config) => { - config.locations = makeObjectKeysLowercase(config.locations) - const backends = Object.keys(config.backends) - - const checkDestination = (backend: string, backup: string) => { - if (!backends.includes(backend)) - throw new Error(`Cannot find the backend "${backend}" for "${backup}"`) - } - - for (const [name, { from, to, ...rest }] of Object.entries(config.locations)) { - if (!from || !to) throw new Error(`The backup "${name}" is missing some required attributes`) - - if (Array.isArray(to)) - for (const t of to) - checkDestination(t, name) - else - checkDestination(to, name) - } + config.locations = makeObjectKeysLowercase(config.locations) + const backends = Object.keys(config.backends) + + const checkDestination = (backend: string, backup: string) => { + if (!backends.includes(backend)) + throw new Error(`Cannot find the backend "${backend}" for "${backup}"`) + } + + for (const [name, { from, to, ...rest }] of Object.entries( + config.locations + )) { + if (!from || !to) + throw new Error( + `The backup "${name}" is missing some required attributes` + ) + + if (Array.isArray(to)) for (const t of to) checkDestination(t, name) + else checkDestination(to, name) + } +} + +const findConfigFile = (): string => { + const config = '.autorestic.yml' + const paths = [ + resolve(flags.config || ''), + resolve('./' + config), + homedir() + '/' + config, + ] + for (const path of paths) { + try { + const file = statSync(path) + if (file.isFile()) return path + } catch (e) {} + } + throw new Error('No Config file found') } +export let CONFIG_FILE: string = '' export const init = (): Config => { - const raw: Config = makeObjectKeysLowercase(yaml.safeLoad(readFileSync(CONFIG_FILE).toString())) + CONFIG_FILE = findConfigFile() + const raw: Config = makeObjectKeysLowercase( + yaml.safeLoad(readFileSync(CONFIG_FILE).toString()) + ) - normalizeAndCheckBackends(raw) - normalizeAndCheckBackups(raw) + normalizeAndCheckBackends(raw) + normalizeAndCheckBackups(raw) - writeFileSync(CONFIG_FILE, yaml.safeDump(raw)) + writeFileSync(CONFIG_FILE, yaml.safeDump(raw)) - return raw + return raw }