-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
110 lines (96 loc) · 2.62 KB
/
config.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//Package config handles retrieval of configuration information
//from the config file
package config
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
)
//MssqlCn represents the connection string
type MssqlCn struct {
UserName string
Password string
Server string
Database string
Port string
CnTimeout string
AppName string
LogLevel string
Encrypt bool
}
//PrjConfig represents the configuration from the json file
type PrjConfig struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Version string `json:"version"`
Scripts CfgScripts `json:"scripts"`
}
//CfgScripts represents the list of schema and process scripts
type CfgScripts struct {
Schema []string `json:"schema"`
Process []string `json:"process"`
}
var wrkConfig PrjConfig
//wrkPath represents the working path
var wrkPath string
//GetCnString returns a sql server connection string
func GetCnString(c MssqlCn) string {
var cnStr string
cnStr += "user id=" + c.UserName +
";password=" + c.Password +
";server=" + c.Server +
";database=" + c.Database +
";port=" + c.Port +
";connection timeout=" + c.CnTimeout +
";app name=" + c.AppName +
";log=" + c.LogLevel
if c.Encrypt {
cnStr += ";encrypt=true;TrustServerCertificate=true"
}
return cnStr
}
//ReadConfig reads the config file based on location passed interface{}
func ReadConfig(f string) {
_, err := os.Stat(f)
if os.IsNotExist(err) {
log.WithFields(log.Fields{"file_name": f}).Fatal(err)
}
wrkPath = filepath.Dir(f)
configInMem, err := ioutil.ReadFile(f)
if err != nil {
log.WithFields(log.Fields{"file_name": f}).Fatal(err)
}
err = json.Unmarshal([]byte(configInMem), &wrkConfig)
if err != nil {
log.WithFields(log.Fields{"file_name": f}).Fatal(err)
}
}
//GetSchemaScripts returns the list of schema scripts from the config
func GetSchemaScripts() []string {
var scripts []string
if len(wrkConfig.Scripts.Schema) > 0 {
for i := range wrkConfig.Scripts.Schema {
scripts = append(scripts, ResolvePath(wrkConfig.Scripts.Schema[i]))
}
return scripts
}
return []string{}
}
//GetProcessScripts returns the list of process scripts from the config
func GetProcessScripts() []string {
var scripts []string
if len(wrkConfig.Scripts.Process) > 0 {
for i := range wrkConfig.Scripts.Process {
scripts = append(scripts, ResolvePath(wrkConfig.Scripts.Process[i]))
}
return scripts
}
return []string{}
}
//ResolvePath returns the fully qualified path of the name of the file passed in
func ResolvePath(p string) string {
return wrkPath + p
}