-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
150 lines (135 loc) · 3.33 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
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
// Copyright 2021 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 (
"encoding/json"
"fmt"
"log"
)
// Config interface
type Config interface {
// Name of Config
Name() string
// ValidSelf with some default values
ValidSelf()
// ToTask transform itself to Task
ToTask() Task
// RunAfter defines pre task names
RunAfter() []string
}
// init config file to this interface map
var configInterfaceMap = make(map[string]interface{})
// transform interface to concrete Config type
var configMap = make(map[string]Config)
// LoadConfig by key of config
func LoadConfig(configKey string, config Config) error {
c, ok := configInterfaceMap[configKey]
if !ok {
return NewError(ConfigNotFound, "config: %q not found", configKey)
}
bytes, err := json.Marshal(c)
if err != nil {
return NewErrorWrapped("config: fail to marshal", err)
}
err = json.Unmarshal(bytes, &config)
if err != nil {
return NewErrorWrapped(fmt.Sprintf("config: fail to unmarshal config bytes for %q", configKey), err)
}
return nil
}
// SetConfig by key & Config
func SetConfig(configKey string, config Config) error {
_, ok := configMap[configKey]
if ok {
return NewError(DuplicateCall, "config: %s has been set before", configKey)
}
configMap[configKey] = config
return nil
}
// ConfigKey for this repo
const ConfigKey = "tao"
// taoConfig implements Config
type taoConfig struct {
Log *Log `json:"log"`
Banner *Banner `json:"banner"`
}
// Banner config
type Banner struct {
Hide bool `json:"hide"`
Content string `json:"content"`
}
var defaultTao = &taoConfig{
Log: &Log{
Level: DEBUG,
Type: Console | File,
Flag: log.LstdFlags | log.Lshortfile,
CallDepth: 3,
Path: "./test.log",
Disable: false,
},
Banner: &Banner{
Hide: false,
Content: `
___________
\__ ___/____ ____
| | \__ \ / _ \
| | / __ \( <_> )
|____| (____ /\____/
\/
`,
},
}
// Name of Config
func (t *taoConfig) Name() string {
return ConfigKey
}
// ValidSelf with some default values
func (t *taoConfig) ValidSelf() {
if t.Log == nil {
t.Log = defaultTao.Log
} else {
if t.Log.Level < DEBUG || t.Log.Level > FATAL {
t.Log.Level = defaultTao.Log.Level
}
if t.Log.CallDepth <= 0 {
t.Log.CallDepth = defaultTao.Log.CallDepth
}
if t.Log.Type == 0 {
t.Log.Type = defaultTao.Log.Type
}
if t.Log.Type&File != 0 {
if t.Log.Path == "" {
t.Log.Path = defaultTao.Log.Path
}
}
if t.Log.Flag == 0 {
t.Log.Flag = defaultTao.Log.Flag
}
}
if t.Banner == nil {
t.Banner = defaultTao.Banner
} else {
if !t.Banner.Hide && t.Banner.Content == "" {
t.Banner.Content = defaultTao.Banner.Content
}
}
}
// ToTask transform itself to Task
func (t *taoConfig) ToTask() Task {
return nil
}
// RunAfter defines pre task names
func (t *taoConfig) RunAfter() []string {
return nil
}