1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
package pencode
import (
"io/ioutil"
"os"
"strings"
)
//isTemplate checks if the name ends with string ".tmpl"
func isTemplate(name string) bool {
return strings.HasSuffix(name, ".tmpl")
}
//hasTemplate checks if the name points to an existing file
func hasTemplate(name string) (bool, error) {
if _, err := os.Stat(name); err == nil {
return true, nil
} else {
return false, err
}
}
type Template struct {
Data []byte
Keyword string
}
func NewTemplateEncoder(name string) (Encoder, error) {
var ok bool
var err error
t := Template{}
// Using static keyword for now
t.Keyword = "#PAYLOAD#"
if ok, err = hasTemplate(name); ok {
t.Data, err = ioutil.ReadFile(name)
}
return t, err
}
func (t Template) Encode(input []byte) ([]byte, error) {
return []byte(strings.ReplaceAll(string(t.Data), t.Keyword, string(input))), nil
}
func (t Template) HelpText() string {
return "Replaces string #PAYLOAD# in content of a file that has .tmpl extension."
}
func (t Template) Type() string {
return "other"
}
|