Skip to content
This repository was archived by the owner on May 3, 2021. It is now read-only.

Commit 84a034b

Browse files
committed
Updated to allow format-file to be specified.
And to aid discovery and usage we can dump the default template.
1 parent 7d6583a commit 84a034b

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

cmd/maildir-tools/message_cmd.go

+38-10
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,31 @@ import (
77
"context"
88
"flag"
99
"fmt"
10+
"io/ioutil"
1011
"text/template"
1112

1213
"github.com/google/subcommands"
1314
"github.com/skx/maildir-tools/mailreader"
1415
)
1516

17+
var (
18+
defaultTemplate = `To: {{.To}}
19+
From: {{.From}}
20+
Cc: {{.Cc}}
21+
Date: {{.Date}}
22+
Subject: {{.Subject}}
23+
24+
{{.Body}}`
25+
)
26+
27+
// messageCmd holds our state
1628
type messageCmd struct {
29+
30+
// Template points to a template to use for rendering the mail.
31+
template string
32+
33+
// If this flag is true we just dump our template
34+
dumpTemplate bool
1735
}
1836

1937
//
@@ -23,28 +41,33 @@ func (*messageCmd) Name() string { return "message" }
2341
func (*messageCmd) Synopsis() string { return "Show a message." }
2442
func (*messageCmd) Usage() string {
2543
return `message :
26-
Show a single formatted message.
44+
Show a single formatted message. By default an internal template
45+
will be used, but you may specify the filename of a Golang text/template
46+
file to use for rendering if you wish.
2747
`
2848
}
2949

3050
//
3151
// Flag setup
3252
//
3353
func (p *messageCmd) SetFlags(f *flag.FlagSet) {
54+
f.StringVar(&p.template, "template", "", "Specify the path to a golang text/template file to use for message-rendering")
55+
f.BoolVar(&p.dumpTemplate, "dump-template", false, "Dump the default template")
3456
}
3557

36-
// Show the specified email
58+
// Show the specified email, with the appropriate template
3759
func (p *messageCmd) GetMessage(path string) (string, error) {
3860

39-
//
40-
// We'll format the message with a template
41-
//
42-
tmpl := `To: {{.To}}
43-
From: {{.From}}
44-
Date: {{.Date}}
45-
Subject: {{.Subject}}
61+
// Load the default template
62+
tmpl := defaultTemplate
4663

47-
{{.Body}}`
64+
if p.template != "" {
65+
content, err := ioutil.ReadFile(p.template)
66+
if err != nil {
67+
return "", err
68+
}
69+
tmpl = string(content)
70+
}
4871

4972
// This is the structure we'll use to populate that
5073
// template with.
@@ -85,6 +108,11 @@ Subject: {{.Subject}}
85108
// Entry-point.
86109
func (p *messageCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
87110

111+
if p.dumpTemplate {
112+
fmt.Println(defaultTemplate)
113+
return subcommands.ExitSuccess
114+
}
115+
88116
for _, path := range f.Args() {
89117
out, err := p.GetMessage(path)
90118
if err != nil {

0 commit comments

Comments
 (0)