From 1ff7e154bb6def40205697ff671af4920a40d953 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Mon, 13 Apr 2026 09:13:24 +0700 Subject: [PATCH] feat: auto-load .env via shared internal/env package Add godotenv dependency and internal/env init loader so all cmd/ scripts get .env vars loaded automatically via blank import. --- go-util/cmd/gitea-leave-orgs/main.go | 2 ++ go-util/go.mod | 2 ++ go-util/go.sum | 2 ++ go-util/internal/env/load.go | 16 ++++++++++++++++ 4 files changed, 22 insertions(+) create mode 100644 go-util/go.sum create mode 100644 go-util/internal/env/load.go diff --git a/go-util/cmd/gitea-leave-orgs/main.go b/go-util/cmd/gitea-leave-orgs/main.go index 1500849..69a4d06 100644 --- a/go-util/cmd/gitea-leave-orgs/main.go +++ b/go-util/cmd/gitea-leave-orgs/main.go @@ -8,6 +8,8 @@ import ( "net/http" "os" "strings" + + _ "github.com/tiennm99/go-util/internal/env" ) // org represents a Gitea organization. diff --git a/go-util/go.mod b/go-util/go.mod index 9e03d03..a2eae01 100644 --- a/go-util/go.mod +++ b/go-util/go.mod @@ -1,3 +1,5 @@ module github.com/tiennm99/go-util go 1.26.1 + +require github.com/joho/godotenv v1.5.1 // indirect diff --git a/go-util/go.sum b/go-util/go.sum new file mode 100644 index 0000000..d61b19e --- /dev/null +++ b/go-util/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/go-util/internal/env/load.go b/go-util/internal/env/load.go new file mode 100644 index 0000000..be96049 --- /dev/null +++ b/go-util/internal/env/load.go @@ -0,0 +1,16 @@ +// Package env loads .env files on init so all cmd/ scripts get env vars automatically. +// Usage: import _ "github.com/tiennm99/go-util/internal/env" +package env + +import ( + "os" + + "github.com/joho/godotenv" +) + +func init() { + // Skip if .env doesn't exist; load if it does. + if _, err := os.Stat(".env"); err == nil { + _ = godotenv.Load() + } +}