Skip to content

Commit

Permalink
add TemplateFile to the templates so user can give a path to a templa…
Browse files Browse the repository at this point in the history
…te (#1)

* add TemplateFile to the templates so user can give a path to a template
  • Loading branch information
xmcqueen authored and sgreben committed May 31, 2019
1 parent 51a0aa2 commit f773586
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Here's a compact overview:
| [TCPAddrsCSV](https://godoc.org/github.com/sgreben/flagvar#TCPAddrsCSV) | 127.0.0.1:10,:123 | []net.TCPAddr |
| [Template](https://godoc.org/github.com/sgreben/flagvar#Template) | "{{.Size}}" | *template.Template |
| [Templates](https://godoc.org/github.com/sgreben/flagvar#Templates) | "{{.Size}}" | []*template.Template |
| [TemplateFile](https://godoc.org/github.com/sgreben/flagvar#TemplateFile) | "/path/to/template.file" | string |
| [Time](https://godoc.org/github.com/sgreben/flagvar#Time) | "10:30 AM" | time.Time |
| [Times](https://godoc.org/github.com/sgreben/flagvar#Times) | "10:30 AM" | []time.Time |
| [UDPAddr](https://godoc.org/github.com/sgreben/flagvar#UDPAddr) | 127.0.0.1:10 | net.UDPAddr |
Expand Down
42 changes: 42 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flagvar

import (
"fmt"
"io/ioutil"
"text/template"
)

Expand Down Expand Up @@ -67,3 +68,44 @@ func (fv *Templates) Set(v string) error {
func (fv *Templates) String() string {
return fmt.Sprint(fv.Texts)
}

// Template is a `flag.Value` for `text.Template` arguments.
// The value of the `Root` field is used as a root template when specified.
// The value specified on the command line is the path to the template
type TemplateFile struct {
Root *template.Template

Value *template.Template
Text string
}

// Help returns a string suitable for inclusion in a flag help message.
func (fv *TemplateFile) Help() string {
return "file system path to a go template"
}

// Set is flag.Value.Set
func (fv *TemplateFile) Set(v string) error {

_template, err := ioutil.ReadFile(v)

if err != nil {
return err
}

root := fv.Root
if root == nil {
root = template.New("")
}
t, err := root.New(fmt.Sprintf("%T(%p)", fv, fv)).Parse(string(_template))
if err == nil {
fv.Text = v
fv.Value = t
}
return err
}

func (fv *TemplateFile) String() string {
return fv.Text
}

11 changes: 11 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,14 @@ func TestTemplatesFail(t *testing.T) {
t.Fail()
}
}

func TestTemplateFile(t *testing.T) {
fv := flagvar.TemplateFile{}
var fs flag.FlagSet
fs.Var(&fv, "template", "")

err := fs.Parse([]string{"-template", "./noSuchFile.tpl"})
if err == nil {
t.Fail()
}
}

0 comments on commit f773586

Please sign in to comment.