Skip to content

Instantly share code, notes, and snippets.

@myndzi
Last active January 2, 2016 03:49
Show Gist options
  • Save myndzi/8246337 to your computer and use it in GitHub Desktop.
Save myndzi/8246337 to your computer and use it in GitHub Desktop.
Setup:
// (actually this is module.exports)
var Prompt = new Blackbird(function (Promise) {
function Prompt(opts, ctx) {
this._opts = opts || { };
// copy over initialization variables
ctx = ctx || { };
for (var key in ctx) {
if (ctx.hasOwnProperty(key)) {
this[key] = ctx[key];
}
}
}
Prompt.prototype.ask = function (res, msg, opts) {
..etc..
};
Prompt.prototype.etc...
Promise.extend('ask');
return Prompt;
});
----
Usage:
var prompt = new Prompt({}, {
addrs: [ ],
iface: null
});
prompt()
.ask('Interface to use:', {
key: 'iface',
validate: Object.keys(interfaces),
default: 'eth0'
})
.ask('Add an IPv6 subnet?', {
type: 'boolean',
default: 'N',
ifTrue: function () { return prompt().ask('Enter subnet:').then(addSubnet); }
})
.then(function () {
var addrs = interfaces[this.iface];
this.v4addrs = addrs.filter(function (a) {
return a.family === 'IPv4' && a.internal === false;
});
this.v6addrs = addrs.filter(function (a) {
return a.family === 'IPv6' && a.internal === false;
});
})
.ask(function () {
var count = this.v4addrs.length;
return {
type: 'boolean',
question: 'Use ' + count + ' IPv4 addresses?',
default: count <= 1 ? 'N' : 'Y',
ifTrue: function () { this.addrs = this.addrs.concat(this.v4addrs); }
};
})
.ask(function () {
var count = this.v6addrs.length;
return {
type: 'boolean',
question: 'Use ' + count + ' IPv6 addresses?',
default: count <= 1 ? 'N' : 'Y',
ifTrue: function () { this.addrs = this.addrs.concat(this.v6addrs); }
};
})
.then(function () {
console.log(this);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment