also check for default file in the current directory

pull/3/head
cupcakearmy 5 years ago
parent 3d1e28e574
commit 993fe072e2

@ -1,16 +1,21 @@
import { readFileSync, writeFileSync } from 'fs' import { readFileSync, writeFileSync, statSync } from 'fs'
import { resolve } from 'path'
import yaml from 'js-yaml' import yaml from 'js-yaml'
import { CONFIG_FILE } from './autorestic' import { flags } from './autorestic'
import { Backend, Config } from './types' import { Backend, Config } from './types'
import { makeObjectKeysLowercase, rand } from './utils' import { makeObjectKeysLowercase, rand } from './utils'
import { homedir } from 'os'
export const normalizeAndCheckBackends = (config: Config) => { export const normalizeAndCheckBackends = (config: Config) => {
config.backends = makeObjectKeysLowercase(config.backends) config.backends = makeObjectKeysLowercase(config.backends)
for (const [name, { type, path, key, ...rest }] of Object.entries(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`) )) {
if (!type || !path)
throw new Error(
`The backend "${name}" is missing some required attributes`
)
const tmp: any = { const tmp: any = {
type, type,
@ -24,7 +29,6 @@ export const normalizeAndCheckBackends = (config: Config) => {
} }
} }
export const normalizeAndCheckBackups = (config: Config) => { export const normalizeAndCheckBackups = (config: Config) => {
config.locations = makeObjectKeysLowercase(config.locations) config.locations = makeObjectKeysLowercase(config.locations)
const backends = Object.keys(config.backends) const backends = Object.keys(config.backends)
@ -34,20 +38,42 @@ export const normalizeAndCheckBackups = (config: Config) => {
throw new Error(`Cannot find the backend "${backend}" for "${backup}"`) throw new Error(`Cannot find the backend "${backend}" for "${backup}"`)
} }
for (const [name, { from, to, ...rest }] of Object.entries(config.locations)) { for (const [name, { from, to, ...rest }] of Object.entries(
if (!from || !to) throw new Error(`The backup "${name}" is missing some required attributes`) 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)
}
}
if (Array.isArray(to)) const findConfigFile = (): string => {
for (const t of to) const config = '.autorestic.yml'
checkDestination(t, name) const paths = [
else resolve(flags.config || ''),
checkDestination(to, name) 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 => { 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) normalizeAndCheckBackends(raw)
normalizeAndCheckBackups(raw) normalizeAndCheckBackups(raw)

Loading…
Cancel
Save