From ab57c7fa4189fe7cc79a52b6cce79d69f477fc48 Mon Sep 17 00:00:00 2001 From: user-penguin Date: Tue, 12 Nov 2024 03:10:06 +0700 Subject: [PATCH] fix env --- build/ci/.drone.yml | 6 +++--- cmd/smm_tg/main.go | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/build/ci/.drone.yml b/build/ci/.drone.yml index 0aff000..54278df 100644 --- a/build/ci/.drone.yml +++ b/build/ci/.drone.yml @@ -7,16 +7,16 @@ steps: pull: if-not-exists image: golang:1.23 environment: - TELEGRAM_TOKEN: + BOT_TOKEN: from_secret: bot_token POSTGRES_URL_CONNECT: from_secret: postgres_url_connect settings: - envs: [ TELEGRAM_TOKEN,POSTGRES_URL_CONNECT ] + envs: [ BOT_TOKEN,POSTGRES_URL_CONNECT ] commands: - CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o smm_tg cmd/smm_tg/main.go - touch .env - - echo TELEGRAM_TOKEN=$${TELEGRAM_TOKEN} >> .env + - echo BOT_TOKEN=$${TELEGRAM_TOKEN} >> .env - echo POSTGRES_URL_CONNECT=$${POSTGRES_URL_CONNECT} >> .env - name: test diff --git a/cmd/smm_tg/main.go b/cmd/smm_tg/main.go index fbf4f10..76ae44a 100644 --- a/cmd/smm_tg/main.go +++ b/cmd/smm_tg/main.go @@ -2,7 +2,9 @@ package main import ( "context" + "fmt" "os" + "path/filepath" "git.3crabs.ru/save_my_money/smm_tg/internal/modules/messenger/telegram" "git.3crabs.ru/save_my_money/smm_tg/internal/services/bot" @@ -15,7 +17,7 @@ func main() { // try get sysenv telegarmToken := os.Getenv("BOT_TOKEN") if len(telegarmToken) == 0 { - if err := godotenv.Load("../../.env"); err != nil { + if err := godotenv.Load(dir(".env")); err != nil { panic(err) } telegarmToken = os.Getenv("BOT_TOKEN") @@ -38,3 +40,25 @@ func main() { ctx := context.Background() listenerService.Run(ctx) } + +func dir(envFile string) string { + currentDir, err := os.Getwd() + if err != nil { + panic(err) + } + + for { + goModPath := filepath.Join(currentDir, "go.mod") + if _, err := os.Stat(goModPath); err == nil { + break + } + + parent := filepath.Dir(currentDir) + if parent == currentDir { + panic(fmt.Errorf("go.mod not found")) + } + currentDir = parent + } + + return filepath.Join(currentDir, envFile) +}