This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
/
cmdr.ext.go
67 lines (56 loc) · 1.43 KB
/
cmdr.ext.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
// Copyright © 2020 Hedzr Yeh.
package vxconf
import (
"fmt"
"github.com/hedzr/cmdr"
"gopkg.in/yaml.v3"
)
// RunMode return running mode string: prod, devel, staging, ...
func RunMode(defaultVal ...string) (runMode string) {
if len(defaultVal) == 0 {
runMode = cmdr.GetStringR("runmode", "prod")
} else {
runMode = cmdr.GetStringR("runmode", defaultVal...)
}
return
}
// RunModeExt return running mode and position: prod-newyork, devel-paris, staging, ...
func RunModeExt(defaultVal ...string) (runMode string) {
if len(defaultVal) == 0 {
runMode = cmdr.GetStringR("runmode", "prod")
} else {
runMode = cmdr.GetStringR("runmode", defaultVal...)
}
pos := cmdr.GetStringR("pos")
if pos != "" {
runMode = fmt.Sprintf("%s-%s", runMode, pos)
}
return
}
// IsProd return true if app is in production mode.
func IsProd() bool {
switch RunMode("devel") {
case "prod", "production":
return true
}
return false
}
// LoadSectionTo returns error while cannot yaml Marshal and Unmarshal
func LoadSectionTo(runMode, sectionKeyPath string, configHolder interface{}) (err error) {
var b []byte
aKey := fmt.Sprintf("%s.%s", sectionKeyPath, runMode)
fObj := cmdr.GetMapR(aKey)
if fObj == nil {
fObj = cmdr.GetMapR(sectionKeyPath)
}
b, err = yaml.Marshal(fObj)
if err != nil {
return
}
err = yaml.Unmarshal(b, configHolder)
if err != nil {
return
}
// cmdr.Logger.Debugf("configuration section got: %v", configHolder)
return
}