-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
172 lines (150 loc) · 4.1 KB
/
init.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2022 huija
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tao
import (
"context"
"encoding/json"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
"log"
"os"
"path"
"strings"
"time"
)
// ConfigType of config file
type ConfigType uint8
const (
// None of config
None ConfigType = iota
// Yaml config
Yaml
// JSON config
JSON
)
// List of default config files, traverse all until one is found
// if no one found, you can config in go code.(SetConfigPath || SetAllConfigBytes)
// if you do not want to config anything, call DevelopMode() to use default configs.
var defaultConfigs = []string{
"./conf/config.yaml",
"./conf/config.json",
"./conf/config.yml",
}
func init() {
for _, confPath := range defaultConfigs {
_ = SetConfigPath(confPath)
}
}
var configPath = ""
// SetConfigPath in your project's init()
func SetConfigPath(confPath string) error {
data, err := ioutil.ReadFile(confPath)
if err != nil {
return NewErrorWrapped("init: fail to read config file", err)
}
switch t := path.Ext(confPath); t {
case ".yaml", ".yml":
err = SetAllConfigBytes(data, Yaml)
case ".json":
err = SetAllConfigBytes(data, JSON)
default:
return NewError(ParamInvalid, "%s file not supported", t)
}
if err != nil {
return NewErrorWrapped("init: fail to set config path", err)
}
configPath = confPath
return err
}
// DevelopMode called to enable default configs for all
func DevelopMode() error {
if len(once) != 0 {
return NewError(DuplicateCall, "tao: init twice")
}
return SetAllConfigBytes(nil, None)
}
// SetAllConfigBytes & taoInit can only be called once
var once = make(chan struct{}, 1)
// SetAllConfigBytes from config file or code
func SetAllConfigBytes(data []byte, configType ConfigType) (err error) {
select {
case once <- struct{}{}:
switch configType {
case Yaml:
err = yaml.Unmarshal(data, &configInterfaceMap)
case JSON:
err = json.Unmarshal(data, &configInterfaceMap)
default:
}
if err == nil {
// init tao with config
err = Register(ConfigKey, t, taoInit)
}
default:
// caused by duplicate config(file & code)
err = NewError(DuplicateCall, "config: SetConfigBytes has been called before")
}
return
}
// t global config of tao
var t = new(taoConfig)
// taoInit can only be called once before tao.Run
func taoInit() (err error) {
// SetLogger
if !t.Log.Disable {
writers := make([]io.Writer, 0)
if t.Log.Type&Console != 0 {
writers = append(writers, os.Stdout)
}
if t.Log.Type&File != 0 {
file, err := os.OpenFile(t.Log.Path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return NewErrorWrapped("init: fail to open log file", err)
}
writers = append(writers, file)
}
writer := io.MultiWriter(writers...)
err = SetWriter(ConfigKey, writer)
if err != nil {
return NewErrorWrapped("init: fail to set writer for 'tao'", err)
}
err = SetLogger(ConfigKey, &logger{Logger: log.New(writer, "", int(t.Log.Flag)), calldepth: t.Log.CallDepth})
if err != nil {
return NewErrorWrapped("init: fail to set logger for 'tao'", err)
}
}
// print banner
if !t.Banner.Hide {
w := GetWriter(ConfigKey)
if w == nil {
w = os.Stdout
}
_, err = w.Write([]byte(strings.TrimSpace(t.Banner.Content) + "\n"))
if err != nil {
return NewErrorWrapped("init: fail to write banner of tao", err)
}
}
// init universe after tao
return universeInit()
}
func universeInit() error {
if tao.universe.State() != Runnable {
return NewError(TaskRunTwice, "universe: init twice")
}
// universe run
timeout, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
return tao.universe.Run(timeout, nil)
}