2019-10-26 18:07:36 +00:00
|
|
|
import { readFileSync, writeFileSync, statSync } from 'fs'
|
|
|
|
import { resolve } from 'path'
|
2019-06-20 21:09:47 +00:00
|
|
|
import yaml from 'js-yaml'
|
2019-10-26 18:07:36 +00:00
|
|
|
import { flags } from './autorestic'
|
2019-06-20 21:09:47 +00:00
|
|
|
import { Backend, Config } from './types'
|
|
|
|
import { makeObjectKeysLowercase, rand } from './utils'
|
2019-10-26 18:07:36 +00:00
|
|
|
import { homedir } from 'os'
|
2019-06-20 21:09:47 +00:00
|
|
|
|
|
|
|
export const normalizeAndCheckBackends = (config: Config) => {
|
2019-10-26 18:07:36 +00:00
|
|
|
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
|
|
|
|
}
|
2019-06-20 21:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const normalizeAndCheckBackups = (config: Config) => {
|
2019-10-26 18:07:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-26 19:03:08 +00:00
|
|
|
const findConfigFile = (): string | undefined => {
|
2019-10-26 18:07:36 +00:00
|
|
|
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) {}
|
|
|
|
}
|
2019-06-20 21:09:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 18:07:36 +00:00
|
|
|
export let CONFIG_FILE: string = ''
|
2019-06-20 21:09:47 +00:00
|
|
|
|
2019-10-26 18:52:17 +00:00
|
|
|
export const init = (): Config | undefined => {
|
2019-10-26 19:03:08 +00:00
|
|
|
const file = findConfigFile()
|
|
|
|
if (file) CONFIG_FILE = file
|
|
|
|
else return
|
2019-10-26 18:52:17 +00:00
|
|
|
|
2019-10-26 18:07:36 +00:00
|
|
|
const raw: Config = makeObjectKeysLowercase(
|
|
|
|
yaml.safeLoad(readFileSync(CONFIG_FILE).toString())
|
|
|
|
)
|
2019-06-20 21:09:47 +00:00
|
|
|
|
2019-10-26 18:07:36 +00:00
|
|
|
normalizeAndCheckBackends(raw)
|
|
|
|
normalizeAndCheckBackups(raw)
|
2019-06-20 21:09:47 +00:00
|
|
|
|
2019-10-26 18:07:36 +00:00
|
|
|
writeFileSync(CONFIG_FILE, yaml.safeDump(raw))
|
2019-06-20 21:09:47 +00:00
|
|
|
|
2019-10-26 18:07:36 +00:00
|
|
|
return raw
|
2019-06-20 21:09:47 +00:00
|
|
|
}
|