1 Star 0 Fork 0

bughou / go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
env_root.go 1.24 KB
一键复制 编辑 原始数据 按行查看 历史
bughou 提交于 2022-03-20 18:50 . save
package config
import (
"log"
"os"
"path/filepath"
"strings"
"gitee.com/go-better/dev/config/config"
"gitee.com/go-better/dev/os/fs"
)
var theEnv = config.NewEnv(getEnv())
var rootDir = getRoot(theEnv)
var configDir = filepath.Join(Root(), "config")
func Env() *config.Environment {
return &theEnv
}
func Root() string {
return rootDir
}
func Dir() string {
return configDir
}
func FilePath(env *config.Environment) string {
return filepath.Join(Dir(), env.Minor()+`.yml`)
}
func getEnv() string {
env := os.Getenv(config.EnvVar)
if env == "" {
if strings.HasSuffix(os.Args[0], ".test") {
env = "test"
} else {
env = "dev"
}
}
return env
}
func getRoot(env config.Environment) string {
root := detectRootByExecutable(env.Minor())
if root == "" {
if release := config.DetectReleaseConfigDirOf(env.Major()); release != "" {
root = filepath.Join(release, "img-app")
}
}
if root == "" {
log.Panic("app root not found.")
}
return root
}
func detectRootByExecutable(minorEnv string) string {
executable, err := filepath.Abs(os.Args[0])
if err != nil {
log.Panic(err)
}
executableDir := filepath.Dir(executable)
if fs.Exist(filepath.Join(executableDir, "config", minorEnv+".yml")) {
return executableDir
}
return ""
}
Go
1
https://gitee.com/bughou/go.git
git@gitee.com:bughou/go.git
bughou
go
go
d31700df43a9

搜索帮助