Skip to content

Commit

Permalink
feat: allow numeric log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Jul 5, 2018
1 parent d13894c commit e2a7b6f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following option flags can be used with any of the CLI commands:
| `--root` | `-r` | string | Override project root directory (defaults to working directory).
| `--silent` | `-s` | boolean | Suppress log output.
| `--env` | `-e` | string | The environment (and optionally namespace) to work against
| `--loglevel` | `-l` | `error` `warn` `info` `verbose` `debug` `silly` | Set logger level.
| `--loglevel` | `-l` | `error` `warn` `info` `verbose` `debug` `silly` `0` `1` `2` `3` `4` `5` | Set logger level, values can be either string or numeric
| `--output` | `-o` | `json` `yaml` | Output command result in specified format (note: disables progress logging).

### garden build
Expand Down
22 changes: 17 additions & 5 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
*/

import * as sywac from "sywac"
import { merge, intersection } from "lodash"
import { merge, intersection, range } from "lodash"
import { resolve } from "path"
import { safeDump } from "js-yaml"
import { coreCommands } from "../commands/commands"
import stringify = require("json-stringify-safe")

import { DeepPrimitiveMap } from "../types/common"
import {
enumToArray,
getEnumKeys,
shutdown,
sleep,
} from "../util/util"
Expand Down Expand Up @@ -63,6 +63,18 @@ const OUTPUT_RENDERERS = {
},
}

const logLevelKeys = getEnumKeys(LogLevel)
// Allow string or numeric log levels
const logLevelChoices = [...logLevelKeys, ...range(logLevelKeys.length).map(String)]

const getLogLevelFromArg = (level: string) => {
const lvl = parseInt(level, 10)
if (lvl) {
return lvl
}
return LogLevel[level]
}

export const GLOBAL_OPTIONS = {
root: new StringParameter({
alias: "r",
Expand All @@ -77,8 +89,8 @@ export const GLOBAL_OPTIONS = {
env: new EnvironmentOption(),
loglevel: new ChoicesParameter({
alias: "l",
choices: enumToArray(LogLevel),
help: "Set logger level.",
choices: logLevelChoices,
help: "Set logger level, values can be either string or numeric",
defaultValue: LogLevel[LogLevel.info],
}),
output: new ChoicesParameter({
Expand Down Expand Up @@ -174,7 +186,7 @@ export class GardenCli {
const { env, loglevel, silent, output } = parsedOpts

// Init logger
const level = LogLevel[<string>loglevel]
const level = getLogLevelFromArg(loglevel)
let writers: Writer[] = []

if (!silent && !output && loggerType !== LoggerType.quiet) {
Expand Down
6 changes: 3 additions & 3 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@ export function deserializeValues(o: object) {
return mapValues(o, deserializeObject)
}

export const enumToArray = Enum => (
Object.values(Enum).filter(k => typeof k === "string") as string[]
)
export function getEnumKeys(Enum) {
return Object.values(Enum).filter(k => typeof k === "string") as string[]
}

export function highlightYaml(s: string) {
return highlight(s, {
Expand Down

0 comments on commit e2a7b6f

Please sign in to comment.