Skip to content

Commit

Permalink
Add dependency check (fix #22) (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjabell authored and ssmirr committed Jun 3, 2019
1 parent a7c0c1f commit eb60fbf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -2,6 +2,7 @@
const yargs = require('yargs');
const { version } = require('./package.json');

const { check } = require('./lib/dependencies');
const env = require('./lib/env');

// Environment reset/sanity check
Expand All @@ -12,6 +13,7 @@ const env = require('./lib/env');
await env.setup();

yargs
.middleware(check)
.commandDir('./lib/commands')
.version()
.epilog(version ? `Version: ${version}`: '')
Expand Down
2 changes: 1 addition & 1 deletion lib/args.js
Expand Up @@ -2,7 +2,7 @@ const providers = Object.keys(require('./providers'));

const providerArg = {
alias: 'p',
choices: providers,
choices: ['hyperkit', 'kvm', 'virtualbox'],
default: defaultProvider(),
description: 'the vm provider to use',
type: 'string'
Expand Down
42 changes: 42 additions & 0 deletions lib/dependencies.js
@@ -0,0 +1,42 @@
const hasbin = require('hasbin');

const providers = Object.keys(require('./providers'));

const { error } = require('./logger');

const depMap = {
'build': [
() => mustBin('cpio'),
() => mustBin('docker'),
() => mustBin('gzip'),
() => mustBin('mkisofs'),
],
'delete': [],
'images': [],
'run': [],
'vms': []
}

const mustBin = bin => {
if (!hasbin.sync(bin)) throw `You must have ${bin} installed to build a microkernel`;
}

exports.check = argv => {
let cmd = argv._[0];

try {
if (providers.length === 0) {
throw 'You don\'t have any providers installed! Please see the docs for a list of supported providers';
}

let { provider } = argv;
if (provider && providers.indexOf(provider) === -1) {
throw `Provider ${provider} is not installed! Please see the docs for a list of supported providers`;
}

depMap[cmd].forEach(d => d(argv));
} catch (e) {
error(e);
process.exit(1);
}
};
7 changes: 2 additions & 5 deletions lib/providers.js
@@ -1,12 +1,9 @@
const hasbin = require('hasbin');

// TODO add hyperkit
const kvm = require('./providers/kvm');
const vbox = require('./providers/virtualbox');

const providers = {};

if (hasbin.sync('virsh')) providers['kvm'] = kvm;
if (hasbin.sync('vboxmanage')) providers['virtualbox'] = vbox;
if (hasbin.sync('virsh')) providers['kvm'] = require('./providers/kvm');
if (hasbin.sync('vboxmanage')) providers['virtualbox'] = require('./providers/virtualbox');

module.exports = providers;

0 comments on commit eb60fbf

Please sign in to comment.