Skip to content
This repository was archived by the owner on Jun 4, 2019. It is now read-only.

Commit dac6f5c

Browse files
author
Xiang Li
committed
Merge pull request #8 from philips/bump-third-party
bump the third party libraries
2 parents 689b265 + 572150b commit dac6f5c

20 files changed

Lines changed: 322 additions & 136 deletions

File tree

third_party/deps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
packages="
22
bitbucket.org/kardianos/osext
33
github.com/coreos/go-etcd/etcd
4+
github.com/coreos/etcd/error
45
github.com/coreos/etcd/store
56
github.com/ccding/go-logging/logging
67
github.com/ccding/go-config-reader/config

third_party/github.com/ccding/go-config-reader/config/config.go

Lines changed: 121 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,62 @@
1313
// limitations under the License.
1414
//
1515
// author: Cong Ding <dinggnu@gmail.com>
16-
//
16+
1717
package config
1818

1919
import (
2020
"bufio"
2121
"errors"
22+
"fmt"
23+
"io/ioutil"
2224
"os"
2325
"strings"
2426
)
2527

2628
var commentPrefix = []string{"//", "#", ";"}
2729

28-
func Read(filename string) (map[string]string, error) {
29-
var res = map[string]string{}
30-
in, err := os.Open(filename)
30+
// Config struct constructs a new configuration handler.
31+
type Config struct {
32+
filename string
33+
config map[string]map[string]string
34+
}
35+
36+
// NewConfig function cnstructs a new Config struct with filename. You have to
37+
// call Read() function to let it read from the file. Otherwise you will get
38+
// empty string (i.e., "") when you are calling Get() function. Another usage
39+
// is that you call NewConfig() function and then call Add()/Set() function to
40+
// add new key-values to the configuration. Finally you can call Write()
41+
// function to write the new configuration to the file.
42+
func NewConfig(filename string) *Config {
43+
c := new(Config)
44+
c.filename = filename
45+
c.config = make(map[string]map[string]string)
46+
return c
47+
}
48+
49+
// Filename function returns the filename of the configuration.
50+
func (c *Config) Filename() string {
51+
return c.filename
52+
}
53+
54+
// SetFilename function sets the filename of the configuration.
55+
func (c *Config) SetFilename(filename string) {
56+
c.filename = filename
57+
}
58+
59+
// Reset function reset the map in the configuration.
60+
func (c *Config) Reset() {
61+
c.config = make(map[string]map[string]string)
62+
}
63+
64+
// Read function reads configurations from the file defined in
65+
// Config.filename.
66+
func (c *Config) Read() error {
67+
in, err := os.Open(c.filename)
3168
if err != nil {
32-
return res, err
69+
return err
3370
}
71+
defer in.Close()
3472
scanner := bufio.NewScanner(in)
3573
line := ""
3674
section := ""
@@ -39,9 +77,9 @@ func Read(filename string) (map[string]string, error) {
3977
continue
4078
}
4179
if line == "" {
42-
sec := checkSection(scanner.Text())
43-
if sec != "" {
44-
section = sec + "."
80+
sec, ok := checkSection(scanner.Text())
81+
if ok {
82+
section = sec
4583
continue
4684
}
4785
}
@@ -53,41 +91,103 @@ func Read(filename string) (map[string]string, error) {
5391
line = line[:len(line)-1]
5492
continue
5593
}
56-
key, value, err := checkLine(line)
57-
if err != nil {
58-
return res, errors.New("WRONG: " + line)
94+
key, value, ok := checkLine(line)
95+
if !ok {
96+
return errors.New("WRONG: " + line)
5997
}
60-
res[section+key] = value
98+
c.Set(section, key, value)
6199
line = ""
62100
}
63-
in.Close()
64-
return res, nil
101+
return nil
102+
}
103+
104+
// Get function returns the value of a key in the configuration. If the key
105+
// does not exist, it returns empty string (i.e., "").
106+
func (c *Config) Get(section string, key string) string {
107+
value, ok := c.config[section][key]
108+
if !ok {
109+
return ""
110+
}
111+
return value
65112
}
66113

67-
func checkSection(line string) string {
114+
// Set function updates the value of a key in the configuration. Function
115+
// Set() is exactly the same as function Add().
116+
func (c *Config) Set(section string, key string, value string) {
117+
_, ok := c.config[section]
118+
if !ok {
119+
c.config[section] = make(map[string]string)
120+
}
121+
c.config[section][key] = value
122+
}
123+
124+
// Add function adds a new key to the configuration. Function Add() is exactly
125+
// the same as function Set().
126+
func (c *Config) Add(section string, key string, value string) {
127+
c.Set(section, key, value)
128+
}
129+
130+
// Del function deletes a key from the configuration.
131+
func (c *Config) Del(section string, key string) {
132+
_, ok := c.config[section]
133+
if ok {
134+
delete(c.config[section], key)
135+
if len(c.config[section]) == 0 {
136+
delete(c.config, section)
137+
}
138+
}
139+
}
140+
141+
// Write function writes the updated configuration back.
142+
func (c *Config) Write() error {
143+
return nil
144+
}
145+
146+
// WriteTo function writes the configuration to a new file. This function
147+
// re-organizes the configuration and deletes all the comments.
148+
func (c *Config) WriteTo(filename string) error {
149+
content := ""
150+
for k, v := range c.config {
151+
format := "%v = %v\n"
152+
if k != "" {
153+
content += fmt.Sprintf("[%v]\n", k)
154+
format = "\t" + format
155+
}
156+
for key, value := range v {
157+
content += fmt.Sprintf(format, key, value)
158+
}
159+
}
160+
return ioutil.WriteFile(filename, []byte(content), 0644)
161+
}
162+
163+
// To check this line is a section or not. If it is not a section, it returns
164+
// "".
165+
func checkSection(line string) (string, bool) {
68166
line = strings.TrimSpace(line)
69167
lineLen := len(line)
70168
if lineLen < 2 {
71-
return ""
169+
return "", false
72170
}
73171
if line[0] == '[' && line[lineLen-1] == ']' {
74-
return line[1 : lineLen-1]
172+
return line[1 : lineLen-1], true
75173
}
76-
return ""
174+
return "", false
77175
}
78176

79-
func checkLine(line string) (string, string, error) {
177+
// To check this line is a valid key-value pair or not.
178+
func checkLine(line string) (string, string, bool) {
80179
key := ""
81180
value := ""
82181
sp := strings.SplitN(line, "=", 2)
83182
if len(sp) != 2 {
84-
return key, value, errors.New("WRONG: " + line)
183+
return key, value, false
85184
}
86185
key = strings.TrimSpace(sp[0])
87186
value = strings.TrimSpace(sp[1])
88-
return key, value, nil
187+
return key, value, true
89188
}
90189

190+
// To check this line is a whole line comment or not.
91191
func checkComment(line string) bool {
92192
line = strings.TrimSpace(line)
93193
for p := range commentPrefix {

third_party/github.com/ccding/go-logging/logging/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515
// author: Cong Ding <dinggnu@gmail.com>
16-
//
16+
1717
package logging
1818

1919
// Logln receives log request from the client. The request includes a set of

third_party/github.com/ccding/go-logging/logging/fields.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515
// author: Cong Ding <dinggnu@gmail.com>
16-
//
16+
1717
package logging
1818

1919
import (

third_party/github.com/ccding/go-logging/logging/fields_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515
// author: Cong Ding <dinggnu@gmail.com>
16-
//
16+
1717
package logging
1818

1919
import (

third_party/github.com/ccding/go-logging/logging/formater.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515
// author: Cong Ding <dinggnu@gmail.com>
16-
//
16+
1717
package logging
1818

1919
import (

third_party/github.com/ccding/go-logging/logging/get_go_id.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515
// author: Cong Ding <dinggnu@gmail.com>
16-
//
16+
1717
// This file defines GetGoId function, which is used to get the id of the
1818
// current goroutine. More details about this function are availeble in the
1919
// runtime.c file of golang source code.

third_party/github.com/ccding/go-logging/logging/level.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515
// author: Cong Ding <dinggnu@gmail.com>
16-
//
16+
1717
package logging
1818

1919
// Level is the type of level.

third_party/github.com/ccding/go-logging/logging/logging.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,15 @@ func RichLogger(name string) (*Logger, error) {
9999
func FileLogger(name string, level Level, format string, timeFormat string, file string, sync bool) (*Logger, error) {
100100
out, err := os.Create(file)
101101
if err != nil {
102-
return new(Logger), err
102+
return nil, err
103103
}
104104
logger, err := createLogger(name, level, format, timeFormat, out, sync)
105105
if err == nil {
106106
logger.fd = out
107+
return logger, nil
108+
} else {
109+
return nil, err
107110
}
108-
return logger, err
109111
}
110112

111113
// WriterLogger creates a new logger with a writer
@@ -115,38 +117,35 @@ func WriterLogger(name string, level Level, format string, timeFormat string, ou
115117

116118
// WriterLogger creates a new logger from a configuration file
117119
func ConfigLogger(filename string) (*Logger, error) {
118-
conf, err := config.Read(filename)
120+
conf := config.NewConfig(filename)
121+
err := conf.Read()
119122
if err != nil {
120-
return new(Logger), err
121-
}
122-
ok := true
123-
name, ok := conf["name"]
124-
if !ok {
125-
name = ""
123+
return nil, err
126124
}
127-
slevel, ok := conf["level"]
128-
if !ok {
125+
name := conf.Get("", "name")
126+
slevel := conf.Get("", "level")
127+
if slevel == "" {
129128
slevel = "0"
130129
}
131130
l, err := strconv.Atoi(slevel)
132131
if err != nil {
133-
return new(Logger), err
132+
return nil, err
134133
}
135134
level := Level(l)
136-
format, ok := conf["format"]
137-
if !ok {
135+
format := conf.Get("", "format")
136+
if format == "" {
138137
format = BasicFormat
139138
}
140-
timeFormat, ok := conf["timeFormat"]
141-
if !ok {
139+
timeFormat := conf.Get("", "timeFormat")
140+
if timeFormat == "" {
142141
timeFormat = DefaultTimeFormat
143142
}
144-
ssync, ok := conf["sync"]
145-
if !ok {
143+
ssync := conf.Get("", "sync")
144+
if ssync == "" {
146145
ssync = "0"
147146
}
148-
file, ok := conf["file"]
149-
if !ok {
147+
file := conf.Get("", "file")
148+
if file == "" {
150149
file = DefaultFileName
151150
}
152151
sync := true
@@ -155,7 +154,7 @@ func ConfigLogger(filename string) (*Logger, error) {
155154
} else if ssync == "1" {
156155
sync = true
157156
} else {
158-
return new(Logger), err
157+
return nil, err
159158
}
160159
return FileLogger(name, level, format, timeFormat, file, sync)
161160
}
@@ -166,7 +165,7 @@ func createLogger(name string, level Level, format string, timeFormat string, ou
166165

167166
err := logger.parseFormat(format)
168167
if err != nil {
169-
return logger, err
168+
return nil, err
170169
}
171170

172171
// asign values to logger

third_party/github.com/ccding/go-logging/logging/logging_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515
// author: Cong Ding <dinggnu@gmail.com>
16-
//
16+
1717
package logging
1818

1919
import (

0 commit comments

Comments
 (0)