auto update function

This commit is contained in:
cupcakearmy 2019-06-21 12:27:39 +02:00
parent 24a364ce08
commit b1f2678dc1
2 changed files with 58 additions and 37 deletions

View File

@ -1,5 +1,6 @@
import 'colors' import 'colors'
import minimist from 'minimist' import minimist from 'minimist'
import { homedir } from 'os'
import { resolve } from 'path' import { resolve } from 'path'
import { init } from './config' import { init } from './config'
@ -26,9 +27,9 @@ export const { _: commands, ...flags } = minimist(process.argv.slice(2), {
}) })
export const VERSION = '0.1' export const VERSION = '0.1'
export const DEFAULT_CONFIG = '~/.autorestic.yml' export const DEFAULT_CONFIG = '/.autorestic.yml'
export const INSTALL_DIR = '/usr/local/bin' export const INSTALL_DIR = '/usr/local/bin'
export const CONFIG_FILE: string = resolve(flags.config || DEFAULT_CONFIG) export const CONFIG_FILE: string = resolve(flags.config || homedir() + DEFAULT_CONFIG)
export const VERBOSE = flags.verbose export const VERBOSE = flags.verbose
export const config: Config = init() export const config: Config = init()

View File

@ -1,14 +1,21 @@
import axios from 'axios' import axios from 'axios'
import { Writer } from 'clitastic' import { Writer } from 'clitastic'
import { createWriteStream, unlinkSync } from 'fs' import { unlinkSync } from 'fs'
import { arch, platform, tmpdir } from 'os' import { tmpdir } from 'os'
import { join, resolve } from 'path' import { join, resolve } from 'path'
import { config, INSTALL_DIR, CONFIG_FILE } from './autorestic' import { config, CONFIG_FILE, INSTALL_DIR, VERSION } from './autorestic'
import { checkAndConfigureBackends, getEnvFromBackend } from './backend' import { checkAndConfigureBackends, getEnvFromBackend } from './backend'
import { backupAll } from './backup' import { backupAll } from './backup'
import { Backends, Flags, Locations } from './types' import { Backends, Flags, Locations } from './types'
import { checkIfCommandIsAvailable, checkIfResticIsAvailable, exec, filterObjectByKey, singleToArray } from './utils' import {
checkIfCommandIsAvailable,
checkIfResticIsAvailable,
downloadFile,
exec,
filterObjectByKey,
singleToArray,
} from './utils'
export type Handlers = { [command: string]: (args: string[], flags: Flags) => void } export type Handlers = { [command: string]: (args: string[], flags: Flags) => void }
@ -114,60 +121,73 @@ const handlers: Handlers = {
} }
w.replaceLn('Downloading binary... 🌎') w.replaceLn('Downloading binary... 🌎')
const name = `${json.name.replace(' ', '_')}_${platform()}_${archMap[arch()]}.bz2` const name = `${json.name.replace(' ', '_')}_${process.platform}_${archMap[process.arch]}.bz2`
const dl = json.assets.find((asset: any) => asset.name === name) 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, 'Cannot get the right binary.'.red,
'Please see https://bit.ly/2Y1Rzai', 'Please see https://bit.ly/2Y1Rzai',
) )
const { data: file } = await axios({ const tmp = join(tmpdir(), name)
method: 'get', const extracted = tmp.slice(0, -4) //without the .bz2
url: dl.browser_download_url,
responseType: 'stream',
})
const from = join(tmpdir(), name) await downloadFile(dl.browser_download_url, tmp)
const to = from.slice(0, -4)
w.replaceLn('Decompressing binary... 📦')
const stream = createWriteStream(from)
await new Promise(res => {
const writer = file.pipe(stream)
writer.on('close', res)
})
stream.close()
w.replaceLn(`Moving to ${INSTALL_DIR} 🚙`)
// TODO: Native bz2 // TODO: Native bz2
// Decompress // Decompress
exec('bzip2', ['-dk', from]) w.replaceLn('Decompressing binary... 📦')
// Remove .bz2 exec('bzip2', ['-dk', tmp])
exec('chmod', ['+x', to]) unlinkSync(tmp)
exec('mv', [to, INSTALL_DIR + '/restic'])
unlinkSync(from) w.replaceLn(`Moving to ${INSTALL_DIR} 🚙`)
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() { uninstall() {
try { for (const bin of ['restic', 'autorestic'])
unlinkSync(INSTALL_DIR + '/restic') try {
console.log(`Finished! restic was uninstalled`) unlinkSync(INSTALL_DIR + '/' + bin)
} catch (e) { console.log(`Finished! ${bin} was uninstalled`)
console.log('restic is already uninstalled'.red) } catch (e) {
} console.log(`${bin} is already uninstalled`.red)
}
}, },
update() { async update() {
checkIfResticIsAvailable() checkIfResticIsAvailable()
const w = new Writer('Checking for new restic version... ⏳') const w = new Writer('Checking for latest restic version... ⏳')
exec('restic', ['self-update']) 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',
responseType: 'json',
})
if (json.tag_name != VERSION) {
const platformMap: { [key: string]: string } = {
'darwin': 'macos',
}
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'
w.replaceLn('Downloading binary... 🌎')
await downloadFile(dl.browser_download_url, to)
exec('chmod', ['+x', to])
}
w.done('All up to date! 🚀') w.done('All up to date! 🚀')
}, },
} }
export const help = () => { export const help = () => {
console.log('\nAutorestic'.blue + ' - Easy Restic CLI Utility' console.log('\nAutorestic'.blue + ` - ${VERSION} - Easy Restic CLI Utility`
+ '\n' + '\n'
+ '\nOptions:'.yellow + '\nOptions:'.yellow
+ `\n -c, --config Specify config file. Default: ${CONFIG_FILE}` + `\n -c, --config Specify config file. Default: ${CONFIG_FILE}`