-
Notifications
You must be signed in to change notification settings - Fork 6
/
osx.go
84 lines (74 loc) · 1.45 KB
/
osx.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package osx
import (
"github.com/joho/godotenv"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/wttech/aemc/pkg/common/pathx"
"github.com/wttech/aemc/pkg/common/stringsx"
"os"
"runtime"
"strings"
)
var (
EnvFileExt = "env"
EnvVar = "AEM_ENV"
EnvLocal = "local"
EnvLocalFile = EnvLocal + "." + EnvFileExt
)
func IsWindows() bool {
return runtime.GOOS == "windows"
}
func IsDarwin() bool {
return runtime.GOOS == "darwin"
}
func IsLinux() bool {
return !IsWindows() && !IsDarwin()
}
func EnvVarsLoad() {
name := os.Getenv(EnvVar)
if name == "" {
name = EnvLocal
}
for _, file := range []string{
name + "." + EnvFileExt,
"." + EnvFileExt + "." + name,
"." + EnvFileExt,
} {
if pathx.Exists(file) {
if err := godotenv.Overload(file); err != nil {
log.Fatalf("cannot load env file '%s': %s", file, err)
}
}
}
}
func EnvVarsMap() map[string]string {
result := make(map[string]string)
for _, e := range os.Environ() {
if i := strings.Index(e, "="); i >= 0 {
result[e[:i]] = e[i+1:]
}
}
return result
}
func EnvVarsWithout(names ...string) []string {
var result []string
for _, pair := range os.Environ() {
key := stringsx.Before(pair, "=")
if !lo.Contains(names, key) {
result = append(result, pair)
}
}
return result
}
func LineSep() string {
if pathx.Sep() == "\\" {
return "\r\n"
}
return "\n"
}
func PathVarSep() string {
if pathx.Sep() == "\\" {
return ";"
}
return ":"
}