Last active
July 23, 2017 17:12
-
-
Save eljefedelrodeodeljefe/ca74fe511e3ed8ce99d5b67a5f80efed to your computer and use it in GitHub Desktop.
Hierarchical, Recursive Conversation Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const argv = require('minimist')(process.argv.slice(2)) | |
const clear = require('clear') | |
const chalk = require('chalk') | |
const debug = require('debug')('binary') | |
const EventEmitter = require('events') | |
const questions = require('./questions') | |
const help = require('./help') | |
const setTitle = require('./setTtitle') // not included here | |
const runKubernetes = require('./runKubernetes') // not included here | |
const ee = new EventEmitter() | |
setTitle('dopsbox') // terminal title | |
const options = {} | |
if (argv.help || !argv._.length) return help() | |
if (argv._.length) clear() // always clear | |
if (argv._.length === 1) { | |
switch (argv._[0]) { | |
case 'start': | |
quetions((err, answers) => { | |
if (err) throw err | |
// handle natural language input explicitly | |
options.destination = answers.destination.toLowerCase() | |
if (answers.orchestration === 'Docker') { | |
options.orchestration = 'docker' | |
} else { | |
options.orchestration = 'kubernetes' // also the fallback | |
} | |
ee.emit('exec', options) // jump out of scope | |
}) | |
break | |
default: | |
help() // fallback | |
} | |
} | |
ee.on('exec', (options) => { | |
// simple feature toggle, because nothing else is prepared | |
if (options.orchestration === 'kubernetes') { | |
options.argv = argv | |
debug(options) | |
runKubernetes(options, (err) => { | |
if (err) throw err | |
}) | |
} else { | |
console.log('nothing happened') | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const inquirer = require('inquirer') | |
const tree = { | |
'orchestration': { | |
type: 'list', | |
name: 'orchestration', | |
message: 'Which container orchestration system do you want to demo:', | |
choices: ['Docker', 'Kubernetes'], | |
default: 'Kubernetes', | |
exit: false | |
}, | |
'server_environment': { | |
type: 'list', | |
name: 'destination', | |
message: 'Should this run locally or in the cloud:', | |
choices: ['Cloud', 'Locally', 'Bare Metal'], | |
default: 'Locally', | |
next: { | |
'Cloud': { | |
type: 'list', | |
name: 'cloud_vendor', | |
message: 'Which cloud vendor do you want to use?', | |
choices: ['GCE', 'AWS', 'GKE', 'Azure'], | |
exit: false | |
} | |
} | |
}, | |
'ci': { | |
type: 'list', | |
name: 'ci', | |
message: 'Which CI system do you want to integrate at this moment?', | |
choices: ['Jenkins', 'Gitlab CI', 'CircleCI (Enterprise)', 'CircleCI (SaaS)', 'Travis'], | |
default: 'Jenkins', | |
exit: false | |
}, | |
'registry': { | |
type: 'checkbox', | |
name: 'registry', | |
message: 'Please Choose which artifact store to bootstrap.', | |
choices: ['Private Docker Registry'], | |
default: ['Private Docker Registry'], | |
exit: false | |
}, | |
'quality': { | |
type: 'checkbox', | |
name: 'quality', | |
message: 'Do you also want to run code quality services?', | |
choices: ['Sonar', 'Code Climate'], | |
exit: true | |
} | |
} | |
function recurseTree (obj, counter = 0, cont, contCounter, answers = {}, cb) { | |
const level = Object.keys(obj) | |
// mind, lots of the whole shebang below is handle the Promises in a | |
// 'functional' way | |
inquirer.prompt(obj[level[counter]]).then((answer) => { | |
let next = obj[level[counter]].next | |
// if the answer given is not matching the condition for the | |
// next-handler, invalidate it. The condition here is the object's | |
// key | |
if (next && !next[answer[obj[level[counter]].name]]) next = false | |
counter++ | |
// merge answers and pass it on, in order to return it later | |
Object.assign(answers, answer) | |
// branch for continuation condition. Recurse down if necessary. | |
// else return to caller's scope with answers. | |
if (counter < level.length) { | |
if (next) { | |
recurseTree(next, 0, obj, counter, answers, cb) | |
} else { | |
recurseTree(obj, counter, null, null, answers, cb) | |
} | |
} else if (cont) { | |
recurseTree(cont, contCounter, null, null, answers, cb) | |
} else { | |
return cb(null, answers) | |
} | |
}) | |
} | |
module.exports = (cb) => { | |
recurseTree(tree, undefined, null, null, undefined, cb) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chalk = require('chalk') | |
module.exports = () => { | |
const name = '\n DOPSBOX' | |
const helpText = ` | |
Commands: | |
install - Installs all needed dependencies | |
Global options: | |
--no-[NAME_OF_SERVICE] - skip creation of specific service, e.g. --no-monitoring | |
` | |
console.log(chalk.yellow(name)) // some extra styling for the title | |
console.log(chalk.green(helpText)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment