From 985c16060ec71b2f906ee900c035e1984ef9906d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ey=C3=BE=C3=B3r=20Magn=C3=BAsson?= Date: Sun, 22 Apr 2018 16:42:51 +0200 Subject: [PATCH] feat: add loglevel as cli option and remove silent/verbose options --- src/cli.ts | 27 +++++++++++---------------- src/util.ts | 4 ++++ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 8d56789799..fb5bcbe25a 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -8,10 +8,9 @@ import * as sywac from "sywac" import chalk from "chalk" -import { shutdown } from "./util" +import { enumToArray, shutdown } from "./util" import { merge, intersection, reduce } from "lodash" import { - BooleanParameter, Command, ChoicesParameter, ParameterValues, @@ -36,6 +35,7 @@ import { LogLevel } from "./logger/types" import { ConfigCommand } from "./commands/config" import { StatusCommand } from "./commands/status" import { PushCommand } from "./commands/push" +import { enumToArray } from "./types/common" const GLOBAL_OPTIONS = { root: new StringParameter({ @@ -44,14 +44,11 @@ const GLOBAL_OPTIONS = { defaultValue: process.cwd(), }), env: new EnvironmentOption(), - verbose: new BooleanParameter({ - alias: "v", - help: "verbose logging", - overrides: ["silent"], - }), - silent: new BooleanParameter({ - alias: "s", - help: "silence logger", + loglevel: new ChoicesParameter({ + alias: "log", + choices: enumToArray(LogLevel), + help: "set logger level", + defaultValue: LogLevel[LogLevel.info], }), } const GLOBAL_OPTIONS_GROUP_NAME = "Global options" @@ -262,6 +259,7 @@ export class GardenCli { const action = async argv => { const logger = this.logger + // Sywac returns positional args and options in a single object which we separate into args and opts const argsForAction = filterByArray(argv, argKeys) const optsForAction = filterByArray(argv, optKeys.concat(globalKeys)) @@ -269,12 +267,9 @@ export class GardenCli { const env = optsForAction.env // Update logger config - if (argv.silent) { - logger.level = LogLevel.silent - logger.writers = [] - } else { - const level = argv.verbose ? LogLevel.verbose : logger.level - logger.level = level + const level = LogLevel[argv.loglevel] + logger.level = level + if (level !== LogLevel.silent) { logger.writers.push( new FileWriter({ level, root }), new FileWriter({ level: LogLevel.error, filename: ERROR_LOG_FILENAME, root }), diff --git a/src/util.ts b/src/util.ts index 97f6ff3d1e..e4a16dee57 100644 --- a/src/util.ts +++ b/src/util.ts @@ -343,6 +343,10 @@ export function deserializeKeys(o: object) { return mapValues(o, deserializeObject) } +export const enumToArray = Enum => ( + Object.values(Enum).filter(k => typeof k === "string") as string[] +) + export function highlightYaml(s: string) { return highlight(s, { language: "yaml",