diff --git a/packages/cli/src/hosting/index.js b/packages/cli/src/hosting/index.js index d1d9c0c1d6..db0596a54a 100644 --- a/packages/cli/src/hosting/index.js +++ b/packages/cli/src/hosting/index.js @@ -1,7 +1,10 @@ -exports.config = { - short: "-h", - long: "hosting", - opts: ["init", "start", "stop", "update"], -} +const Option = require("../utils/Option") -exports.addOption = () => {} +const option = new Option("-h", "hosting") + .addHelp("Controls self hosting on the Budibase platform.") + .addSubOption("init", "Configure a self hosted platform in current directory.") + .addSubOption("start", "Start the configured platform in current directory.") + .addSubOption("stop", "Stop the configured platform in the current directory.") + .addSubOption("update", "Updates the Budibase images to the latest version.") + +exports.option = option diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index e1a4b0b074..78f40f6ed7 100644 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -1,8 +1,18 @@ -const hosting = require("./hosting") - +const { getOptions, addHelp, displayHelp } = require("./options") const { Command } = require("commander") -const program = new Command() - -program - .option(hosting.Config.short, hosting.Config.) +// add hosting config +function init() { + const program = new Command() + addHelp(program) + for (let option of getOptions()) { + option.configure(program) + } + program.parse(process.argv) + const userInput = program.opts() + for (let option of getOptions()) { + if (option.isItThisOption(userInput)) { + option.execute(userInput) + } + } +} diff --git a/packages/cli/src/options.js b/packages/cli/src/options.js new file mode 100644 index 0000000000..7f8a991a8c --- /dev/null +++ b/packages/cli/src/options.js @@ -0,0 +1,13 @@ +const hosting = require("./hosting") + +exports.getOptions = () => { + return [hosting.option] +} + +exports.addHelp = program => { + program.option("-h", "help") +} + +exports.displayHelp = () => { + +} diff --git a/packages/cli/src/utils/Option.js b/packages/cli/src/utils/Option.js new file mode 100644 index 0000000000..96c2ced9cd --- /dev/null +++ b/packages/cli/src/utils/Option.js @@ -0,0 +1,52 @@ +class Option { + constructor(short, long) { + this.short = short + this.long = long + this.opts = [] + } + + addHelp(help) { + this.help = help + return this + } + + addSubOption(command, help) { + this.opts.push({command, help}) + return this + } + + configure(program) { + if (this.opts.length !== 0) { + const opts = this.opts.map(opt => opt.command) + // add a help option to all sub-options + if (opts.indexOf("help") === -1) { + opts.push("help") + } + program.option(this.short, this.long, opts) + } else { + program.option(this.short, this.long) + } + } + + getHelp() { + return this.help + } + + getSubOptions() { + if (this.opts) { + return [{command: "help", help: "Display help for this command."}].concat(this.opts) + } else { + return [] + } + } + + isItThisOption(userInput) { + return !!userInput[this.long] + } + + execute(userInput) { + + } +} + +module.exports = Option