From b4f1d82a51b92bbaaf823a75d7aef63fb7678ebc Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 14 Nov 2016 11:22:08 +0200 Subject: [PATCH] fix(CLI): check CLI elevation early on (#864) The CLI requires elevation permissions to actually be able to write to drives. For this reason, it makes sense that we introduce such a check early on. See: https://github.com/resin-io/etcher/issues/726 Change-Type: patch Changelog-Entry: Check available permissions in the CLI early on. Signed-off-by: Juan Cruz Viotti --- lib/cli/etcher.js | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/lib/cli/etcher.js b/lib/cli/etcher.js index 1d1727f7..2e5b6af1 100644 --- a/lib/cli/etcher.js +++ b/lib/cli/etcher.js @@ -18,6 +18,7 @@ const _ = require('lodash'); const Bluebird = require('bluebird'); +const isElevated = Bluebird.promisify(require('is-elevated')); const visuals = require('resin-cli-visuals'); const form = require('resin-cli-form'); const drivelist = Bluebird.promisifyAll(require('drivelist')); @@ -26,28 +27,34 @@ const utils = require('./utils'); const options = require('./cli'); const EXIT_CODES = require('../src/exit-codes'); -form.run([ - { - message: 'Select drive', - type: 'drive', - name: 'drive' - }, - { - message: 'This will erase the selected drive. Are you sure?', - type: 'confirm', - name: 'yes', - default: false +isElevated().then((elevated) => { + if (!elevated) { + throw new Error('This should should be run with root/administrator permissions'); } -], { - override: { - drive: options.drive, - // If `options.yes` is `false`, pass `undefined`, - // otherwise the question will not be asked because - // `false` is a defined value. - yes: options.robot || options.yes || undefined + return form.run([ + { + message: 'Select drive', + type: 'drive', + name: 'drive' + }, + { + message: 'This will erase the selected drive. Are you sure?', + type: 'confirm', + name: 'yes', + default: false + } + ], { + override: { + drive: options.drive, - } + // If `options.yes` is `false`, pass `undefined`, + // otherwise the question will not be asked because + // `false` is a defined value. + yes: options.robot || options.yes || undefined + + } + }); }).then((answers) => { if (!answers.yes) { throw new Error('Aborted');