@@ -4,18 +4,33 @@ import (
44 "fmt"
55 "os"
66
7+ "github.com/sirupsen/logrus"
8+
9+ "github.com/devstream-io/devstream/internal/log"
10+
711 "github.com/spf13/cobra"
812 "github.com/spf13/viper"
13+
14+ "github.com/devstream-io/devstream/internal/option"
915)
1016
1117var cfgFile string
18+
19+ // isDebug is a flag to enable debug level log
20+ var isDebug bool
21+
22+ // OutputFormat is the output format for the command. One of: json|yaml|raw
23+ // Default value is "raw"
1224var OutputFormat string
1325
1426// rootCmd represents the base command when called without any subcommands
1527var rootCmd = & cobra.Command {
1628 Use : "dtm" ,
1729 Short : "dtm is a tool to manage variaties of development platforms" ,
1830 Long : `dtm is a tool to manage variaties of development platforms.` ,
31+ PersistentPreRun : func (cmd * cobra.Command , args []string ) {
32+ initLog ()
33+ },
1934}
2035
2136// Execute adds all child commands to the root command and sets flags appropriately.
@@ -30,14 +45,9 @@ func Execute() {
3045func init () {
3146 cobra .OnInitialize (initConfig )
3247
33- // Here you will define your flags and configuration settings.
34- // Cobra supports persistent flags, which, if defined here,
35- // will be global for your application.
36-
3748 rootCmd .PersistentFlags ().StringVar (& cfgFile , "config" , "" , "config file (default is $HOME/.devstream.yaml)" )
38-
39- // Cobra also supports local flags, which will only run
40- // when this action is called directly.
49+ rootCmd .PersistentFlags ().StringVarP (& OutputFormat , "output" , "o" , "raw" , "Output format. One of: json|yaml|raw" )
50+ rootCmd .PersistentFlags ().BoolVarP (& isDebug , "debug" , "" , false , "debug level log" )
4151 rootCmd .Flags ().BoolP ("toggle" , "t" , false , "Help message for toggle" )
4252}
4353
@@ -63,4 +73,20 @@ func initConfig() {
6373 if err := viper .ReadInConfig (); err == nil {
6474 fmt .Fprintln (os .Stderr , "Using config file:" , viper .ConfigFileUsed ())
6575 }
76+
77+ if OutputFormat != "raw" {
78+ option .Silence = true
79+ }
80+ }
81+
82+ func initLog () {
83+ // if OutputFormat is not "raw", set log level to PanicLevel to disable log
84+ if OutputFormat != "raw" {
85+ logrus .SetLevel (logrus .PanicLevel )
86+ } else if isDebug {
87+ logrus .SetLevel (logrus .DebugLevel )
88+ log .Infof ("Log level is: %s." , logrus .GetLevel ())
89+ } else {
90+ logrus .SetLevel (logrus .InfoLevel )
91+ }
6692}
0 commit comments