diff --git a/.env.example b/.env.example index 9fdaff7..f6d7f56 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,5 @@ -$env.LOG_TIMESTAMP_FORMAT = 2006-01-02T15:04:05.000Z -$env.LOG_FORMAT = ansi -$env.LOG_FILE = /var/log/crontab-go.log -$env.LOG_STDOUT = true -$env.LOG_LEVEL = debug +LOG_TIMESTAMP_FORMAT=2006-01-02T15:04:05.000Z +LOG_FORMAT=ansi +LOG_FILE=/var/log/crontab-go.log +LOG_STDOUT=true +LOG_LEVEL=debug diff --git a/cmd/root.go b/cmd/root.go index 060f3cd..f84ef3c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,6 +5,7 @@ import ( "log" "os" + "github.com/joho/godotenv" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -41,6 +42,7 @@ func init() { } func initConfig() { + godotenv.Load() viper.BindEnv( "log_timestamp_format", "timestamp_format", @@ -67,7 +69,6 @@ func initConfig() { } else { viper.SetConfigName("config") viper.SetConfigType("yaml") - } viper.AutomaticEnv() @@ -75,6 +76,7 @@ func initConfig() { if err := viper.ReadInConfig(); err == nil { fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) } + if err := viper.Unmarshal(CFG); err != nil { log.Fatalln("Cannot unmarshal the config file", err) } diff --git a/config/config.go b/config/config.go index 2c5c7de..db8962c 100644 --- a/config/config.go +++ b/config/config.go @@ -11,11 +11,11 @@ type ( JobMetadata = map[string]interface{} Config struct { - LogTimestampFormat string `mapstructure:"log_timestamp_format"` - LogFormat enums.LoggerFormatType `mapstructure:"log_format"` - LogFile string `mapstructure:"log_file"` - LogStdout bool `mapstructure:"log_stdout"` - LogLevel string `mapstructure:"log_level"` + LogTimestampFormat string `mapstructure:"LOG_TIMESTAMP_FORMAT"` + LogFormat enums.LoggerFormatType `mapstructure:"LOG_FORMAT"` + LogFile string `mapstructure:"LOG_FILE"` + LogStdout bool `mapstructure:"LOG_STDOUT"` + LogLevel string `mapstructure:"LOG_LEVEL"` Jobs map[string]JobConfig `mapstructure:"jobs"` } diff --git a/enums/logger_format_type.go b/enums/logger_format_type.go index 748f032..5866f6a 100644 --- a/enums/logger_format_type.go +++ b/enums/logger_format_type.go @@ -5,16 +5,17 @@ import "fmt" type LoggerFormatType string var ( - JsonLogger = LoggerFormatType("json") - AnsiLogger = LoggerFormatType("ansi") - PlainLogger = LoggerFormatType("plain") + DefaultLogger = LoggerFormatType("") + JsonLogger = LoggerFormatType("json") + AnsiLogger = LoggerFormatType("ansi") + PlainLogger = LoggerFormatType("plain") ) -func (lf *LoggerFormatType) Validate() error { +func (lf LoggerFormatType) Validate() error { switch lf { - case &JsonLogger, &AnsiLogger, &PlainLogger: + case JsonLogger, AnsiLogger, PlainLogger, DefaultLogger: return nil default: - return fmt.Errorf("Given Logger type: `%s`", *lf) + return fmt.Errorf("Given Logger type: `%s`", lf) } } diff --git a/go.mod b/go.mod index 80239a9..a05490d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/FMotalleb/crontab-go go 1.22 require ( + github.com/joho/godotenv v1.5.1 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.0 github.com/spf13/viper v1.18.2 diff --git a/go.sum b/go.sum index 8c76228..9d79ca5 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= diff --git a/main.go b/main.go index 74dca41..12d75c9 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,8 @@ package main import ( "encoding/json" + "fmt" + "strings" "github.com/sirupsen/logrus" @@ -37,6 +39,6 @@ func main() { logger.InitFromConfig() log = *logger.SetupLogger("Crontab-GO") - j, _ := json.Marshal(cmd.CFG) - logrus.Infoln(string(j)) + j, _ := json.MarshalIndent(cmd.CFG, "", " ") + fmt.Println(strings.Replace(string(j), `\n`, "\n", -1)) }