Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add charset flag #29

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add error log on charset convert fail.
  • Loading branch information
Daniel Machado Kraus committed Aug 31, 2017
commit 6ec07bb0b29a9c68a9e0329b6ff32f056a198931
18 changes: 10 additions & 8 deletions envplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ func (h *Handler) parse(file string) error {
errors = append(errors, Log(ERROR, "Tried to escape '%s', but was no escape sequence", content))
}

data, err := fromCharset(escaped, h.Charset)
encodedValue, err := convertToCharset(escaped, h.Charset)
if err != nil {
errors = append(errors, Log(ERROR, "Tried to convert string '%s' to charset '%s' but an error ocourred: %v", encodedValue, h.Charset, err))
return value
}
return data
return encodedValue
}

if !keyDefined {
Expand All @@ -119,11 +120,12 @@ func (h *Handler) parse(file string) error {
if len(esc) > 0 {
value = esc[:len(esc)/2] + value
}
data, err := fromCharset(value, h.Charset)
encodedValue, err := convertToCharset(value, h.Charset)
if err != nil {
errors = append(errors, Log(ERROR, "Tried to convert string '%s' to charset '%s' but an error ocourred: %v", encodedValue, h.Charset, err))
return value
}
return string(data)
return encodedValue
})

if h.DryRun {
Expand Down Expand Up @@ -166,14 +168,14 @@ func saveFile(file string, parsed string, cs string) error {
return nil
}

func fromCharset(data string, cs string) (string, error) {
if cs == "" {
func convertToCharset(data string, charSet string) (string, error) {
if charSet == "" {
return data, nil
}

buf := new(bytes.Buffer)
w, err := charset.NewWriter(cs, buf)
if err != nil {
w, err := charset.NewWriter(charSet, buf)
if err != nil {
return "", err
}
fmt.Fprintf(w, data)
Expand Down