mirror of
https://github.com/eiblog/eiblog.git
synced 2026-03-01 00:34:58 +08:00
update vendor
This commit is contained in:
41
vendor/github.com/qiniu/x/config.v7/getdir.go
generated
vendored
Normal file
41
vendor/github.com/qiniu/x/config.v7/getdir.go
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
var homeEnvNames = [][]string{
|
||||
{"HOME"},
|
||||
{"HOMEDRIVE", "HOMEPATH"},
|
||||
}
|
||||
|
||||
var (
|
||||
ErrHomeNotFound = errors.New("$HOME not found")
|
||||
)
|
||||
|
||||
func getEnv(name []string) (v string) {
|
||||
|
||||
if len(name) == 1 {
|
||||
return os.Getenv(name[0])
|
||||
}
|
||||
for _, k := range name {
|
||||
v += os.Getenv(k)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetDir(app string) (dir string, err error) {
|
||||
|
||||
for _, name := range homeEnvNames {
|
||||
home := getEnv(name)
|
||||
if home == "" {
|
||||
continue
|
||||
}
|
||||
dir = home + "/." + app
|
||||
err = os.MkdirAll(dir, 0777)
|
||||
return
|
||||
}
|
||||
return "", ErrHomeNotFound
|
||||
}
|
||||
|
||||
116
vendor/github.com/qiniu/x/config.v7/load_conf.go
generated
vendored
Normal file
116
vendor/github.com/qiniu/x/config.v7/load_conf.go
generated
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
|
||||
"qiniupkg.com/x/log.v7"
|
||||
)
|
||||
|
||||
var (
|
||||
confName *string
|
||||
)
|
||||
|
||||
func Init(cflag, app, default_conf string) {
|
||||
|
||||
confDir, _ := GetDir(app)
|
||||
confName = flag.String(cflag, confDir+"/"+default_conf, "the config file")
|
||||
}
|
||||
|
||||
func GetPath() string {
|
||||
|
||||
if confName != nil {
|
||||
return *confName
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func Load(conf interface{}) (err error) {
|
||||
|
||||
if !flag.Parsed() {
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
log.Info("Use the config file of ", *confName)
|
||||
return LoadEx(conf, *confName)
|
||||
}
|
||||
|
||||
func LoadEx(conf interface{}, confName string) (err error) {
|
||||
|
||||
data, err := ioutil.ReadFile(confName)
|
||||
if err != nil {
|
||||
log.Error("Load conf failed:", err)
|
||||
return
|
||||
}
|
||||
data = trimComments(data)
|
||||
|
||||
err = json.Unmarshal(data, conf)
|
||||
if err != nil {
|
||||
log.Error("Parse conf failed:", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func LoadFile(conf interface{}, confName string) (err error) {
|
||||
|
||||
data, err := ioutil.ReadFile(confName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data = trimComments(data)
|
||||
|
||||
return json.Unmarshal(data, conf)
|
||||
}
|
||||
|
||||
func LoadBytes(conf interface{}, data []byte) (err error) {
|
||||
|
||||
return json.Unmarshal(trimComments(data), conf)
|
||||
}
|
||||
|
||||
func LoadString(conf interface{}, data string) (err error) {
|
||||
|
||||
return json.Unmarshal(trimComments([]byte(data)), conf)
|
||||
}
|
||||
|
||||
func trimComments(data []byte) (data1 []byte) {
|
||||
|
||||
var line []byte
|
||||
|
||||
data1 = data[:0]
|
||||
for {
|
||||
pos := bytes.IndexByte(data, '\n')
|
||||
if pos < 0 {
|
||||
line = data
|
||||
} else {
|
||||
line = data[:pos+1]
|
||||
}
|
||||
data1 = append(data1, trimCommentsLine(line)...)
|
||||
if pos < 0 {
|
||||
return
|
||||
}
|
||||
data = data[pos+1:]
|
||||
}
|
||||
}
|
||||
|
||||
func trimCommentsLine(line []byte) []byte {
|
||||
|
||||
n := len(line)
|
||||
quoteCount := 0
|
||||
for i := 0; i < n; i++ {
|
||||
c := line[i]
|
||||
switch c {
|
||||
case '\\':
|
||||
i++
|
||||
case '"':
|
||||
quoteCount++
|
||||
case '#':
|
||||
if (quoteCount&1) == 0 {
|
||||
return line[:i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return line
|
||||
}
|
||||
|
||||
59
vendor/github.com/qiniu/x/config.v7/load_conf_test.go
generated
vendored
Normal file
59
vendor/github.com/qiniu/x/config.v7/load_conf_test.go
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"qiniupkg.com/x/config.v7"
|
||||
)
|
||||
|
||||
func TestTrimComments(t *testing.T) {
|
||||
|
||||
confData := `{
|
||||
"debug_level": 0, # 调试级别
|
||||
"rs_host": "http://localhost:15001", #RS服务
|
||||
"limit": 5, #限制数
|
||||
"retryTimes": 56,
|
||||
"quote0": "###",
|
||||
"quote": "quo\\\"\\#",
|
||||
"ant": "ant\\#" #123
|
||||
}`
|
||||
|
||||
confDataExp := `{
|
||||
"debug_level": 0,
|
||||
"rs_host": "http://localhost:15001",
|
||||
"limit": 5,
|
||||
"retryTimes": 56,
|
||||
"quote0": "###",
|
||||
"quote": "quo\\\"\\#",
|
||||
"ant": "ant\\#"
|
||||
}`
|
||||
|
||||
var (
|
||||
conf, confExp interface{}
|
||||
)
|
||||
err := config.LoadString(&conf, confData)
|
||||
if err != nil {
|
||||
t.Fatal("config.LoadString(conf) failed:", err)
|
||||
}
|
||||
err = config.LoadString(&confExp, confDataExp)
|
||||
if err != nil {
|
||||
t.Fatal("config.LoadString(confExp) failed:", err)
|
||||
}
|
||||
|
||||
b, err := json.Marshal(conf)
|
||||
if err != nil {
|
||||
t.Fatal("json.Marshal failed:", err)
|
||||
}
|
||||
|
||||
bExp, err := json.Marshal(confExp)
|
||||
if err != nil {
|
||||
t.Fatal("json.Marshal(exp) failed:", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(b, bExp) {
|
||||
t.Fatal("b != bExp")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user