config optional if not required for current operation

This commit is contained in:
cupcakearmy 2019-10-26 20:52:17 +02:00
parent 9dafe9d36a
commit de27034b94
7 changed files with 157 additions and 119 deletions

View File

@ -32,7 +32,7 @@ if (flags.version) {
process.exit(0)
}
export const config: Config = init()
export const config = init()
function main() {
if (commands.length < 1) return help()

View File

@ -2,11 +2,10 @@ import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic'
import { Backend, Backends } from './types'
import { exec } from './utils'
import { exec, ConfigError } from './utils'
const ALREADY_EXISTS = /(?=.*exists)(?=.*already)(?=.*config).*/
export const getPathFromBackend = (backend: Backend): string => {
switch (backend.type) {
case 'local':
@ -24,7 +23,6 @@ export const getPathFromBackend = (backend: Backend): string => {
}
}
export const getEnvFromBackend = (backend: Backend) => {
const { type, path, key, ...rest } = backend
return {
@ -34,7 +32,6 @@ export const getEnvFromBackend = (backend: Backend) => {
}
}
export const checkAndConfigureBackend = (name: string, backend: Backend) => {
const writer = new Writer(name.blue + ' : ' + 'Configuring... ⏳')
const env = getEnvFromBackend(backend)
@ -49,8 +46,12 @@ export const checkAndConfigureBackend = (name: string, backend: Backend) => {
writer.done(name.blue + ' : ' + 'Done ✓'.green)
}
export const checkAndConfigureBackends = (backends?: Backends) => {
if (!backends) {
if (!config) throw ConfigError
backends = config.backends
}
export const checkAndConfigureBackends = (backends: Backends = config.backends) => {
console.log('\nConfiguring Backends'.grey.underline)
for (const [name, backend] of Object.entries(backends))
checkAndConfigureBackend(name, backend)

View File

@ -3,11 +3,12 @@ import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend'
import { Locations, Location } from './types'
import { exec } from './utils'
import { exec, ConfigError } from './utils'
import { CONFIG_FILE } from './config'
import { resolve, dirname } from 'path'
export const backupSingle = (name: string, from: string, to: string) => {
if (!config) throw ConfigError
const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳')
const backend = config.backends[to]
const pathRelativeToConfigFile = resolve(dirname(CONFIG_FILE), from)
@ -34,7 +35,12 @@ export const backupLocation = (name: string, backup: Location) => {
} else backupSingle(display, backup.from, backup.to)
}
export const backupAll = (backups: Locations = config.locations) => {
export const backupAll = (backups?: Locations) => {
if (!backups) {
if (!config) throw ConfigError
backups = config.locations
}
console.log('\nBacking Up'.underline.grey)
for (const [name, backup] of Object.entries(backups))
backupLocation(name, backup)

View File

@ -69,8 +69,13 @@ const findConfigFile = (): string => {
export let CONFIG_FILE: string = ''
export const init = (): Config => {
export const init = (): Config | undefined => {
try {
CONFIG_FILE = findConfigFile()
} catch (e) {
return
}
const raw: Config = makeObjectKeysLowercase(
yaml.safeLoad(readFileSync(CONFIG_FILE).toString())
)

View File

@ -15,6 +15,7 @@ import {
exec,
filterObjectByKey,
singleToArray,
ConfigError,
} from './utils'
export type Handlers = {
@ -22,6 +23,7 @@ export type Handlers = {
}
const parseBackend = (flags: Flags): Backends => {
if (!config) throw ConfigError
if (!flags.all && !flags.backend)
throw new Error(
'No backends specified.'.red +
@ -39,6 +41,7 @@ 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 +
@ -64,6 +67,7 @@ const handlers: Handlers = {
checkAndConfigureBackends(backends)
},
backup(args, flags) {
if (!config) throw ConfigError
checkIfResticIsAvailable()
const locations: Locations = parseLocations(flags)
@ -79,6 +83,7 @@ const handlers: Handlers = {
console.log('\nFinished!'.underline + ' 🎉')
},
restore(args, flags) {
if (!config) throw ConfigError
checkIfResticIsAvailable()
const locations = parseLocations(flags)
for (const [name, location] of Object.entries(locations)) {

View File

@ -1,62 +1,69 @@
type BackendLocal = {
type: 'local',
key: string,
type: 'local'
key: string
path: string
}
type BackendSFTP = {
type: 'sftp',
key: string,
path: string,
password?: string,
type: 'sftp'
key: string
path: string
password?: string
}
type BackendREST = {
type: 'rest',
key: string,
path: string,
user?: string,
type: 'rest'
key: string
path: string
user?: string
password?: string
}
type BackendS3 = {
type: 's3',
key: string,
path: string,
aws_access_key_id: string,
aws_secret_access_key: string,
type: 's3'
key: string
path: string
aws_access_key_id: string
aws_secret_access_key: string
}
type BackendB2 = {
type: 'b2',
key: string,
path: string,
b2_account_id: string,
type: 'b2'
key: string
path: string
b2_account_id: string
b2_account_key: string
}
type BackendAzure = {
type: 'azure',
key: string,
path: string,
azure_account_name: string,
type: 'azure'
key: string
path: string
azure_account_name: string
azure_account_key: string
}
type BackendGS = {
type: 'gs',
key: string,
path: string,
google_project_id: string,
type: 'gs'
key: string
path: string
google_project_id: string
google_application_credentials: string
}
export type Backend = BackendAzure | BackendB2 | BackendGS | BackendLocal | BackendREST | BackendS3 | BackendSFTP
export type Backend =
| BackendAzure
| BackendB2
| BackendGS
| BackendLocal
| BackendREST
| BackendS3
| BackendSFTP
export type Backends = { [name: string]: Backend }
export type Location = {
from: string,
from: string
to: string | string[]
}

View File

@ -3,8 +3,11 @@ import { spawnSync, SpawnSyncOptions } from 'child_process'
import { randomBytes } from 'crypto'
import { createWriteStream } from 'fs'
export const exec = (command: string, args: string[], { env, ...rest }: SpawnSyncOptions = {}) => {
export const exec = (
command: string,
args: string[],
{ env, ...rest }: SpawnSyncOptions = {}
) => {
const cmd = spawnSync(command, args, {
...rest,
env: {
@ -19,9 +22,11 @@ export const exec = (command: string, args: string[], { env, ...rest }: SpawnSyn
return { out, err }
}
export const checkIfResticIsAvailable = () => checkIfCommandIsAvailable(
export const checkIfResticIsAvailable = () =>
checkIfCommandIsAvailable(
'restic',
'Restic is not installed'.red + ' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases',
'Restic is not installed'.red +
' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases'
)
export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
@ -31,22 +36,29 @@ export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
export const makeObjectKeysLowercase = (object: Object): any =>
Object.fromEntries(
Object.entries(object)
.map(([key, value]) => [key.toLowerCase(), value]),
Object.entries(object).map(([key, value]) => [key.toLowerCase(), value])
)
export function rand(length = 32): string {
return randomBytes(length / 2).toString('hex')
}
export const singleToArray = <T>(singleOrArray: T | T[]): T[] => Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
export const singleToArray = <T>(singleOrArray: T | T[]): T[] =>
Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
export const filterObject = <T>(obj: { [key: string]: T }, filter: (item: [string, T]) => boolean): { [key: string]: T } => Object.fromEntries(Object.entries(obj).filter(filter))
export const filterObject = <T>(
obj: { [key: string]: T },
filter: (item: [string, T]) => boolean
): { [key: string]: T } =>
Object.fromEntries(Object.entries(obj).filter(filter))
export const filterObjectByKey = <T>(obj: { [key: string]: T }, keys: string[]) => filterObject(obj, ([key]) => keys.includes(key))
export const filterObjectByKey = <T>(
obj: { [key: string]: T },
keys: string[]
) => filterObject(obj, ([key]) => keys.includes(key))
export const downloadFile = async (url: string, to: string) => new Promise<void>(async res => {
export const downloadFile = async (url: string, to: string) =>
new Promise<void>(async res => {
const { data: file } = await axios({
method: 'get',
url: url,
@ -61,3 +73,5 @@ export const downloadFile = async (url: string, to: string) => new Promise<void>
res()
})
})
export const ConfigError = new Error('Config file not found')