Skip to content

Commit

Permalink
feat: add global --output flag for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald authored and eysi09 committed May 14, 2018
1 parent 8998907 commit 7f25653
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"is-subset": "^0.1.1",
"joi": "^13.3.0",
"js-yaml": "^3.11.0",
"json-stringify-safe": "^5.0.1",
"klaw": "^2.1.1",
"kubernetes-client": "^5.2.0",
"log-symbols": "^2.2.0",
Expand Down Expand Up @@ -66,6 +67,7 @@
"@types/joi": "^13.0.8",
"@types/inquirer": "0.0.41",
"@types/js-yaml": "^3.11.1",
"@types/json-stringify-safe": "^5.0.0",
"@types/klaw": "^2.1.1",
"@types/log-symbols": "^2.0.0",
"@types/log-update": "^2.0.0",
Expand Down
45 changes: 42 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { safeDump } from "js-yaml"
import * as sywac from "sywac"
import chalk from "chalk"
import { RunCommand } from "./commands/run"
import { DeepPrimitiveMap } from "./types/common"
import { enumToArray, shutdown } from "./util"
import { merge, intersection, reduce } from "lodash"
import {
Expand Down Expand Up @@ -39,6 +41,16 @@ import { StatusCommand } from "./commands/status"
import { PushCommand } from "./commands/push"
import { LoginCommand } from "./commands/login"
import { LogoutCommand } from "./commands/logout"
import stringify = require("json-stringify-safe")

const OUTPUT_RENDERERS = {
json: (data: DeepPrimitiveMap) => {
return stringify(data, null, 2)
},
yaml: (data: DeepPrimitiveMap) => {
return safeDump(data, { noRefs: true, skipInvalid: true })
},
}

const GLOBAL_OPTIONS = {
root: new StringParameter({
Expand All @@ -58,6 +70,11 @@ const GLOBAL_OPTIONS = {
help: "set logger level",
defaultValue: LogLevel[LogLevel.info],
}),
output: new ChoicesParameter({
alias: "o",
choices: Object.keys(OUTPUT_RENDERERS),
help: "output command result in specified format (note: disable progress logging)",
}),
}
const GLOBAL_OPTIONS_GROUP_NAME = "Global options"
const STYLE_CONFIG = {
Expand Down Expand Up @@ -278,10 +295,10 @@ export class GardenCli {

// Update logger
const logger = this.logger
const { loglevel, silent } = optsForAction
const { loglevel, silent, output } = optsForAction
const level = LogLevel[<string>loglevel]
logger.level = level
if (!silent) {
if (!silent && !output) {
logger.writers.push(
new FileWriter({ level, root }),
new FileWriter({ level: LogLevel.error, filename: ERROR_LOG_FILENAME, root }),
Expand All @@ -291,7 +308,29 @@ export class GardenCli {
}

const garden = await Garden.factory(root, { env, logger })
return command.action(garden.pluginContext, argsForAction, optsForAction)

try {
// TODO: enforce that commands always output DeepPrimitiveMap
const result = await command.action(garden.pluginContext, argsForAction, optsForAction)

if (output && result !== undefined) {
const renderer = OUTPUT_RENDERERS[output]
console.log(renderer({ success: true, result }))
}

return result

} catch (error) {
// TODO: we can likely do this better
const result = { error }

if (output) {
const renderer = OUTPUT_RENDERERS[output]
console.error(renderer(result))
}

return result
}
}

// Command specific positional args and options are set inside the builder function
Expand Down

0 comments on commit 7f25653

Please sign in to comment.