diff --git a/pkg/cmd/download.go b/pkg/cmd/download.go index 2994847c9a7244c46dc5a0a8f3fc474c960b0bef..aaa73828d1743488852d89feb583add92ba5e11b 100644 --- a/pkg/cmd/download.go +++ b/pkg/cmd/download.go @@ -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}, diff --git a/pkg/cmd/flags.go b/pkg/cmd/flags.go index 64ff6d88ee7941f2a276325b24f8a15db0759956..1f573e78e8c8cd494c9aa238b7ba6987502cc233 100644 --- a/pkg/cmd/flags.go +++ b/pkg/cmd/flags.go @@ -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() +} diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 1c74cfdf8ba941b425d2fafeb0481b0fa48615f0..9ca10cdb36889c816bf1dae446e6caaa9478dec9 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -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) diff --git a/pkg/cmd/use.go b/pkg/cmd/use.go index 0297afe29db668f2f3ff4c762d13f02aa242d9b3..9966b60c6b15e26d1f902e4e76730db313f39a3f 100644 --- a/pkg/cmd/use.go +++ b/pkg/cmd/use.go @@ -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}, diff --git a/pkg/cmd/version.go b/pkg/cmd/version.go index df06182483442b48aeb40f4e09a04f6c890f7817..ca1e38f97c0095657f7fb77f997dd26ea34161d6 100644 --- a/pkg/cmd/version.go +++ b/pkg/cmd/version.go @@ -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") diff --git a/pkg/util/tlog/log.go b/pkg/util/tlog/log.go index ac69ef004f0c0014edca3da057699e71b223e856..ddd9871946f70322d9504bf25a55f7cad09e4852 100644 --- a/pkg/util/tlog/log.go +++ b/pkg/util/tlog/log.go @@ -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) }