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

Commit

Permalink
Merge pull request #8 from philips/bump-third-party
Browse files Browse the repository at this point in the history
bump the third party libraries
  • Loading branch information
xiang90 committed Aug 19, 2013
2 parents 689b265 + 572150b commit dac6f5c
Show file tree
Hide file tree
Showing 20 changed files with 322 additions and 136 deletions.
1 change: 1 addition & 0 deletions third_party/deps
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
packages="
bitbucket.org/kardianos/osext
github.com/coreos/go-etcd/etcd
github.com/coreos/etcd/error
github.com/coreos/etcd/store
github.com/ccding/go-logging/logging
github.com/ccding/go-config-reader/config
Expand Down
142 changes: 121 additions & 21 deletions third_party/github.com/ccding/go-config-reader/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,62 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

package config

import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
)

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

func Read(filename string) (map[string]string, error) {
var res = map[string]string{}
in, err := os.Open(filename)
// Config struct constructs a new configuration handler.
type Config struct {
filename string
config map[string]map[string]string
}

// NewConfig function cnstructs a new Config struct with filename. You have to
// call Read() function to let it read from the file. Otherwise you will get
// empty string (i.e., "") when you are calling Get() function. Another usage
// is that you call NewConfig() function and then call Add()/Set() function to
// add new key-values to the configuration. Finally you can call Write()
// function to write the new configuration to the file.
func NewConfig(filename string) *Config {
c := new(Config)
c.filename = filename
c.config = make(map[string]map[string]string)
return c
}

// Filename function returns the filename of the configuration.
func (c *Config) Filename() string {
return c.filename
}

// SetFilename function sets the filename of the configuration.
func (c *Config) SetFilename(filename string) {
c.filename = filename
}

// Reset function reset the map in the configuration.
func (c *Config) Reset() {
c.config = make(map[string]map[string]string)
}

// Read function reads configurations from the file defined in
// Config.filename.
func (c *Config) Read() error {
in, err := os.Open(c.filename)
if err != nil {
return res, err
return err
}
defer in.Close()
scanner := bufio.NewScanner(in)
line := ""
section := ""
Expand All @@ -39,9 +77,9 @@ func Read(filename string) (map[string]string, error) {
continue
}
if line == "" {
sec := checkSection(scanner.Text())
if sec != "" {
section = sec + "."
sec, ok := checkSection(scanner.Text())
if ok {
section = sec
continue
}
}
Expand All @@ -53,41 +91,103 @@ func Read(filename string) (map[string]string, error) {
line = line[:len(line)-1]
continue
}
key, value, err := checkLine(line)
if err != nil {
return res, errors.New("WRONG: " + line)
key, value, ok := checkLine(line)
if !ok {
return errors.New("WRONG: " + line)
}
res[section+key] = value
c.Set(section, key, value)
line = ""
}
in.Close()
return res, nil
return nil
}

// Get function returns the value of a key in the configuration. If the key
// does not exist, it returns empty string (i.e., "").
func (c *Config) Get(section string, key string) string {
value, ok := c.config[section][key]
if !ok {
return ""
}
return value
}

func checkSection(line string) string {
// Set function updates the value of a key in the configuration. Function
// Set() is exactly the same as function Add().
func (c *Config) Set(section string, key string, value string) {
_, ok := c.config[section]
if !ok {
c.config[section] = make(map[string]string)
}
c.config[section][key] = value
}

// Add function adds a new key to the configuration. Function Add() is exactly
// the same as function Set().
func (c *Config) Add(section string, key string, value string) {
c.Set(section, key, value)
}

// Del function deletes a key from the configuration.
func (c *Config) Del(section string, key string) {
_, ok := c.config[section]
if ok {
delete(c.config[section], key)
if len(c.config[section]) == 0 {
delete(c.config, section)
}
}
}

// Write function writes the updated configuration back.
func (c *Config) Write() error {
return nil
}

// WriteTo function writes the configuration to a new file. This function
// re-organizes the configuration and deletes all the comments.
func (c *Config) WriteTo(filename string) error {
content := ""
for k, v := range c.config {
format := "%v = %v\n"
if k != "" {
content += fmt.Sprintf("[%v]\n", k)
format = "\t" + format
}
for key, value := range v {
content += fmt.Sprintf(format, key, value)
}
}
return ioutil.WriteFile(filename, []byte(content), 0644)
}

// To check this line is a section or not. If it is not a section, it returns
// "".
func checkSection(line string) (string, bool) {
line = strings.TrimSpace(line)
lineLen := len(line)
if lineLen < 2 {
return ""
return "", false
}
if line[0] == '[' && line[lineLen-1] == ']' {
return line[1 : lineLen-1]
return line[1 : lineLen-1], true
}
return ""
return "", false
}

func checkLine(line string) (string, string, error) {
// To check this line is a valid key-value pair or not.
func checkLine(line string) (string, string, bool) {
key := ""
value := ""
sp := strings.SplitN(line, "=", 2)
if len(sp) != 2 {
return key, value, errors.New("WRONG: " + line)
return key, value, false
}
key = strings.TrimSpace(sp[0])
value = strings.TrimSpace(sp[1])
return key, value, nil
return key, value, true
}

// To check this line is a whole line comment or not.
func checkComment(line string) bool {
line = strings.TrimSpace(line)
for p := range commentPrefix {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

package logging

// Logln receives log request from the client. The request includes a set of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

package logging

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

package logging

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

package logging

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

// This file defines GetGoId function, which is used to get the id of the
// current goroutine. More details about this function are availeble in the
// runtime.c file of golang source code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

package logging

// Level is the type of level.
Expand Down
43 changes: 21 additions & 22 deletions third_party/github.com/ccding/go-logging/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ func RichLogger(name string) (*Logger, error) {
func FileLogger(name string, level Level, format string, timeFormat string, file string, sync bool) (*Logger, error) {
out, err := os.Create(file)
if err != nil {
return new(Logger), err
return nil, err
}
logger, err := createLogger(name, level, format, timeFormat, out, sync)
if err == nil {
logger.fd = out
return logger, nil
} else {
return nil, err
}
return logger, err
}

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

// WriterLogger creates a new logger from a configuration file
func ConfigLogger(filename string) (*Logger, error) {
conf, err := config.Read(filename)
conf := config.NewConfig(filename)
err := conf.Read()
if err != nil {
return new(Logger), err
}
ok := true
name, ok := conf["name"]
if !ok {
name = ""
return nil, err
}
slevel, ok := conf["level"]
if !ok {
name := conf.Get("", "name")
slevel := conf.Get("", "level")
if slevel == "" {
slevel = "0"
}
l, err := strconv.Atoi(slevel)
if err != nil {
return new(Logger), err
return nil, err
}
level := Level(l)
format, ok := conf["format"]
if !ok {
format := conf.Get("", "format")
if format == "" {
format = BasicFormat
}
timeFormat, ok := conf["timeFormat"]
if !ok {
timeFormat := conf.Get("", "timeFormat")
if timeFormat == "" {
timeFormat = DefaultTimeFormat
}
ssync, ok := conf["sync"]
if !ok {
ssync := conf.Get("", "sync")
if ssync == "" {
ssync = "0"
}
file, ok := conf["file"]
if !ok {
file := conf.Get("", "file")
if file == "" {
file = DefaultFileName
}
sync := true
Expand All @@ -155,7 +154,7 @@ func ConfigLogger(filename string) (*Logger, error) {
} else if ssync == "1" {
sync = true
} else {
return new(Logger), err
return nil, err
}
return FileLogger(name, level, format, timeFormat, file, sync)
}
Expand All @@ -166,7 +165,7 @@ func createLogger(name string, level Level, format string, timeFormat string, ou

err := logger.parseFormat(format)
if err != nil {
return logger, err
return nil, err
}

// asign values to logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

package logging

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

package logging

// request struct stores the logger request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// author: Cong Ding <dinggnu@gmail.com>
//

package logging

import (
Expand Down
Loading

0 comments on commit dac6f5c

Please sign in to comment.