Files
Gumble-Backend/internal/util/get_project_root_dir.go
Zane Walker 7e940c83a7
Some checks failed
Build & Test / build-test (push) Has been cancelled
Build & Test / swagger-codegen-cli (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
(Feat): Initial Commit
2026-07-03 19:41:31 +05:30

37 lines
969 B
Go

//go:build !scripts
// nolint:revive
package util
import (
"os"
"path/filepath"
"sync"
"github.com/rs/zerolog/log"
)
var (
projectRootDir string
dirOnce sync.Once
)
// GetProjectRootDir returns the path as string to the project_root for a **running application**.
// Note: This function should not be used for generation targets (go generate, make go-generate).
// Thus it's explicitly excluded from the build tag scripts, see instead:
// * /scripts/get_project_root_dir.go
// * ./get_project_root_dir_scripts.go (delegates to above)
// https://stackoverflow.com/questions/43215655/building-multiple-binaries-using-different-packages-and-build-tags
func GetProjectRootDir() string {
dirOnce.Do(func() {
ex, err := os.Executable()
if err != nil {
log.Panic().Err(err).Msg("Failed to get executable path while retrieving project root directory")
}
projectRootDir = GetEnv("PROJECT_ROOT_DIR", filepath.Dir(ex))
})
return projectRootDir
}