-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_root.go
65 lines (58 loc) · 1.23 KB
/
env_root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package config
import (
"log"
"os"
"path/filepath"
"strings"
"github.com/lovego/config/config"
"github.com/lovego/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 ""
}