Skip to content

Commit

Permalink
Merge pull request #30 from ghostiam/feature/copy_binary
Browse files Browse the repository at this point in the history
allow copy binary file (tmrts/boilr#68 and …
  • Loading branch information
Ilyes512 authored Feb 6, 2021
2 parents b7087c6 + c5d5fd2 commit cb5602e
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package template

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"text/template"
"unicode/utf8"

"github.com/Masterminds/sprig"

"github.com/Masterminds/sprig"
"github.com/ryanuber/go-glob"
Expand Down Expand Up @@ -193,6 +198,27 @@ func (t *dirTemplate) Execute(dirPrefix string) error {
}
defer f.Close()

// Open original file and check if a binary file
origF, err := os.Open(filename)
if err != nil {
return err
}
defer origF.Close()

isBin, err := isBinary(origF)
if err != nil {
return err
}
if isBin {
_, err = origF.Seek(io.SeekStart, 0)
if err != nil {
return err
}

_, err = io.Copy(f, origF)
return err
}

defer func(fname string) {
contents, err := ioutil.ReadFile(fname)
if err != nil {
Expand Down Expand Up @@ -228,6 +254,16 @@ func (t *dirTemplate) Execute(dirPrefix string) error {
})
}

func isBinary(r io.Reader) (bool, error) {
buf := make([]byte, 1024)
n, err := r.Read(buf)
if err != nil && err != io.EOF {
return false, err
}

return !utf8.Valid(buf[:n]) || bytes.ContainsAny(buf[:n], "\x00"), nil
}

func ignoreCopyFile(filename string) bool {
for _, pattern := range boilr.Configuration.IgnoreCopyFiles {
if glob.Glob(pattern, filename) {
Expand Down

0 comments on commit cb5602e

Please sign in to comment.