@@ -7,13 +7,31 @@ import (
7
7
"context"
8
8
"flag"
9
9
"fmt"
10
+ "io/ioutil"
10
11
"text/template"
11
12
12
13
"github.com/google/subcommands"
13
14
"github.com/skx/maildir-tools/mailreader"
14
15
)
15
16
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
16
28
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
17
35
}
18
36
19
37
//
@@ -23,28 +41,33 @@ func (*messageCmd) Name() string { return "message" }
23
41
func (* messageCmd ) Synopsis () string { return "Show a message." }
24
42
func (* messageCmd ) Usage () string {
25
43
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.
27
47
`
28
48
}
29
49
30
50
//
31
51
// Flag setup
32
52
//
33
53
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" )
34
56
}
35
57
36
- // Show the specified email
58
+ // Show the specified email, with the appropriate template
37
59
func (p * messageCmd ) GetMessage (path string ) (string , error ) {
38
60
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
46
63
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
+ }
48
71
49
72
// This is the structure we'll use to populate that
50
73
// template with.
@@ -85,6 +108,11 @@ Subject: {{.Subject}}
85
108
// Entry-point.
86
109
func (p * messageCmd ) Execute (_ context.Context , f * flag.FlagSet , _ ... interface {}) subcommands.ExitStatus {
87
110
111
+ if p .dumpTemplate {
112
+ fmt .Println (defaultTemplate )
113
+ return subcommands .ExitSuccess
114
+ }
115
+
88
116
for _ , path := range f .Args () {
89
117
out , err := p .GetMessage (path )
90
118
if err != nil {
0 commit comments