mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2025-01-22 14:56:24 +00:00
formatting & trailing commas
This commit is contained in:
parent
b68dc75053
commit
352754dad9
@ -5,6 +5,8 @@ import { init } from './config'
|
||||
import handlers, { error, help } from './handlers'
|
||||
import { Config } from './types'
|
||||
|
||||
|
||||
|
||||
process.on('uncaughtException', err => {
|
||||
console.log(err.message)
|
||||
process.exit(1)
|
||||
@ -30,6 +32,7 @@ export const VERBOSE = flags.verbose
|
||||
|
||||
export const config = init()
|
||||
|
||||
|
||||
function main() {
|
||||
if (commands.length < 1) return help()
|
||||
|
||||
@ -38,4 +41,5 @@ function main() {
|
||||
;(handlers[command] || error)(args, flags)
|
||||
}
|
||||
|
||||
|
||||
main()
|
||||
|
@ -4,6 +4,8 @@ import { config, VERBOSE } from './autorestic'
|
||||
import { Backend, Backends } from './types'
|
||||
import { exec, ConfigError } from './utils'
|
||||
|
||||
|
||||
|
||||
const ALREADY_EXISTS = /(?=.*already)(?=.*config).*/
|
||||
|
||||
export const getPathFromBackend = (backend: Backend): string => {
|
||||
|
@ -5,6 +5,8 @@ import { getEnvFromBackend } from './backend'
|
||||
import { Locations, Location } from './types'
|
||||
import { exec, ConfigError, pathRelativeToConfigFile } from './utils'
|
||||
|
||||
|
||||
|
||||
export const backupSingle = (name: string, from: string, to: string) => {
|
||||
if (!config) throw ConfigError
|
||||
const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳')
|
||||
|
@ -6,15 +6,17 @@ 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
|
||||
config.backends,
|
||||
)) {
|
||||
if (!type || !path)
|
||||
throw new Error(
|
||||
`The backend "${name}" is missing some required attributes`
|
||||
`The backend "${name}" is missing some required attributes`,
|
||||
)
|
||||
|
||||
const tmp: any = {
|
||||
@ -39,11 +41,11 @@ export const normalizeAndCheckBackups = (config: Config) => {
|
||||
}
|
||||
|
||||
for (const [name, { from, to, ...rest }] of Object.entries(
|
||||
config.locations
|
||||
config.locations,
|
||||
)) {
|
||||
if (!from || !to)
|
||||
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)
|
||||
@ -62,7 +64,8 @@ const findConfigFile = (): string | undefined => {
|
||||
try {
|
||||
const file = statSync(path)
|
||||
if (file.isFile()) return path
|
||||
} catch (e) {}
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -74,7 +77,7 @@ export const init = (): Config | undefined => {
|
||||
else return
|
||||
|
||||
const raw: Config = makeObjectKeysLowercase(
|
||||
yaml.safeLoad(readFileSync(CONFIG_FILE).toString())
|
||||
yaml.safeLoad(readFileSync(CONFIG_FILE).toString()),
|
||||
)
|
||||
|
||||
normalizeAndCheckBackends(raw)
|
||||
|
@ -5,6 +5,8 @@ import { getEnvFromBackend } from './backend'
|
||||
import { Locations, Location, ForgetPolicy, Flags } from './types'
|
||||
import { exec, ConfigError } from './utils'
|
||||
|
||||
|
||||
|
||||
export const forgetSingle = (dryRun: boolean, name: string, from: string, to: string, policy: ForgetPolicy) => {
|
||||
if (!config) throw ConfigError
|
||||
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 + ' ▶ '
|
||||
if (!policy) {
|
||||
console.log(display + 'skipping, no policy declared')
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (Array.isArray(backup.to)) {
|
||||
let first = true
|
||||
for (const t of backup.to) {
|
||||
|
@ -19,6 +19,8 @@ import {
|
||||
ConfigError,
|
||||
} from './utils'
|
||||
|
||||
|
||||
|
||||
export type Handlers = {
|
||||
[command: string]: (args: string[], flags: Flags) => void
|
||||
}
|
||||
@ -29,7 +31,7 @@ const parseBackend = (flags: Flags): Backends => {
|
||||
throw new Error(
|
||||
'No backends specified.'.red +
|
||||
'\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
|
||||
else {
|
||||
@ -47,7 +49,7 @@ const parseLocations = (flags: Flags): Locations => {
|
||||
throw new Error(
|
||||
'No locations specified.'.red +
|
||||
'\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) {
|
||||
@ -77,7 +79,7 @@ const handlers: Handlers = {
|
||||
Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to)
|
||||
|
||||
checkAndConfigureBackends(
|
||||
filterObjectByKey(config.backends, Array.from(backends))
|
||||
filterObjectByKey(config.backends, Array.from(backends)),
|
||||
)
|
||||
backupAll(locations)
|
||||
|
||||
@ -92,13 +94,13 @@ const handlers: Handlers = {
|
||||
const env = getEnvFromBackend(
|
||||
config.backends[
|
||||
Array.isArray(location.to) ? location.to[0] : location.to
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
exec(
|
||||
'restic',
|
||||
['restore', 'latest', '--path', resolve(location.from), ...args],
|
||||
{ env }
|
||||
{ env },
|
||||
)
|
||||
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)
|
||||
|
||||
checkAndConfigureBackends(
|
||||
filterObjectByKey(config.backends, Array.from(backends))
|
||||
filterObjectByKey(config.backends, Array.from(backends)),
|
||||
)
|
||||
forgetAll(flags['dry-run'], locations)
|
||||
|
||||
@ -135,7 +137,8 @@ const handlers: Handlers = {
|
||||
checkIfResticIsAvailable()
|
||||
console.log('Restic is already installed')
|
||||
return
|
||||
} catch (e) {}
|
||||
} catch (e) {
|
||||
}
|
||||
|
||||
const w = new Writer('Checking latest version... ⏳')
|
||||
checkIfCommandIsAvailable('bzip2')
|
||||
@ -158,7 +161,7 @@ const handlers: Handlers = {
|
||||
if (!dl)
|
||||
return console.log(
|
||||
'Cannot get the right binary.'.red,
|
||||
'Please see https://bit.ly/2Y1Rzai'
|
||||
'Please see https://bit.ly/2Y1Rzai',
|
||||
)
|
||||
|
||||
const tmp = join(tmpdir(), name)
|
||||
@ -177,7 +180,7 @@ const handlers: Handlers = {
|
||||
exec('mv', [extracted, INSTALL_DIR + '/restic'])
|
||||
|
||||
w.done(
|
||||
`\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉'
|
||||
`\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉',
|
||||
)
|
||||
},
|
||||
uninstall() {
|
||||
@ -248,14 +251,14 @@ export const help = () => {
|
||||
'\n' +
|
||||
'\nExamples: '.yellow +
|
||||
'https://git.io/fjVbg' +
|
||||
'\n'
|
||||
'\n',
|
||||
)
|
||||
}
|
||||
export const error = () => {
|
||||
help()
|
||||
console.log(
|
||||
`Invalid Command:`.red.underline,
|
||||
`${process.argv.slice(2).join(' ')}`
|
||||
`${process.argv.slice(2).join(' ')}`,
|
||||
)
|
||||
}
|
||||
|
||||
|
14
src/utils.ts
14
src/utils.ts
@ -5,10 +5,12 @@ import { createWriteStream } from 'fs'
|
||||
import { isAbsolute, resolve, dirname } from 'path'
|
||||
import { CONFIG_FILE } from './config'
|
||||
|
||||
|
||||
|
||||
export const exec = (
|
||||
command: string,
|
||||
args: string[],
|
||||
{ env, ...rest }: SpawnSyncOptions = {}
|
||||
{ env, ...rest }: SpawnSyncOptions = {},
|
||||
) => {
|
||||
const cmd = spawnSync(command, args, {
|
||||
...rest,
|
||||
@ -28,7 +30,7 @@ export const checkIfResticIsAvailable = () =>
|
||||
checkIfCommandIsAvailable(
|
||||
'restic',
|
||||
'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) => {
|
||||
@ -38,25 +40,27 @@ 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 filterObject = <T>(
|
||||
obj: { [key: string]: T },
|
||||
filter: (item: [string, T]) => boolean
|
||||
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[]
|
||||
keys: string[],
|
||||
) => filterObject(obj, ([key]) => keys.includes(key))
|
||||
|
||||
export const downloadFile = async (url: string, to: string) =>
|
||||
|
Loading…
Reference in New Issue
Block a user