diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6f1934b --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +all: tidy test + +tidy: + @echo "Tidy up..." + @go mod tidy -v + +test: + @echo "Running tests..." + @go test -cover ./... diff --git a/log/log.go b/log/log.go index 7f00c51..1bf68b2 100644 --- a/log/log.go +++ b/log/log.go @@ -10,7 +10,7 @@ var lg = logrus.New() // Setup configures the global logger instance with level and // formatter. -func Setup(format, level string) { +func Setup(level, format string) { lvl, err := logrus.ParseLevel(level) if err != nil { lvl = logrus.WarnLevel diff --git a/moonshot.go b/moonshot.go index 6aa926f..d841412 100644 --- a/moonshot.go +++ b/moonshot.go @@ -6,6 +6,7 @@ import ( "github.com/go-chi/chi" "github.com/spf13/cobra" + "github.com/spy16/moonshot/log" ) // App represents an instance of app command. Invoke App.Launch() @@ -27,7 +28,17 @@ func (app *App) Launch(ctx context.Context, cmds ...*cobra.Command) int { DisableDefaultCmd: true, }, } - root.PersistentFlags().StringP("config", "c", "", "Config file path override") + + flags := root.PersistentFlags() + + var logLevel, logFormat string + flags.StringP("config", "c", "", "Config file path override") + flags.StringVar(&logLevel, "log-level", "info", "Log level") + flags.StringVar(&logFormat, "log-format", "text", "Log format (json/text)") + + root.PersistentPreRun = func(cmd *cobra.Command, args []string) { + log.Setup(logLevel, logFormat) + } root.AddCommand(cmds...) root.AddCommand(