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
|
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
||||||
.glide/
|
.glide/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
.idea/
|
||||||
79
store.go
79
store.go
@@ -1,10 +1,13 @@
|
|||||||
package properties
|
package properties
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StoringFunction -- type
|
// StoringFunction -- type
|
||||||
@@ -15,39 +18,69 @@ func defaultStore(p Properties) (*os.File, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write this file
|
// Write this file
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
for _, key := range p.GetProperties() {
|
writer := bufio.NewWriter(file)
|
||||||
value, err := p.Get(key)
|
for _, pair := range p.values {
|
||||||
if err != nil {
|
line := fmt.Sprintf("%s=%s\n", escape(pair.First, true), escape(pair.Second, false))
|
||||||
return nil, err
|
log.Print(line)
|
||||||
}
|
if _, err := writer.Write([]byte(line)); err != nil {
|
||||||
|
|
||||||
row, err := buildRow(key, value)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := file.Write([]byte(row + "\n")); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := writer.Flush(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return file, nil
|
return file, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildRow(key, value string) (row string, err error) {
|
// escape returns a string that is safe to use as either a key or value in a
|
||||||
if key == "" || value == "" {
|
// property file. Whitespace characters, key separators, and comment markers
|
||||||
return "", errors.New("Key or Value param is a empty string")
|
// should always be escaped.
|
||||||
}
|
func escape(s string, key bool) string {
|
||||||
|
|
||||||
newKey := strings.Replace(key, "\"", "", -1)
|
leading := true
|
||||||
newValue := strings.Join([]string{"\"", value, "\""}, "")
|
var buf bytes.Buffer
|
||||||
row = strings.Join([]string{newKey, newValue}, "=")
|
for _, ch := range s {
|
||||||
return strings.TrimSpace(row), nil
|
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