diff --git a/docs/reference/commands.md b/docs/reference/commands.md index 99fa683376..f69d19f1c8 100644 --- a/docs/reference/commands.md +++ b/docs/reference/commands.md @@ -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 diff --git a/src/cli/cli.ts b/src/cli/cli.ts index c2f424c980..8d9be2b1df 100644 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -7,7 +7,7 @@ */ 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" @@ -15,7 +15,7 @@ import stringify = require("json-stringify-safe") import { DeepPrimitiveMap } from "../types/common" import { - enumToArray, + getEnumKeys, shutdown, sleep, } from "../util/util" @@ -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", @@ -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({ @@ -174,7 +186,7 @@ export class GardenCli { const { env, loglevel, silent, output } = parsedOpts // Init logger - const level = LogLevel[loglevel] + const level = getLogLevelFromArg(loglevel) let writers: Writer[] = [] if (!silent && !output && loggerType !== LoggerType.quiet) { diff --git a/src/util/util.ts b/src/util/util.ts index a7a7317afd..2e01ccf3a0 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -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, {