This commit is contained in:
cupcakearmy 2019-10-26 20:07:19 +02:00
parent 3c0ebdfb4a
commit 3d1e28e574

View File

@ -4,7 +4,7 @@ import { unlinkSync } from 'fs'
import { tmpdir } from 'os'
import { join, resolve } from 'path'
import { config, CONFIG_FILE, INSTALL_DIR, VERSION } from './autorestic'
import { config, INSTALL_DIR, VERSION } from './autorestic'
import { checkAndConfigureBackends, getEnvFromBackend } from './backend'
import { backupAll } from './backup'
import { Backends, Flags, Locations } from './types'
@ -17,16 +17,18 @@ import {
singleToArray,
} from './utils'
export type Handlers = { [command: string]: (args: string[], flags: Flags) => void }
export type Handlers = {
[command: string]: (args: string[], flags: Flags) => void
}
const parseBackend = (flags: Flags): Backends => {
if (!flags.all && !flags.backend)
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',
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'
)
if (flags.all)
return config.backends
if (flags.all) return config.backends
else {
const backends = singleToArray<string>(flags.backend)
for (const backend of backends)
@ -38,9 +40,10 @@ const parseBackend = (flags: Flags): Backends => {
const parseLocations = (flags: Flags): Locations => {
if (!flags.all && !flags.location)
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',
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'
)
if (flags.all) {
@ -68,7 +71,9 @@ const handlers: Handlers = {
for (const to of Object.values(locations).map(location => location.to))
Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to)
checkAndConfigureBackends(filterObjectByKey(config.backends, Array.from(backends)))
checkAndConfigureBackends(
filterObjectByKey(config.backends, Array.from(backends))
)
backupAll(locations)
console.log('\nFinished!'.underline + ' 🎉')
@ -78,12 +83,16 @@ const handlers: Handlers = {
const locations = parseLocations(flags)
for (const [name, location] of Object.entries(locations)) {
const w = new Writer(name.green + `\t\tRestoring... ⏳`)
const env = getEnvFromBackend(config.backends[Array.isArray(location.to) ? location.to[0] : location.to])
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 🎉')
}
@ -104,8 +113,7 @@ const handlers: Handlers = {
checkIfResticIsAvailable()
console.log('Restic is already installed')
return
} catch (e) {
}
} catch (e) {}
const w = new Writer('Checking latest version... ⏳')
checkIfCommandIsAvailable('bzip2')
@ -116,16 +124,19 @@ const handlers: Handlers = {
})
const archMap: { [a: string]: string } = {
'x32': '386',
'x64': 'amd64',
x32: '386',
x64: 'amd64',
}
w.replaceLn('Downloading binary... 🌎')
const name = `${json.name.replace(' ', '_')}_${process.platform}_${archMap[process.arch]}.bz2`
const name = `${json.name.replace(' ', '_')}_${process.platform}_${
archMap[process.arch]
}.bz2`
const dl = json.assets.find((asset: any) => asset.name === name)
if (!dl) return console.log(
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)
@ -143,7 +154,9 @@ const handlers: Handlers = {
exec('chmod', ['+x', extracted])
exec('mv', [extracted, INSTALL_DIR + '/restic'])
w.done(`\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉')
w.done(
`\nFinished! restic is installed under: ${INSTALL_DIR}`.underline + ' 🎉'
)
},
uninstall() {
for (const bin of ['restic', 'autorestic'])
@ -159,20 +172,21 @@ const handlers: Handlers = {
const w = new Writer('Checking for latest restic version... ⏳')
exec('restic', ['self-update'])
w.replaceLn('Checking for latest autorestic version... ⏳')
const { data: json } = await axios({
method: 'get',
url: 'https://api.github.com/repos/cupcakearmy/autorestic/releases/latest',
url:
'https://api.github.com/repos/cupcakearmy/autorestic/releases/latest',
responseType: 'json',
})
if (json.tag_name != VERSION) {
const platformMap: { [key: string]: string } = {
'darwin': 'macos',
darwin: 'macos',
}
const name = `autorestic_${platformMap[process.platform] || process.platform}_${process.arch}`
const name = `autorestic_${platformMap[process.platform] ||
process.platform}_${process.arch}`
const dl = json.assets.find((asset: any) => asset.name === name)
const to = INSTALL_DIR + '/autorestic'
@ -187,30 +201,36 @@ const handlers: Handlers = {
}
export const help = () => {
console.log('\nAutorestic'.blue + ` - ${VERSION} - Easy Restic CLI Utility`
+ '\n'
+ '\nOptions:'.yellow
+ `\n -c, --config Specify config file. Default: ${CONFIG_FILE}`
+ '\n'
+ '\nCommands:'.yellow
+ '\n check [-b, --backend] [-a, --all] Check backends'
+ '\n backup [-l, --location] [-a, --all] Backup all or specified locations'
+ '\n restore [-l, --location] [-- --target <out dir>] Check backends'
+ '\n'
+ '\n exec [-b, --backend] [-a, --all] <command> -- [native options] Execute native restic command'
+ '\n'
+ '\n install install restic'
+ '\n uninstall uninstall restic'
+ '\n update update restic'
+ '\n help Show help'
+ '\n'
+ '\nExamples: '.yellow + 'https://git.io/fjVbg'
+ '\n',
console.log(
'\nAutorestic'.blue +
` - ${VERSION} - Easy Restic CLI Utility` +
'\n' +
'\nOptions:'.yellow +
`\n -c, --config Specify config file. Default: .autorestic.yml` +
'\n' +
'\nCommands:'.yellow +
'\n check [-b, --backend] [-a, --all] Check backends' +
'\n backup [-l, --location] [-a, --all] Backup all or specified locations' +
'\n restore [-l, --location] [-- --target <out dir>] Restore all or specified locations' +
'\n' +
'\n exec [-b, --backend] [-a, --all] <command> -- [native options] Execute native restic command' +
'\n' +
'\n install install restic' +
'\n uninstall uninstall restic' +
'\n update update restic' +
'\n help Show help' +
'\n' +
'\nExamples: '.yellow +
'https://git.io/fjVbg' +
'\n'
)
}
export const error = () => {
help()
console.log(`Invalid Command:`.red.underline, `${process.argv.slice(2).join(' ')}`)
console.log(
`Invalid Command:`.red.underline,
`${process.argv.slice(2).join(' ')}`
)
}
export default handlers