Commit 6e5ef561 by Torkel Ödegaard

Updated Godeps

parent 95305e7e
......@@ -10,10 +10,6 @@
"Rev": "d9bcf409c8a368d06c9b347705c381e7c12d54df"
},
{
"ImportPath": "github.com/Unknwon/goconfig",
"Rev": "897bf5765c8d23edc846fdab2499a63ae64b6277"
},
{
"ImportPath": "github.com/Unknwon/macaron",
"Rev": "da7cbddc50b9d33e076fb1eabff13b55c3b85fc5"
},
......
.DS_Store
*.iml
.idea
\ No newline at end of file
goconfig [![Build Status](https://drone.io/github.com/Unknwon/goconfig/status.png)](https://drone.io/github.com/Unknwon/goconfig/latest) [![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/Unknwon/goconfig) [![](http://gocover.io/_badge/github.com/Unknwon/goconfig)](http://gocover.io/github.com/Unknwon/goconfig)
========
[中文文档](README_ZH.md)
Code Convention: based on [Go Code Convention](https://github.com/Unknwon/go-code-convention).
## About
Package goconfig is a easy-use, comments-support configuration file parser for the Go Programming Language, which provides a structure similar to what you would find on Microsoft Windows INI files.
The configuration file consists of sections, led by a `[section]` header and followed by `name:value` or `name=value` entries. Note that leading whitespace is removed from values. The optional values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Comments are indicated by ";" or "#"; comments may begin anywhere on a single line.
## Features
- It simplified operation processes, easy to use and undersatnd; therefore, there are less chances to have errors.
- It uses exactly the same way to access a configuration file as you use Windows APIs, so you don't need to change your code style.
- It supports read recursion sections.
- It supports auto increment of key.
- It supports **READ** and **WRITE** configuration file with comments each section or key which all the other parsers don't support!!!!!!!
- It supports get value through type bool, float64, int, int64 and string, methods that start with "Must" means ignore errors and get zero-value if error occurs, or you can specify a default value.
- It's able to load multiple files to overwrite key values.
## Installation
go get github.com/Unknwon/goconfig
Or
gopm get github.com/Unknwon/goconfig
## API Documentation
[Go Walker](http://gowalker.org/github.com/Unknwon/goconfig).
## Example
Please see [conf.ini](testdata/conf.ini) as an example.
### Usage
- Function `LoadConfigFile` load file(s) depends on your situation, and return a variable with type `ConfigFile`.
- `GetValue` gives basic functionality of getting a value of given section and key.
- Methods like `Bool`, `Int`, `Int64` return corresponding type of values.
- Methods start with `Must` return corresponding type of values and returns zero-value of given type if something goes wrong.
- `SetValue` sets value to given section and key, and inserts somewhere if it does not exist.
- `DeleteKey` deletes by given section and key.
- Finally, `SaveConfigFile` saves your configuration to local file system.
- Use method `Reload` in case someone else modified your file(s).
- Methods contains `Comment` help you manipulate comments.
## More Information
- All characters are CASE SENSITIVE, BE CAREFULL!
## Credits
- [goconf](http://code.google.com/p/goconf/)
- [robfig/config](https://github.com/robfig/config)
- [Delete an item from a slice](https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/lYz8ftASMQ0)
## License
This project is under Apache v2 License. See the [LICENSE](LICENSE) file for the full license text.
\ No newline at end of file
goconfig [![Build Status](https://drone.io/github.com/Unknwon/goconfig/status.png)](https://drone.io/github.com/Unknwon/goconfig/latest) [![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/Unknwon/goconfig)
========
本库已被 [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases/tree/master/lectures/01-goconfig) 收录讲解,欢迎前往学习如何使用!
编码规范:基于 [Go 编码规范](https://github.com/Unknwon/go-code-convention)
## 关于
包 goconfig 是一个易于使用,支持注释的 Go 语言配置文件解析器,该文件的书写格式和 Windows 下的 INI 文件一样。
配置文件由形为 `[section]` 的节构成,内部使用 `name:value``name=value` 这样的键值对;每行开头和尾部的空白符号都将被忽略;如果未指定任何节,则会默认放入名为 `DEFAULT` 的节当中;可以使用 “;” 或 “#” 来作为注释的开头,并可以放置于任意的单独一行中。
## 特性
- 简化流程,易于理解,更少出错。
- 提供与 Windows API 一模一样的操作方式。
- 支持读取递归节。
- 支持自增键名。
- 支持对注释的 **读****写** 操作,其它所有解析器都不支持!!!!
- 可以直接返回 bool, float64, int, int64 和 string 类型的值,如果使用 “Must” 开头的方法,则一定会返回这个类型的一个值而不返回错误,如果错误发生则会返回零值。
- 支持加载多个文件来重写值。
## 安装
go get github.com/Unknwon/goconfig
gopm get github.com/Unknwon/goconfig
## API 文档
[Go Walker](http://gowalker.org/github.com/Unknwon/goconfig).
## 示例
请查看 [conf.ini](testdata/conf.ini) 文件作为使用示例。
### 用例
- 函数 `LoadConfigFile` 加载一个或多个文件,然后返回一个类型为 `ConfigFile` 的变量。
- `GetValue` 可以简单的获取某个值。
-`Bool``Int``Int64` 这样的方法会直接返回指定类型的值。
-`Must` 开头的方法不会返回错误,但当错误发生时会返回零值。
- `SetValue` 可以设置某个值。
- `DeleteKey` 可以删除某个键。
- 最后,`SaveConfigFile` 可以保持您的配置到本地文件系统。
- 使用方法 `Reload` 可以重载您的配置文件。
## 更多信息
- 所有字符都是大小写敏感的!
## 参考信息
- [goconf](http://code.google.com/p/goconf/)
- [robfig/config](https://github.com/robfig/config)
- [Delete an item from a slice](https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/lYz8ftASMQ0)
## 授权许可
本项目采用 Apache v2 开源授权许可证,完整的授权说明已放置在 [LICENSE](LICENSE) 文件中。
// Copyright 2013 Unknwon
//
// 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 goconfig
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
"time"
)
// Read reads an io.Reader and returns a configuration representation.
// This representation can be queried with GetValue.
func (c *ConfigFile) read(reader io.Reader) (err error) {
buf := bufio.NewReader(reader)
// Handle BOM-UTF8.
// http://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding
mask, err := buf.Peek(3)
if err == nil && len(mask) >= 3 &&
mask[0] == 239 && mask[1] == 187 && mask[2] == 191 {
buf.Read(mask)
}
count := 1 // Counter for auto increment.
// Current section name.
section := DEFAULT_SECTION
var comments string
// Parse line-by-line
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
lineLengh := len(line) //[SWH|+]
if err != nil {
if err != io.EOF {
return err
}
// Reached end of file, if nothing to read then break,
// otherwise handle the last line.
if lineLengh == 0 {
break
}
}
// switch written for readability (not performance)
switch {
case lineLengh == 0: // Empty line
continue
case line[0] == '#' || line[0] == ';': // Comment
// Append comments
if len(comments) == 0 {
comments = line
} else {
comments += LineBreak + line
}
continue
case line[0] == '[' && line[lineLengh-1] == ']': // New sction.
// Get section name.
section = strings.TrimSpace(line[1 : lineLengh-1])
// Set section comments and empty if it has comments.
if len(comments) > 0 {
c.SetSectionComments(section, comments)
comments = ""
}
// Make section exist even though it does not have any key.
c.SetValue(section, " ", " ")
// Reset counter.
count = 1
continue
case section == "": // No section defined so far
return readError{ERR_BLANK_SECTION_NAME, line}
default: // Other alternatives
var (
i int
keyQuote string
key string
valQuote string
value string
)
//[SWH|+]:支持引号包围起来的字串
if line[0] == '"' {
if lineLengh >= 6 && line[0:3] == `"""` {
keyQuote = `"""`
} else {
keyQuote = `"`
}
} else if line[0] == '`' {
keyQuote = "`"
}
if keyQuote != "" {
qLen := len(keyQuote)
pos := strings.Index(line[qLen:], keyQuote)
if pos == -1 {
return readError{ERR_COULD_NOT_PARSE, line}
}
pos = pos + qLen
i = strings.IndexAny(line[pos:], "=:")
if i <= 0 {
return readError{ERR_COULD_NOT_PARSE, line}
}
i = i + pos
key = line[qLen:pos] //保留引号内的两端的空格
} else {
i = strings.IndexAny(line, "=:")
if i <= 0 {
return readError{ERR_COULD_NOT_PARSE, line}
}
key = strings.TrimSpace(line[0:i])
}
//[SWH|+];
// Check if it needs auto increment.
if key == "-" {
key = "#" + fmt.Sprint(count)
count++
}
//[SWH|+]:支持引号包围起来的字串
lineRight := strings.TrimSpace(line[i+1:])
lineRightLength := len(lineRight)
firstChar := ""
if lineRightLength >= 2 {
firstChar = lineRight[0:1]
}
if firstChar == "`" {
valQuote = "`"
} else if lineRightLength >= 6 && lineRight[0:3] == `"""` {
valQuote = `"""`
}
if valQuote != "" {
qLen := len(valQuote)
pos := strings.LastIndex(lineRight[qLen:], valQuote)
if pos == -1 {
return readError{ERR_COULD_NOT_PARSE, line}
}
pos = pos + qLen
value = lineRight[qLen:pos]
} else {
value = strings.TrimSpace(lineRight[0:])
}
//[SWH|+];
c.SetValue(section, key, value)
// Set key comments and empty if it has comments.
if len(comments) > 0 {
c.SetKeyComments(section, key, comments)
comments = ""
}
}
// Reached end of file.
if err == io.EOF {
break
}
}
return nil
}
// LoadFromData accepts raw data directly from memory
// and returns a new configuration representation.
func LoadFromData(data []byte) (c *ConfigFile, err error) {
// Save memory data to temporary file to support further operations.
tmpName := path.Join(os.TempDir(), "goconfig", 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
}
c = newConfigFile([]string{tmpName})
err = c.read(bytes.NewBuffer(data))
return c, err
}
func (c *ConfigFile) loadFile(fileName string) (err error) {
f, err := os.Open(fileName)
if err != nil {
return err
}
defer f.Close()
return c.read(f)
}
// LoadConfigFile reads a file and returns a new configuration representation.
// This representation can be queried with GetValue.
func LoadConfigFile(fileName string, moreFiles ...string) (c *ConfigFile, err error) {
// Append files' name together.
fileNames := make([]string, 1, len(moreFiles)+1)
fileNames[0] = fileName
if len(moreFiles) > 0 {
fileNames = append(fileNames, moreFiles...)
}
c = newConfigFile(fileNames)
for _, name := range fileNames {
if err = c.loadFile(name); err != nil {
return nil, err
}
}
return c, nil
}
// Reload reloads configuration file in case it has changes.
func (c *ConfigFile) Reload() (err error) {
var cfg *ConfigFile
if len(c.fileNames) == 1 {
cfg, err = LoadConfigFile(c.fileNames[0])
} else {
cfg, err = LoadConfigFile(c.fileNames[0], c.fileNames[1:]...)
}
if err == nil {
*c = *cfg
}
return err
}
// AppendFiles appends more files to ConfigFile and reload automatically.
func (c *ConfigFile) AppendFiles(files ...string) error {
c.fileNames = append(c.fileNames, files...)
return c.Reload()
}
// readError occurs when read configuration file with wrong format.
type readError struct {
Reason ParseError
Content string // Line content
}
// Error implement Error interface.
func (err readError) Error() string {
switch err.Reason {
case ERR_BLANK_SECTION_NAME:
return "empty section name not allowed"
case ERR_COULD_NOT_PARSE:
return fmt.Sprintf("could not parse line: %s", string(err.Content))
}
return "invalid read error"
}
; Google
google = www.google.com
search = http://%(google)s
; Here are Comments
; Second line
[Demo]
# This symbol can also make this line to be comments
key1 = Let's us goconfig!!!
key2 = test data
key3 = this is based on key2:%(key2)s
quote = "special case for quote
"key:1" = This is the value of "key:1"
"key:2=key:1" = this is based on "key:2=key:1" => %(key:1)s
中国 = China
chinese-var = hello %(中国)s!
array_key = 1,2,3,4,5
[What's this?]
; Not Enough Comments!!
name = try one more value ^-^
empty_value =
[url]
google_fake = www.google.fake
google_url = http://%(google_fake)s
[parent]
name = john
relation = father
sex = male
age = 32
money = 1.25
[parent.child]
age = 3
married = true
[parent.child.child]
; Auto increment by setting key to "-"
[auto increment]
- = hello
- = go
- = config
; Google
google = www.google.com
search = http://%(google)s
; Here are Comments
; Second line
[Demo]
# This symbol can also make this line to be comments
key1 = Let's us goconfig!!!
key2 = rewrite this key of conf.ini
key3 = this is based on key2:%(key2)s
"key:1" = This is the value of "key:1"
"""key:2"""="""this is based on "key:1" => `%(key:1)s`"""
[What's this?]
; Not Enough Comments!!
name = try one more value ^-^
[parent]
name = john
relation = father
sex = male
age = 32
[parent.child]
age = 3
[parent.child.child]
; Auto increment by setting key to "-"
[auto increment]
- = hello
- = go
- = config
[new section]
key1 = conf.ini does not have this key
\ No newline at end of file
; Google
google = www.google.com
search = http://%(google)s
; Here are Comments
; Second line
[Demo]
# This symbol can also make this line to be comments
key1 = Let's us goconfig!!!
key2 = rewrite this key of conf.ini
key3 = this is based on key2:%(key2)s
quote = "special case for quote
`key:1` = This is the value of "key:1"
`key:2=key:1` = this is based on "key:2=key:1" => %(key:1)s
中国 = China
chinese-var = hello %(中国)s!
array_key = 1,2,3,4,5
`key:2` = """this is based on "key:1" => `%(key:1)s`"""
[What's this?]
; Not Enough Comments!!
name = try one more value ^-^
empty_value =
[url]
google_fake = www.google.fake
google_url = http://%(google_fake)s
[parent]
name = john
relation = father
sex = male
age = 32
money = 1.25
[parent.child]
age = 3
married = true
[parent.child.child]
; Auto increment by setting key to "-"
[auto increment]
- = hello
- = go
- = config
[new section]
key1 = conf.ini does not have this key
// Copyright 2013 Unknwon
//
// 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 goconfig
import (
"bytes"
"os"
"strings"
)
// Write spaces around "=" to look better.
var PrettyFormat = true
// SaveConfigFile writes configuration file to local file system
func SaveConfigFile(c *ConfigFile, filename string) (err error) {
// Write configuration file by filename.
var f *os.File
if f, err = os.Create(filename); err != nil {
return err
}
equalSign := "="
if PrettyFormat {
equalSign = " = "
}
buf := bytes.NewBuffer(nil)
for _, section := range c.sectionList {
// Write section comments.
if len(c.GetSectionComments(section)) > 0 {
if _, err = buf.WriteString(c.GetSectionComments(section) + LineBreak); err != nil {
return err
}
}
if section != DEFAULT_SECTION {
// Write section name.
if _, err = buf.WriteString("[" + section + "]" + LineBreak); err != nil {
return err
}
}
for _, key := range c.keyList[section] {
if key != " " {
// Write key comments.
if len(c.GetKeyComments(section, key)) > 0 {
if _, err = buf.WriteString(c.GetKeyComments(section, key) + LineBreak); err != nil {
return err
}
}
keyName := key
// Check if it's auto increment.
if keyName[0] == '#' {
keyName = "-"
}
//[SWH|+]:支持键名包含等号和冒号
if strings.Contains(keyName, `=`) || strings.Contains(keyName, `:`) {
if strings.Contains(keyName, "`") {
if strings.Contains(keyName, `"`) {
keyName = `"""` + keyName + `"""`
} else {
keyName = `"` + keyName + `"`
}
} else {
keyName = "`" + keyName + "`"
}
}
value := c.data[section][key]
// In case key value contains "`" or "\"".
if strings.Contains(value, "`") {
if strings.Contains(value, `"`) {
value = `"""` + value + `"""`
} else {
value = `"` + value + `"`
}
}
// Write key and value.
if _, err = buf.WriteString(keyName + equalSign + value + LineBreak); err != nil {
return err
}
}
}
// Put a line between sections.
if _, err = buf.WriteString(LineBreak); err != nil {
return err
}
}
if _, err = buf.WriteTo(f); err != nil {
return err
}
return f.Close()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment