Skip to content
Snippets Groups Projects
Commit dc59b93e authored by David McKay's avatar David McKay Committed by Tamer Tas
Browse files

Feature/enable log levels (#19)

Implement log levels for pkg tlog
parent 6caa7833
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,7 @@ import (
"github.com/tmrts/boilr/pkg/host"
"github.com/tmrts/boilr/pkg/util/exit"
"github.com/tmrts/boilr/pkg/util/osutil"
"github.com/tmrts/boilr/pkg/util/tlog"
"github.com/tmrts/boilr/pkg/util/validate"
)
......@@ -98,6 +99,8 @@ var Download = &cli.Command{
Short: "Download a project template from a github repository to template registry",
// FIXME Half-Updates leave messy templates
Run: func(c *cli.Command, args []string) {
tlog.SetLogLevel(GetStringFlag(c, "log-level"))
MustValidateArgs(args, []validate.Argument{
{"template-repo", validate.UnixPath},
{"template-tag", validate.Alphanumeric},
......
......@@ -7,3 +7,7 @@ import cli "github.com/spf13/cobra"
func GetBoolFlag(c *cli.Command, name string) bool {
return c.PersistentFlags().Lookup(name).Value.String() == "true"
}
func GetStringFlag(c *cli.Command, name string) string {
return c.PersistentFlags().Lookup(name).Value.String()
}
......@@ -22,6 +22,7 @@ func Run() {
Template.AddCommand(Delete)
Download.PersistentFlags().BoolP("force", "f", false, "Overwrite existing template with the same name")
Download.PersistentFlags().StringP("log-level", "l", "error", "log-level for output")
Template.AddCommand(Download)
List.PersistentFlags().BoolP("dont-prettify", "", false, "Print only the template names without fancy formatting")
......@@ -33,6 +34,7 @@ func Run() {
Template.AddCommand(Save)
Use.PersistentFlags().BoolP("use-defaults", "f", false, "Uses default values in project.json instead of prompting the user")
Use.PersistentFlags().StringP("log-level", "l", "error", "log-level for output")
Template.AddCommand(Use)
Template.AddCommand(Validate)
......
......@@ -12,6 +12,7 @@ import (
"github.com/tmrts/boilr/pkg/template"
"github.com/tmrts/boilr/pkg/util/exit"
"github.com/tmrts/boilr/pkg/util/osutil"
"github.com/tmrts/boilr/pkg/util/tlog"
"github.com/tmrts/boilr/pkg/util/validate"
)
......@@ -32,6 +33,8 @@ var Use = &cli.Command{
Use: "use <template-tag> <target-dir>",
Short: "Execute a project template in the given directory",
Run: func(cmd *cli.Command, args []string) {
tlog.SetLogLevel(GetStringFlag(cmd, "log-level"))
MustValidateArgs(args, []validate.Argument{
{"template-tag", validate.UnixPath},
{"target-dir", validate.UnixPath},
......
......@@ -15,6 +15,8 @@ var Version = &cli.Command{
Use: "version",
Short: "Show the boilr version information",
Run: func(c *cli.Command, args []string) {
tlog.SetLogLevel(GetStringFlag(c, "log-level"))
MustValidateArgs(args, []validate.Argument{})
shouldntPrettify := GetBoolFlag(c, "dont-prettify")
......
......@@ -29,6 +29,30 @@ const (
QuestionMark = "?"
)
const (
LevelDebug = 32
LevelFatal = 16
LevelError = 8
LevelWarn = 4
LevelInfo = 2
LevelSuccess = 1
)
var LogLevel uint16
func SetLogLevel(LogLevelString string) {
switch strings.ToLower(LogLevelString) {
case "debug":
LogLevel |= (LevelSuccess | LevelError | LevelFatal | LevelWarn | LevelInfo | LevelDebug)
case "info":
LogLevel |= (LevelSuccess | LevelError | LevelFatal | LevelWarn | LevelInfo)
case "warn":
LogLevel |= (LevelSuccess | LevelError | LevelFatal | LevelWarn)
default:
LogLevel |= (LevelSuccess | LevelError | LevelFatal)
}
}
// TODO add log levels
func coloredPrintMsg(icon string, msg string, iC color.Attribute, mC color.Attribute) {
fmt.Println(
......@@ -38,6 +62,10 @@ func coloredPrintMsg(icon string, msg string, iC color.Attribute, mC color.Attri
// Debug logs the given message as a debug message.
func Debug(msg string) {
if 0 == LogLevel & LevelDebug {
return
}
coloredPrintMsg(DebugMark, msg, color.FgYellow, color.FgYellow)
}
......@@ -48,11 +76,19 @@ func Success(msg string) {
// Info logs the given message as a info message.
func Info(msg string) {
if 0 == LogLevel & LevelInfo {
return
}
coloredPrintMsg(InfoMark, msg, color.FgWhite, color.FgBlue)
}
// Warn logs the given message as a warn message.
func Warn(msg string) {
if 0 == LogLevel & LevelWarn {
return
}
coloredPrintMsg(WarnMark, msg, color.FgMagenta, color.FgMagenta)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment