Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
d1b58a00
Commit
d1b58a00
authored
Jan 11, 2017
by
Faissal Elamraoui
Committed by
GitHub
Jan 11, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2373 from fugr/config
avoid creating tmp files to read/parse config
parents
9266ece7
6a2ee371
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
51 deletions
+44
-51
ini.go
config/ini.go
+23
-20
xml.go
config/xml/xml.go
+8
-20
yaml.go
config/yaml/yaml.go
+13
-11
No files found.
config/ini.go
View file @
d1b58a00
...
...
@@ -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.
...
...
config/xml/xml.go
View file @
d1b58a00
...
...
@@ -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.
...
...
config/yaml/yaml.go
View file @
d1b58a00
...
...
@@ -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
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment