Fix store function
This commit is contained in:
committed by
fscotto
parent
1023d72879
commit
8ef3bd1643
1
.gitignore
vendored
1
.gitignore
vendored
@@ -14,3 +14,4 @@
|
||||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
||||
.glide/
|
||||
.vscode/
|
||||
.idea/
|
||||
79
store.go
79
store.go
@@ -1,10 +1,13 @@
|
||||
package properties
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// StoringFunction -- type
|
||||
@@ -15,39 +18,69 @@ func defaultStore(p Properties) (*os.File, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
file, err := os.OpenFile(absolutePathFile, os.O_CREATE, 0644)
|
||||
file, err := os.OpenFile(absolutePathFile, os.O_CREATE|os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Write this file
|
||||
defer file.Close()
|
||||
for _, key := range p.GetProperties() {
|
||||
value, err := p.Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
row, err := buildRow(key, value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := file.Write([]byte(row + "\n")); err != nil {
|
||||
writer := bufio.NewWriter(file)
|
||||
for _, pair := range p.values {
|
||||
line := fmt.Sprintf("%s=%s\n", escape(pair.First, true), escape(pair.Second, false))
|
||||
log.Print(line)
|
||||
if _, err := writer.Write([]byte(line)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := writer.Flush(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func buildRow(key, value string) (row string, err error) {
|
||||
if key == "" || value == "" {
|
||||
return "", errors.New("Key or Value param is a empty string")
|
||||
}
|
||||
// escape returns a string that is safe to use as either a key or value in a
|
||||
// property file. Whitespace characters, key separators, and comment markers
|
||||
// should always be escaped.
|
||||
func escape(s string, key bool) string {
|
||||
|
||||
newKey := strings.Replace(key, "\"", "", -1)
|
||||
newValue := strings.Join([]string{"\"", value, "\""}, "")
|
||||
row = strings.Join([]string{newKey, newValue}, "=")
|
||||
return strings.TrimSpace(row), nil
|
||||
leading := true
|
||||
var buf bytes.Buffer
|
||||
for _, ch := range s {
|
||||
wasSpace := false
|
||||
if ch == '\t' {
|
||||
buf.WriteString(`\t`)
|
||||
} else if ch == '\n' {
|
||||
buf.WriteString(`\n`)
|
||||
} else if ch == '\r' {
|
||||
buf.WriteString(`\r`)
|
||||
} else if ch == '\f' {
|
||||
buf.WriteString(`\f`)
|
||||
} else if ch == ' ' {
|
||||
if key || leading {
|
||||
buf.WriteString(`\ `)
|
||||
wasSpace = true
|
||||
} else {
|
||||
buf.WriteRune(ch)
|
||||
}
|
||||
} else if ch == ':' {
|
||||
buf.WriteString(`\:`)
|
||||
} else if ch == '=' {
|
||||
buf.WriteString(`\=`)
|
||||
} else if ch == '#' {
|
||||
buf.WriteString(`\#`)
|
||||
} else if ch == '!' {
|
||||
buf.WriteString(`\!`)
|
||||
} else if !unicode.IsPrint(ch) || ch > 126 {
|
||||
buf.WriteString(fmt.Sprintf(`\u%04x`, ch))
|
||||
} else {
|
||||
buf.WriteRune(ch)
|
||||
}
|
||||
|
||||
if !wasSpace {
|
||||
leading = false
|
||||
}
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user