Skip to content

Commit

Permalink
Merge pull request #194 from garden-io/numeric-loglevels
Browse files Browse the repository at this point in the history
feat: allow numeric log levels (#89)
  • Loading branch information
edvald authored Jul 9, 2018
2 parents d13894c + 37c3159 commit 3e3d13f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 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
26 changes: 21 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,12 @@ 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 and are prioritized from 0 to 5 " +
"(highest to lowest) as follows: error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5",
hints:
"[enum] [default: info] [error || 0, warn || 1, info || 2, verbose || 3, debug || 4, silly || 5]",
defaultValue: LogLevel[LogLevel.info],
}),
output: new ChoicesParameter({
Expand Down Expand Up @@ -174,7 +190,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: 4 additions & 2 deletions src/cli/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const styleConfig = {
usagePrefix: str => (
`
${chalk.bold(str.slice(0, 5).toUpperCase())}
${chalk.italic(str.slice(7))}`
),
usageCommandPlaceholder: str => chalk.blue(str),
Expand All @@ -34,7 +33,7 @@ ${chalk.bold(str.slice(0, 5).toUpperCase())}
usageOptionsPlaceholder: str => chalk.yellow(str),
group: (str: string) => {
const cleaned = str.endsWith(":") ? str.slice(0, -1) : str
return chalk.bold(cleaned.toUpperCase()) + "\n"
return chalk.bold(cleaned.toUpperCase())
},
flags: (str, _type) => {
const style = str.startsWith("-") ? chalk.green : chalk.magenta
Expand Down Expand Up @@ -115,13 +114,15 @@ export interface SywacOptionConfig {
defaultValue?: any
choices?: any[]
required?: boolean
hints?: string
strict: true
}

export function prepareOptionConfig(param: Parameter<any>): SywacOptionConfig {
const {
defaultValue,
help: desc,
hints,
required,
type,
} = param
Expand All @@ -136,6 +137,7 @@ export function prepareOptionConfig(param: Parameter<any>): SywacOptionConfig {
desc,
required,
type,
hints,
strict: true,
}
if (type === "choice") {
Expand Down
5 changes: 4 additions & 1 deletion src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ParameterConstructor<T> {
alias?: string,
defaultValue?: T,
valueName?: string,
hints?: string,
overrides?: string[],
}

Expand All @@ -35,13 +36,15 @@ export abstract class Parameter<T> {
help: string
required: boolean
alias?: string
hints?: string
valueName: string
overrides: string[]

constructor({ help, required, alias, defaultValue, valueName, overrides }: ParameterConstructor<T>) {
constructor({ help, required, alias, defaultValue, valueName, overrides, hints }: ParameterConstructor<T>) {
this.help = help
this.required = required || false
this.alias = alias
this.hints = hints
this.defaultValue = defaultValue
this.valueName = valueName || "_valueType"
this.overrides = overrides || []
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 3e3d13f

Please sign in to comment.