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
14114018
Commit
14114018
authored
Oct 24, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config ini support include
parent
8ac2b9bf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
8 deletions
+52
-8
config.go
config.go
+10
-6
ini.go
config/ini.go
+42
-2
No files found.
config.go
View file @
14114018
...
...
@@ -88,13 +88,13 @@ type beegoAppConfig struct {
innerConfig
config
.
ConfigContainer
}
func
newAppConfig
(
AppConfigProvider
,
AppConfigPath
string
)
*
beegoAppConfig
{
func
newAppConfig
(
AppConfigProvider
,
AppConfigPath
string
)
(
*
beegoAppConfig
,
error
)
{
ac
,
err
:=
config
.
NewConfig
(
AppConfigProvider
,
AppConfigPath
)
if
err
!=
nil
{
ac
=
config
.
NewFakeConfig
()
return
nil
,
err
}
rac
:=
&
beegoAppConfig
{
ac
}
return
rac
return
rac
,
nil
}
func
(
b
*
beegoAppConfig
)
Set
(
key
,
val
string
)
error
{
...
...
@@ -281,15 +281,19 @@ func init() {
err
=
ParseConfig
()
if
err
!=
nil
&&
!
os
.
IsNotExist
(
err
)
{
// for init if doesn't have app.conf will not panic
Info
(
err
)
ac
:=
config
.
NewFakeConfig
()
AppConfig
=
&
beegoAppConfig
{
ac
}
Warning
(
err
)
}
}
// ParseConfig parsed default config file.
// now only support ini, next will support json.
func
ParseConfig
()
(
err
error
)
{
AppConfig
=
newAppConfig
(
AppConfigProvider
,
AppConfigPath
)
AppConfig
,
err
=
newAppConfig
(
AppConfigProvider
,
AppConfigPath
)
if
err
!=
nil
{
return
err
}
envRunMode
:=
os
.
Getenv
(
"BEEGO_RUNMODE"
)
// set the runmode first
if
envRunMode
!=
""
{
...
...
config/ini.go
View file @
14114018
...
...
@@ -48,6 +48,10 @@ type IniConfig struct {
// ParseFile creates a new Config and parses the file configuration from the named file.
func
(
ini
*
IniConfig
)
Parse
(
name
string
)
(
ConfigContainer
,
error
)
{
return
ini
.
parseFile
(
name
)
}
func
(
ini
*
IniConfig
)
parseFile
(
name
string
)
(
*
IniConfigContainer
,
error
)
{
file
,
err
:=
os
.
Open
(
name
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -66,6 +70,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
var
comment
bytes
.
Buffer
buf
:=
bufio
.
NewReader
(
file
)
// check the BOM
head
,
err
:=
buf
.
Peek
(
3
)
if
err
==
nil
&&
head
[
0
]
==
239
&&
head
[
1
]
==
187
&&
head
[
2
]
==
191
{
for
i
:=
1
;
i
<=
3
;
i
++
{
...
...
@@ -114,13 +119,48 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
cfg
.
data
[
section
]
=
make
(
map
[
string
]
string
)
}
keyValue
:=
bytes
.
SplitN
(
line
,
bEqual
,
2
)
key
:=
string
(
bytes
.
TrimSpace
(
keyValue
[
0
]))
// key name case insensitive
key
=
strings
.
ToLower
(
key
)
// 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
!
path
.
IsAbs
(
otherfile
)
{
otherfile
=
path
.
Join
(
path
.
Dir
(
name
),
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
)
}
for
k
,
v
:=
range
dt
{
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
}
}
if
len
(
keyValue
)
!=
2
{
return
nil
,
errors
.
New
(
"read the content error:
\"
"
+
string
(
line
)
+
"
\"
, should key = val"
)
}
val
:=
bytes
.
TrimSpace
(
keyValue
[
1
])
if
bytes
.
HasPrefix
(
val
,
bDQuote
)
{
val
=
bytes
.
Trim
(
val
,
`"`
)
}
key
:=
string
(
bytes
.
TrimSpace
(
keyValue
[
0
]))
// key name case insensitive
key
=
strings
.
ToLower
(
key
)
cfg
.
data
[
section
][
key
]
=
string
(
val
)
if
comment
.
Len
()
>
0
{
cfg
.
keyComment
[
section
+
"."
+
key
]
=
comment
.
String
()
...
...
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