From 165cb77a85bc56716b7e66d471b19fce9cda3523 Mon Sep 17 00:00:00 2001 From: Raghu Rajagopalan Date: Fri, 20 Apr 2018 16:18:13 +0530 Subject: [PATCH] feat(cmd): add support to specify log level --- cmd/libasciidoc/root_cmd.go | 14 ++++++++++++++ cmd/libasciidoc/root_cmd_test.go | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/cmd/libasciidoc/root_cmd.go b/cmd/libasciidoc/root_cmd.go index c3d84973..e91f0421 100644 --- a/cmd/libasciidoc/root_cmd.go +++ b/cmd/libasciidoc/root_cmd.go @@ -6,9 +6,12 @@ import ( "github.com/bytesparadise/libasciidoc" "github.com/bytesparadise/libasciidoc/renderer" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) +var logLevel string + // NewRootCmd returns the root command func NewRootCmd() *cobra.Command { var source string @@ -26,8 +29,19 @@ func NewRootCmd() *cobra.Command { } return nil }, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + lvl, err := log.ParseLevel(logLevel) + if err != nil { + log.Errorf("unable to parse log level %v", err) + return err + } + log.Debug("Setting log level to %v", lvl) + log.SetLevel(lvl) + return nil + }, } flags := rootCmd.Flags() flags.StringVarP(&source, "source", "s", "", "the path to the asciidoc source to process") + rootCmd.PersistentFlags().StringVar(&logLevel, "log", "warning", "log level to set {debug, info, warning, error, fatal, panic}") return rootCmd } diff --git a/cmd/libasciidoc/root_cmd_test.go b/cmd/libasciidoc/root_cmd_test.go index 9e64f8a5..f423f327 100644 --- a/cmd/libasciidoc/root_cmd_test.go +++ b/cmd/libasciidoc/root_cmd_test.go @@ -37,6 +37,18 @@ var _ = Describe("root cmd", func() { require.Error(GinkgoT(), err) }) + It("should fail to parse bad log level", func() { + // given + root := main.NewRootCmd() + buf := new(bytes.Buffer) + root.SetOutput(buf) + root.SetArgs([]string{"--log", "debug1", "-s", "test/test.adoc"}) + // when + err := root.Execute() + // then + GinkgoT().Logf("command output: %v", buf.String()) + require.Error(GinkgoT(), err) + }) }) func TestRootCommand(t *testing.T) {