(Feat): Initial Commit
This commit is contained in:
36
internal/util/get_project_root_dir.go
Normal file
36
internal/util/get_project_root_dir.go
Normal file
@@ -0,0 +1,36 @@
|
||||
//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
|
||||
}
|
||||
Reference in New Issue
Block a user