Skip to content

Commit

Permalink
fix: .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
FMotalleb committed Jun 2, 2024
1 parent 4aa46a0 commit bcd66dd
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
10 changes: 5 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"

"github.com/joho/godotenv"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -41,6 +42,7 @@ func init() {
}

func initConfig() {
godotenv.Load()
viper.BindEnv(
"log_timestamp_format",
"timestamp_format",
Expand All @@ -67,14 +69,14 @@ func initConfig() {
} else {
viper.SetConfigName("config")
viper.SetConfigType("yaml")

}

viper.AutomaticEnv()

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)
}
Expand Down
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
13 changes: 7 additions & 6 deletions enums/logger_format_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package main

import (
"encoding/json"
"fmt"
"strings"

"github.com/sirupsen/logrus"

Expand All @@ -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))
}

0 comments on commit bcd66dd

Please sign in to comment.