@@ -16,18 +16,18 @@ limitations under the License.
1616package cmd
1717
1818import (
19- "fmt"
20- "os"
2119 "path/filepath"
2220
23- "github.com/kleveross/ormb/pkg/ormb"
24- "github.com/spf13/cobra"
25-
2621 homedir "github.com/mitchellh/go-homedir"
22+ "github.com/sirupsen/logrus"
23+ "github.com/spf13/cobra"
2724 "github.com/spf13/viper"
25+
26+ "github.com/kleveross/ormb/pkg/ormb"
2827)
2928
3029var cfgFile string
30+ var logLevel uint32
3131
3232var ormbClient ormb.Interface
3333
@@ -42,26 +42,36 @@ var rootCmd = &cobra.Command{
4242// This is called by main.main(). It only needs to happen once to the rootCmd.
4343func Execute () {
4444 if err := rootCmd .Execute (); err != nil {
45- fmt .Println (err )
46- os .Exit (1 )
45+ logrus .WithField ("error" , err ).Panicln ("Failed to run the command" )
4746 }
4847}
4948
5049func init () {
5150 viper .SetEnvPrefix ("ORMB" )
51+ cobra .OnInitialize (initLogger )
5252 cobra .OnInitialize (initConfig )
5353
5454 // Here you will define your flags and configuration settings.
5555 // Cobra supports persistent flags, which, if defined here,
5656 // will be global for your application.
5757
5858 rootCmd .PersistentFlags ().StringVar (& cfgFile , "config" , "" , "config file (default is $HOME/.ormb/config.yaml)" )
59+ rootCmd .PersistentFlags ().Uint32Var (& logLevel , "log-level" , 4 , "Log level" )
5960
6061 // Cobra also supports local flags, which will only run
6162 // when this action is called directly.
6263 rootCmd .Flags ().BoolP ("toggle" , "t" , false , "Help message for toggle" )
6364}
6465
66+ func initLogger () {
67+ logrus .SetLevel (logrus .Level (logLevel ))
68+
69+ logrus .SetFormatter (& logrus.TextFormatter {
70+ FullTimestamp : true ,
71+ })
72+ logrus .SetReportCaller (false )
73+ }
74+
6575// initConfig reads in config file and ENV variables if set.
6676func initConfig () {
6777 if cfgFile != "" {
@@ -71,8 +81,7 @@ func initConfig() {
7181 // Find home directory.
7282 home , err := homedir .Dir ()
7383 if err != nil {
74- fmt .Println (err )
75- os .Exit (1 )
84+ logrus .WithField ("error" , err ).Panicln ("Failed to find the home directory" )
7685 }
7786
7887 // Search config in home directory with name "config" (without extension).
@@ -87,6 +96,8 @@ func initConfig() {
8796
8897 // If a config file is found, read it in.
8998 if err := viper .ReadInConfig (); err == nil {
90- fmt .Println ("Using config file:" , viper .ConfigFileUsed ())
99+ logrus .WithFields (logrus.Fields {
100+ "config" : viper .ConfigFileUsed (),
101+ }).Debugln ("Found the config file" )
91102 }
92103}
0 commit comments