Skip to content

Commit

Permalink
Merge pull request #46 from ccil-kbw/feature/ISSUE-36
Browse files Browse the repository at this point in the history
ISSUE-36 - Organize Environment and Logger init
  • Loading branch information
GitHNX authored May 13, 2024
2 parents 0927e22 + 0084dae commit 21ca548
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
8 changes: 7 additions & 1 deletion cmd/darsrec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package main

import (
"fmt"
rec2 "github.com/ccil-kbw/robot/pkg/rec"
"os"

environment "github.com/ccil-kbw/robot/internal/environment"
rec2 "github.com/ccil-kbw/robot/pkg/rec"
)

func init() {
environment.LoadEnvironmentVariables()
}

// TODO: Swap all this as a darsRec.StartServer()
func main() {

Expand Down
17 changes: 5 additions & 12 deletions cmd/discord_bot/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"log"

"github.com/caarlos0/env/v11"
environment "github.com/ccil-kbw/robot/internal/environment"
"github.com/ccil-kbw/robot/pkg/discord"
"github.com/joho/godotenv"
"go.uber.org/zap"
"log"
)

// TODO: Should Types have a folder of their own?
type config struct {
Environment string `env:"ENVIRONMENT" envDefault:"dev"`
DiscordServerID string `env:"DISCORD_SERVER_ID"`
Expand All @@ -21,7 +23,7 @@ var (
)

func init() {
loadEnvs()
environment.LoadEnvironmentVariables()
loadConfig()
initializeLogger()
}
Expand All @@ -31,15 +33,6 @@ func main() {
bot.StartBot()
}

func loadEnvs() {
err := godotenv.Load()
if err != nil {
log.Println(".env not present, using process envs and defaults")
} else {
log.Println(".env loaded")
}
}

func loadConfig() {
if err := env.Parse(&cfg); err != nil {
log.Fatalf("%+v\n", err)
Expand Down
15 changes: 2 additions & 13 deletions cmd/monolith/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"fmt"
"log"

environment "github.com/ccil-kbw/robot/internal/environment"
"github.com/ccil-kbw/robot/pkg/discord"
rec2 "github.com/ccil-kbw/robot/pkg/rec"

Expand All @@ -13,8 +13,6 @@ import (
"time"

"go.uber.org/zap"

"github.com/joho/godotenv"
)

var (
Expand Down Expand Up @@ -45,7 +43,7 @@ var (
)

func init() {
loadEnvs()
environment.LoadEnvironmentVariables()
}

func main() {
Expand Down Expand Up @@ -137,12 +135,3 @@ func startServerWithRetry(host string, password string, data *rec2.RecordConfigD
}
}
}

func loadEnvs() {
err := godotenv.Load()
if err != nil {
log.Println(".env not present, using process envs and defaults")
} else {
log.Println(".env loaded")
}
}
16 changes: 16 additions & 0 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package environment

import (
"log"

"github.com/joho/godotenv"
)

func LoadEnvironmentVariables() {
err := godotenv.Load()
if err != nil {
log.Println(".env not present, using process envs and defaults")
} else {
log.Println(".env loaded")
}
}

0 comments on commit 21ca548

Please sign in to comment.