|
| 1 | +// npm packages |
| 2 | +const got = require('got'); |
| 3 | +const chalk = require('chalk'); |
| 4 | +const prettyBytes = require('pretty-bytes'); |
| 5 | + |
| 6 | +// our packages |
| 7 | +const {userConfig, isLoggedIn, logout} = require('../config'); |
| 8 | + |
| 9 | +exports.command = ['system [cmd]']; |
| 10 | +exports.describe = 'execute system commands (prune to remove unused data)'; |
| 11 | +exports.builder = { |
| 12 | + cmd: { |
| 13 | + default: '', |
| 14 | + description: 'command to execute [prune]', |
| 15 | + }, |
| 16 | +}; |
| 17 | +exports.handler = async args => { |
| 18 | + if (!isLoggedIn()) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // get command |
| 23 | + const {cmd} = args; |
| 24 | + |
| 25 | + if (cmd !== 'prune') { |
| 26 | + console.log('Only "prune" command is currently supported!'); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + // services request url |
| 31 | + const remoteUrl = `${userConfig.endpoint}/system/prune`; |
| 32 | + |
| 33 | + // construct shared request params |
| 34 | + const options = { |
| 35 | + method: 'POST', |
| 36 | + headers: { |
| 37 | + Authorization: `Bearer ${userConfig.token}`, |
| 38 | + }, |
| 39 | + responseType: 'json', |
| 40 | + json: {}, |
| 41 | + }; |
| 42 | + // try sending request |
| 43 | + try { |
| 44 | + const {body} = await got(remoteUrl, options); |
| 45 | + console.log(chalk.bold('Data prune successful!')); |
| 46 | + console.log(''); |
| 47 | + console.log( |
| 48 | + chalk.bold('Reclaimed:'), |
| 49 | + prettyBytes(body.data.map(item => item.SpaceReclaimed).reduce((acc, val) => acc + val, 0)) |
| 50 | + ); |
| 51 | + } catch (e) { |
| 52 | + // if authorization is expired/broken/etc |
| 53 | + if (e.response.statusCode === 401) { |
| 54 | + logout(userConfig); |
| 55 | + console.log(chalk.red('Error: authorization expired!'), 'Please, relogin and try again.'); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + console.log(chalk.red(`Error executing ${cmd} command:`), e.toString()); |
| 60 | + console.error(e); |
| 61 | + } |
| 62 | +}; |
0 commit comments