formatting & trailing commas

This commit is contained in:
cupcakearmy 2019-12-03 23:37:55 +01:00
parent b68dc75053
commit 352754dad9
8 changed files with 500 additions and 481 deletions

View File

@ -5,6 +5,8 @@ import { init } from './config'
import handlers, { error, help } from './handlers' import handlers, { error, help } from './handlers'
import { Config } from './types' import { Config } from './types'
process.on('uncaughtException', err => { process.on('uncaughtException', err => {
console.log(err.message) console.log(err.message)
process.exit(1) process.exit(1)
@ -30,6 +32,7 @@ export const VERBOSE = flags.verbose
export const config = init() export const config = init()
function main() { function main() {
if (commands.length < 1) return help() if (commands.length < 1) return help()
@ -38,4 +41,5 @@ function main() {
;(handlers[command] || error)(args, flags) ;(handlers[command] || error)(args, flags)
} }
main() main()

View File

@ -4,6 +4,8 @@ import { config, VERBOSE } from './autorestic'
import { Backend, Backends } from './types' import { Backend, Backends } from './types'
import { exec, ConfigError } from './utils' import { exec, ConfigError } from './utils'
const ALREADY_EXISTS = /(?=.*already)(?=.*config).*/ const ALREADY_EXISTS = /(?=.*already)(?=.*config).*/
export const getPathFromBackend = (backend: Backend): string => { export const getPathFromBackend = (backend: Backend): string => {

View File

@ -5,6 +5,8 @@ import { getEnvFromBackend } from './backend'
import { Locations, Location } from './types' import { Locations, Location } from './types'
import { exec, ConfigError, pathRelativeToConfigFile } from './utils' import { exec, ConfigError, pathRelativeToConfigFile } from './utils'
export const backupSingle = (name: string, from: string, to: string) => { export const backupSingle = (name: string, from: string, to: string) => {
if (!config) throw ConfigError if (!config) throw ConfigError
const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳') const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳')

View File

@ -6,15 +6,17 @@ import { Backend, Config } from './types'
import { makeObjectKeysLowercase, rand } from './utils' import { makeObjectKeysLowercase, rand } from './utils'
import { homedir } from 'os' 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( for (const [name, { type, path, key, ...rest }] of Object.entries(
config.backends config.backends,
)) { )) {
if (!type || !path) if (!type || !path)
throw new Error( throw new Error(
`The backend "${name}" is missing some required attributes` `The backend "${name}" is missing some required attributes`,
) )
const tmp: any = { const tmp: any = {
@ -39,11 +41,11 @@ export const normalizeAndCheckBackups = (config: Config) => {
} }
for (const [name, { from, to, ...rest }] of Object.entries( for (const [name, { from, to, ...rest }] of Object.entries(
config.locations config.locations,
)) { )) {
if (!from || !to) if (!from || !to)
throw new Error( throw new Error(
`The backup "${name}" is missing some required attributes` `The backup "${name}" is missing some required attributes`,
) )
if (Array.isArray(to)) for (const t of to) checkDestination(t, name) if (Array.isArray(to)) for (const t of to) checkDestination(t, name)
@ -62,7 +64,8 @@ const findConfigFile = (): string | undefined => {
try { try {
const file = statSync(path) const file = statSync(path)
if (file.isFile()) return path if (file.isFile()) return path
} catch (e) {} } catch (e) {
}
} }
} }
@ -74,7 +77,7 @@ export const init = (): Config | undefined => {
else return else return
const raw: Config = makeObjectKeysLowercase( const raw: Config = makeObjectKeysLowercase(
yaml.safeLoad(readFileSync(CONFIG_FILE).toString()) yaml.safeLoad(readFileSync(CONFIG_FILE).toString()),
) )
normalizeAndCheckBackends(raw) normalizeAndCheckBackends(raw)

View File

@ -5,6 +5,8 @@ import { getEnvFromBackend } from './backend'
import { Locations, Location, ForgetPolicy, Flags } from './types' import { Locations, Location, ForgetPolicy, Flags } from './types'
import { exec, ConfigError } from './utils' import { exec, ConfigError } from './utils'
export const forgetSingle = (dryRun: boolean, name: string, from: string, to: string, policy: ForgetPolicy) => { export const forgetSingle = (dryRun: boolean, name: string, from: string, to: string, policy: ForgetPolicy) => {
if (!config) throw ConfigError if (!config) throw ConfigError
const writer = new Writer(name + to.blue + ' : ' + 'Removing old spnapshots… ⏳') const writer = new Writer(name + to.blue + ' : ' + 'Removing old spnapshots… ⏳')
@ -29,8 +31,7 @@ export const forgetLocation = (dryRun: boolean, name: string, backup: Location,
const display = name.yellow + ' ▶ ' const display = name.yellow + ' ▶ '
if (!policy) { if (!policy) {
console.log(display + 'skipping, no policy declared') console.log(display + 'skipping, no policy declared')
} } else {
else {
if (Array.isArray(backup.to)) { if (Array.isArray(backup.to)) {
let first = true let first = true
for (const t of backup.to) { for (const t of backup.to) {

View File

@ -19,6 +19,8 @@ import {
ConfigError, ConfigError,
} from './utils' } from './utils'
export type Handlers = { export type Handlers = {
[command: string]: (args: string[], flags: Flags) => void [command: string]: (args: string[], flags: Flags) => void
} }
@ -29,7 +31,7 @@ const parseBackend = (flags: Flags): Backends => {
throw new Error( throw new Error(
'No backends specified.'.red + 'No backends specified.'.red +
'\n--all [-a]\t\t\t\tCheck all.' + '\n--all [-a]\t\t\t\tCheck all.' +
'\n--backend [-b] myBackend\t\tSpecify one or more backend' '\n--backend [-b] myBackend\t\tSpecify one or more backend',
) )
if (flags.all) return config.backends if (flags.all) return config.backends
else { else {
@ -47,7 +49,7 @@ const parseLocations = (flags: Flags): Locations => {
throw new Error( throw new Error(
'No locations specified.'.red + 'No locations specified.'.red +
'\n--all [-a]\t\t\t\tBackup all.' + '\n--all [-a]\t\t\t\tBackup all.' +
'\n--location [-l] site1\t\t\tSpecify one or more locations' '\n--location [-l] site1\t\t\tSpecify one or more locations',
) )
if (flags.all) { if (flags.all) {
@ -77,7 +79,7 @@ const handlers: Handlers = {
Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to) Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to)
checkAndConfigureBackends( checkAndConfigureBackends(
filterObjectByKey(config.backends, Array.from(backends)) filterObjectByKey(config.backends, Array.from(backends)),
) )
backupAll(locations) backupAll(locations)
@ -92,13 +94,13 @@ const handlers: Handlers = {
const env = getEnvFromBackend( const env = getEnvFromBackend(
config.backends[ config.backends[
Array.isArray(location.to) ? location.to[0] : location.to Array.isArray(location.to) ? location.to[0] : location.to
] ],
) )
exec( exec(
'restic', 'restic',
['restore', 'latest', '--path', resolve(location.from), ...args], ['restore', 'latest', '--path', resolve(location.from), ...args],
{ env } { env },
) )
w.done(name.green + '\t\tDone 🎉') w.done(name.green + '\t\tDone 🎉')
} }
@ -113,7 +115,7 @@ const handlers: Handlers = {
Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to) Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to)
checkAndConfigureBackends( checkAndConfigureBackends(
filterObjectByKey(config.backends, Array.from(backends)) filterObjectByKey(config.backends, Array.from(backends)),
) )
forgetAll(flags['dry-run'], locations) forgetAll(flags['dry-run'], locations)
@ -135,7 +137,8 @@ const handlers: Handlers = {
checkIfResticIsAvailable() checkIfResticIsAvailable()
console.log('Restic is already installed') console.log('Restic is already installed')
return return
} catch (e) {} } catch (e) {
}
const w = new Writer('Checking latest version... ⏳') const w = new Writer('Checking latest version... ⏳')
checkIfCommandIsAvailable('bzip2') checkIfCommandIsAvailable('bzip2')
@ -158,7 +161,7 @@ const handlers: Handlers = {
if (!dl) if (!dl)
return console.log( return console.log(
'Cannot get the right binary.'.red, 'Cannot get the right binary.'.red,
'Please see https://bit.ly/2Y1Rzai' 'Please see https://bit.ly/2Y1Rzai',
) )
const tmp = join(tmpdir(), name) const tmp = join(tmpdir(), name)
@ -177,7 +180,7 @@ const handlers: Handlers = {
exec('mv', [extracted, INSTALL_DIR + '/restic']) exec('mv', [extracted, INSTALL_DIR + '/restic'])
w.done( w.done(
`\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉' `\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉',
) )
}, },
uninstall() { uninstall() {
@ -248,14 +251,14 @@ export const help = () => {
'\n' + '\n' +
'\nExamples: '.yellow + '\nExamples: '.yellow +
'https://git.io/fjVbg' + 'https://git.io/fjVbg' +
'\n' '\n',
) )
} }
export const error = () => { export const error = () => {
help() help()
console.log( console.log(
`Invalid Command:`.red.underline, `Invalid Command:`.red.underline,
`${process.argv.slice(2).join(' ')}` `${process.argv.slice(2).join(' ')}`,
) )
} }

View File

@ -5,10 +5,12 @@ import { createWriteStream } from 'fs'
import { isAbsolute, resolve, dirname } from 'path' import { isAbsolute, resolve, dirname } from 'path'
import { CONFIG_FILE } from './config' import { CONFIG_FILE } from './config'
export const exec = ( export const exec = (
command: string, command: string,
args: string[], args: string[],
{ env, ...rest }: SpawnSyncOptions = {} { env, ...rest }: SpawnSyncOptions = {},
) => { ) => {
const cmd = spawnSync(command, args, { const cmd = spawnSync(command, args, {
...rest, ...rest,
@ -28,7 +30,7 @@ export const checkIfResticIsAvailable = () =>
checkIfCommandIsAvailable( checkIfCommandIsAvailable(
'restic', 'restic',
'Restic is not installed'.red + 'Restic is not installed'.red +
' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases' ' https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases',
) )
export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => { export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
@ -38,25 +40,27 @@ export const checkIfCommandIsAvailable = (cmd: string, errorMsg?: string) => {
export const makeObjectKeysLowercase = (object: Object): any => export const makeObjectKeysLowercase = (object: Object): any =>
Object.fromEntries( 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 { export function rand(length = 32): string {
return randomBytes(length / 2).toString('hex') return randomBytes(length / 2).toString('hex')
} }
export const singleToArray = <T>(singleOrArray: T | T[]): T[] => export const singleToArray = <T>(singleOrArray: T | T[]): T[] =>
Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray] Array.isArray(singleOrArray) ? singleOrArray : [singleOrArray]
export const filterObject = <T>( export const filterObject = <T>(
obj: { [key: string]: T }, obj: { [key: string]: T },
filter: (item: [string, T]) => boolean filter: (item: [string, T]) => boolean,
): { [key: string]: T } => ): { [key: string]: T } =>
Object.fromEntries(Object.entries(obj).filter(filter)) Object.fromEntries(Object.entries(obj).filter(filter))
export const filterObjectByKey = <T>( export const filterObjectByKey = <T>(
obj: { [key: string]: T }, obj: { [key: string]: T },
keys: string[] keys: string[],
) => filterObject(obj, ([key]) => keys.includes(key)) ) => filterObject(obj, ([key]) => keys.includes(key))
export const downloadFile = async (url: string, to: string) => export const downloadFile = async (url: string, to: string) =>