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
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Optionally, `ep` can:
* dry-run (`-d` flag): output to stdout instead of replacing values inline
* strict (`-s` flag): refuse to fallback to default values
* verbose (`-v` flag): be verbose about it's operations
* charset (`-c` flag): specify a custom charset to replace variables. Example: `/usr/local/bin/ep -c iso8859-1 *.conf`

`ep` can also `exec()` another command by passing

Expand Down
4 changes: 4 additions & 0 deletions bin/ep.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/kreuzwerker/envplate"
"github.com/spf13/cobra"
"github.com/yawn/doubledash"
_ "github.com/paulrosania/go-charset/data"
)

var (
Expand All @@ -27,6 +28,7 @@ func main() {
dryRun *bool
strict *bool
verbose *bool
charset *string
)

root := &cobra.Command{
Expand All @@ -41,6 +43,7 @@ func main() {
Backup: *backup,
DryRun: *dryRun,
Strict: *strict,
Charset: *charset,
}

if err := h.Apply(args); err != nil {
Expand All @@ -67,6 +70,7 @@ func main() {
dryRun = root.Flags().BoolP("dry-run", "d", false, "Dry-run - output templates to stdout instead of inline replacement")
strict = root.Flags().BoolP("strict", "s", false, "Strict-mode - fail when falling back on defaults")
verbose = root.Flags().BoolP("verbose", "v", false, "Verbose logging")
charset = root.Flags().StringP("charset", "c", "", "Output charset")

if err := root.Execute(); err != nil {
log.Fatalf("Failed to start the application: %v", err)
Expand Down
69 changes: 50 additions & 19 deletions envplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import (
"os"
"path/filepath"
"regexp"
"fmt"
"bytes"

"github.com/yawn/envmap"
)
"github.com/paulrosania/go-charset/charset"
)

const (
noDefaultDefined = ""
notAnEscapeSequence = ""
)

type Handler struct {
Backup bool
DryRun bool
Strict bool
Backup bool
DryRun bool
Strict bool
Charset string
}

var exp = regexp.MustCompile(`(\\*)\$\{(.+?)(?:(\:\-)(.*?))?\}`)
Expand Down Expand Up @@ -62,7 +66,7 @@ func (h *Handler) parse(file string) error {

env := envmap.Import()
content, err := ioutil.ReadFile(file)

if err != nil {
return Log(ERROR, "Cannot open %s: %v", file, err)
}
Expand All @@ -86,8 +90,12 @@ func (h *Handler) parse(file string) error {
errors = append(errors, Log(ERROR, "Tried to escape '%s', but was no escape sequence", content))
}

return escaped

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 encodedValue
}

if !keyDefined {
Expand All @@ -112,9 +120,12 @@ func (h *Handler) parse(file string) error {
if len(esc) > 0 {
value = esc[:len(esc)/2] + value
}

return value

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 encodedValue
})

if h.DryRun {
Expand All @@ -131,25 +142,45 @@ func (h *Handler) parse(file string) error {
}

}

mode, err := filemode(file)

if err != nil {
return err
}

if err := ioutil.WriteFile(file, []byte(parsed), mode); err != nil {
if err := saveFile(file, parsed, h.Charset); err != nil {
return err
}

}

if len(errors) > 0 {
return errors[0]
}

return nil
}


func saveFile(file string, parsed string, cs string) error {
mode, err := filemode(file)
if err != nil {
return err
}

if err := ioutil.WriteFile(file, []byte(parsed), mode); err != nil {
return err
}

return nil
}

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

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

func capture(s string) (esc, key, sep, def string) {
Expand Down
11 changes: 11 additions & 0 deletions vendor/github.com/paulrosania/go-charset/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/paulrosania/go-charset/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions vendor/github.com/paulrosania/go-charset/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions vendor/github.com/paulrosania/go-charset/charset/ascii.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading