Skip to content

Commit

Permalink
Code format
Browse files Browse the repository at this point in the history
  • Loading branch information
subchen committed Mar 19, 2019
1 parent 5551295 commit 708fe54
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/locations/api.nginx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
location /api {
{{ include "../shared/log.nginx" | indent 4 | trim }}

proxy_http_version 1.1;
proxy_pass http://backend;
proxy_redirect off;
Expand Down
26 changes: 17 additions & 9 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"text/template"
"time"

"fmt"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig"
"github.com/go-yaml/yaml"
"path"
"strings"
)

func FuncMap(templateName string) template.FuncMap {
Expand All @@ -30,15 +30,23 @@ func FuncMap(templateName string) template.FuncMap {
f["fileLastModified"] = fileLastModified
f["fileGetBytes"] = fileGetBytes
f["fileGetString"] = fileGetString

// include
f["include"] = include(templateName)

// Fix sprig regex functions
oRegexReplaceAll := f["regexReplaceAll"].(func(regex string, s string, repl string) string)
oRegexReplaceAllLiteral := f["regexReplaceAllLiteral"].(func(regex string, s string, repl string) string)
oRegexSplit := f["regexSplit"].(func(regex string, s string, n int) []string)
f["reReplaceAll"] = func(regex string, replacement string, input string) string { return oRegexReplaceAll(regex, input, replacement) }
f["reReplaceAllLiteral"] = func(regex string, replacement string, input string) string { return oRegexReplaceAllLiteral(regex, input, replacement) }
f["reSplit"] = func(regex string, n int, input string) []string { return oRegexSplit(regex, input, n) }
f["reReplaceAll"] = func(regex string, replacement string, input string) string {
return oRegexReplaceAll(regex, input, replacement)
}
f["reReplaceAllLiteral"] = func(regex string, replacement string, input string) string {
return oRegexReplaceAllLiteral(regex, input, replacement)
}
f["reSplit"] = func(regex string, n int, input string) []string {
return oRegexSplit(regex, input, n)
}
return f
}

Expand Down Expand Up @@ -155,14 +163,14 @@ func fileGetString(file string) string {
return string(data)
}

type relativeInclude func(include string) string
type relativeIncludeFunc func(include string) string

func include(callingFile string) relativeInclude {
func include(callingFile string) relativeIncludeFunc {
filePair := strings.SplitN(callingFile, ":", 2)
callingFile = filePair[0]

return func(includedFile string) string {
if ! path.IsAbs(includedFile) {
if !path.IsAbs(includedFile) {
includedFile = path.Join(path.Dir(callingFile), includedFile)
}

Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
// template shared context
var (
delims []string
ctx interface{}
ctx interface{}
)

// create template context
Expand Down Expand Up @@ -168,11 +168,12 @@ func templateExecute(t *template.Template, file string) {
if err != nil {
panic(fmt.Errorf("unable to create file, caused:\n\n %v\n", err))
}
defer dest.Close()

_, err = dest.Write(output.Bytes())
if err != nil {
panic(fmt.Errorf("unable to write file, caused:\n\n %v\n", err))
}
defer dest.Close()
}

func main() {
Expand Down Expand Up @@ -270,8 +271,8 @@ echo "{{ .Env.PATH }}" | frep -

t := template.New(srcFile)
t.Delims(delims[0], delims[1])

t.Funcs(FuncMap(file))

templateExecute(t, file)
}
}
Expand Down

0 comments on commit 708fe54

Please sign in to comment.