Skip to content

Commit

Permalink
feat: add loglevel as cli option and remove silent/verbose options
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Apr 25, 2018
1 parent 42fa17e commit 985c160
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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({
Expand All @@ -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"
Expand Down Expand Up @@ -262,19 +259,17 @@ 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))
const root = resolve(process.cwd(), optsForAction.root)
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[<string>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 }),
Expand Down
4 changes: 4 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 985c160

Please sign in to comment.