init commit

This commit is contained in:
Fabio Scotto di Santolo
2017-10-06 21:33:29 +02:00
parent 8e232e4ff6
commit 7923305e1a
5 changed files with 364 additions and 1 deletions

51
parser.go Normal file
View File

@@ -0,0 +1,51 @@
package properties
import (
"bufio"
"io"
"os"
"path/filepath"
"strings"
)
// ParseFunction -- type
type ParseFunction func(string, string) (map[string]string, error)
// Default parse method for parsing key - value file
func defaultParse(path, fileName string) (m map[string]string, err error) {
absolutePathFile, err := filepath.Abs(filepath.Join(path, fileName))
if err != nil {
return nil, err
}
file, err := os.Open(absolutePathFile)
if err != nil {
return nil, err
}
defer file.Close()
m = make(map[string]string)
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
// check if the line has = sign
// and process the line. Ignore the rest.
if equal := strings.Index(line, "="); equal >= 0 {
if key := strings.TrimSpace(line[:equal]); len(key) > 0 {
value := ""
if len(line) > equal {
value = strings.TrimSpace(strings.Replace(line[equal+1:], "\"", "", -1))
}
// assign the values map
m[key] = value
}
}
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
}
return m, nil
}