mirror of
https://github.com/astaxie/beego.git
synced 2024-12-23 03:10:50 +00:00
Merge pull request #2373 from fugr/config
avoid creating tmp files to read/parse config
This commit is contained in:
commit
d1b58a00ce
@ -18,16 +18,13 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -52,24 +49,26 @@ func (ini *IniConfig) Parse(name string) (Configer, error) {
|
||||
}
|
||||
|
||||
func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
|
||||
file, err := os.Open(name)
|
||||
data, err := ioutil.ReadFile(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ini.parseData(data)
|
||||
}
|
||||
|
||||
func (ini *IniConfig) parseData(data []byte) (*IniConfigContainer, error) {
|
||||
cfg := &IniConfigContainer{
|
||||
file.Name(),
|
||||
make(map[string]map[string]string),
|
||||
make(map[string]string),
|
||||
make(map[string]string),
|
||||
sync.RWMutex{},
|
||||
data: make(map[string]map[string]string),
|
||||
sectionComment: make(map[string]string),
|
||||
keyComment: make(map[string]string),
|
||||
RWMutex: sync.RWMutex{},
|
||||
}
|
||||
cfg.Lock()
|
||||
defer cfg.Unlock()
|
||||
defer file.Close()
|
||||
|
||||
var comment bytes.Buffer
|
||||
buf := bufio.NewReader(file)
|
||||
buf := bufio.NewReader(bytes.NewBuffer(data))
|
||||
// check the BOM
|
||||
head, err := buf.Peek(3)
|
||||
if err == nil && head[0] == 239 && head[1] == 187 && head[2] == 191 {
|
||||
@ -130,16 +129,24 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
|
||||
|
||||
// handle include "other.conf"
|
||||
if len(keyValue) == 1 && strings.HasPrefix(key, "include") {
|
||||
|
||||
includefiles := strings.Fields(key)
|
||||
if includefiles[0] == "include" && len(includefiles) == 2 {
|
||||
|
||||
otherfile := strings.Trim(includefiles[1], "\"")
|
||||
if !filepath.IsAbs(otherfile) {
|
||||
otherfile = filepath.Join(filepath.Dir(name), otherfile)
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
otherfile = filepath.Join(dir, otherfile)
|
||||
}
|
||||
|
||||
i, err := ini.parseFile(otherfile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for sec, dt := range i.data {
|
||||
if _, ok := cfg.data[sec]; !ok {
|
||||
cfg.data[sec] = make(map[string]string)
|
||||
@ -148,12 +155,15 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
|
||||
cfg.data[sec][k] = v
|
||||
}
|
||||
}
|
||||
|
||||
for sec, comm := range i.sectionComment {
|
||||
cfg.sectionComment[sec] = comm
|
||||
}
|
||||
|
||||
for k, comm := range i.keyComment {
|
||||
cfg.keyComment[k] = comm
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
}
|
||||
@ -178,19 +188,12 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
|
||||
|
||||
// ParseData parse ini the data
|
||||
func (ini *IniConfig) ParseData(data []byte) (Configer, error) {
|
||||
// Save memory data to temporary file
|
||||
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
|
||||
os.MkdirAll(path.Dir(tmpName), os.ModePerm)
|
||||
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ini.Parse(tmpName)
|
||||
return ini.parseData(data)
|
||||
}
|
||||
|
||||
// IniConfigContainer A Config represents the ini configuration.
|
||||
// When set and get value, support key as section:name type.
|
||||
type IniConfigContainer struct {
|
||||
filename string
|
||||
data map[string]map[string]string // section=> key:val
|
||||
sectionComment map[string]string // section : comment
|
||||
keyComment map[string]string // id: []{comment, key...}; id 1 is for main comment.
|
||||
|
@ -35,11 +35,9 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/config"
|
||||
"github.com/beego/x2j"
|
||||
@ -52,36 +50,26 @@ type Config struct{}
|
||||
|
||||
// Parse returns a ConfigContainer with parsed xml config map.
|
||||
func (xc *Config) Parse(filename string) (config.Configer, error) {
|
||||
file, err := os.Open(filename)
|
||||
context, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return xc.ParseData(context)
|
||||
}
|
||||
|
||||
// ParseData xml data
|
||||
func (xc *Config) ParseData(data []byte) (config.Configer, error) {
|
||||
x := &ConfigContainer{data: make(map[string]interface{})}
|
||||
content, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d, err := x2j.DocToMap(string(content))
|
||||
d, err := x2j.DocToMap(string(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
x.data = config.ExpandValueEnvForMap(d["config"].(map[string]interface{}))
|
||||
return x, nil
|
||||
}
|
||||
|
||||
// ParseData xml data
|
||||
func (xc *Config) ParseData(data []byte) (config.Configer, error) {
|
||||
// Save memory data to temporary file
|
||||
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
|
||||
os.MkdirAll(path.Dir(tmpName), os.ModePerm)
|
||||
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return xc.Parse(tmpName)
|
||||
return x, nil
|
||||
}
|
||||
|
||||
// ConfigContainer A Config represents the xml configuration.
|
||||
|
@ -37,10 +37,8 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/config"
|
||||
"github.com/beego/goyaml2"
|
||||
@ -63,26 +61,30 @@ func (yaml *Config) Parse(filename string) (y config.Configer, err error) {
|
||||
|
||||
// ParseData parse yaml data
|
||||
func (yaml *Config) ParseData(data []byte) (config.Configer, error) {
|
||||
// Save memory data to temporary file
|
||||
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
|
||||
os.MkdirAll(path.Dir(tmpName), os.ModePerm)
|
||||
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
|
||||
cnf, err := parseYML(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return yaml.Parse(tmpName)
|
||||
|
||||
return &ConfigContainer{
|
||||
data: cnf,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ReadYmlReader Read yaml file to map.
|
||||
// if json like, use json package, unless goyaml2 package.
|
||||
func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
|
||||
f, err := os.Open(path)
|
||||
buf, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
buf, err := ioutil.ReadAll(f)
|
||||
if err != nil || len(buf) < 3 {
|
||||
return parseYML(buf)
|
||||
}
|
||||
|
||||
// parseYML parse yaml formatted []byte to map.
|
||||
func parseYML(buf []byte) (cnf map[string]interface{}, err error) {
|
||||
if len(buf) < 3 {
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user