@@ -44,6 +44,18 @@ const repoErrTmpl = `
4444 {{ range .Repos }}- {{ . }}
4545 {{ end }}`
4646
47+ // Templates are parsed once at package init. text/template.Template.Execute is
48+ // safe for concurrent use, so the same parsed template can be shared across
49+ // goroutines without re-parsing on every call.
50+ var (
51+ successTpl = template .Must (template .New ("success" ).Funcs (helpers ).Parse (successTmpl ))
52+ errTpl = template .Must (template .New ("err" ).Funcs (helpers ).Parse (errTmpl ))
53+ cmdTpl = template .Must (template .New ("cmd" ).Funcs (helpers ).Parse (cmdTmpl ))
54+ msgTpl = template .Must (template .New ("msg" ).Funcs (helpers ).Parse (msgTmpl ))
55+ msgErrTpl = template .Must (template .New ("msgErr" ).Funcs (helpers ).Parse (msgErrTmpl ))
56+ repoErrTpl = template .Must (template .New ("repoErr" ).Funcs (helpers ).Parse (repoErrTmpl ))
57+ )
58+
4759// Printer is struct
4860type Printer struct {
4961 writer io.Writer
@@ -78,9 +90,7 @@ func (p *Printer) PrintCmd(cmd string, options []string) {
7890 Cmd string
7991 Ops string
8092 }
81- t := template .Must (template .New ("item" ).Funcs (helpers ).Parse (cmdTmpl ))
82- err := t .Execute (p .writer , cmds {Cmd : cmd , Ops : strings .Join (options , " " )})
83- if err != nil {
93+ if err := cmdTpl .Execute (p .writer , cmds {Cmd : cmd , Ops : strings .Join (options , " " )}); err != nil {
8494 log .Println (err )
8595 }
8696}
@@ -90,9 +100,7 @@ func (p *Printer) PrintMsg(msg string) {
90100 type message struct {
91101 Msg string
92102 }
93- t := template .Must (template .New ("msg" ).Funcs (helpers ).Parse (msgTmpl ))
94- err := t .Execute (p .writer , message {Msg : msg })
95- if err != nil {
103+ if err := msgTpl .Execute (p .writer , message {Msg : msg }); err != nil {
96104 log .Println (err )
97105 }
98106}
@@ -102,9 +110,7 @@ func (p *Printer) PrintMsgErr(msg string) {
102110 type message struct {
103111 Msg string
104112 }
105- t := template .Must (template .New ("msg" ).Funcs (helpers ).Parse (msgErrTmpl ))
106- err := t .Execute (p .writer , message {Msg : msg })
107- if err != nil {
113+ if err := msgErrTpl .Execute (p .writer , message {Msg : msg }); err != nil {
108114 log .Println (err )
109115 }
110116}
@@ -115,33 +121,22 @@ func (p *Printer) PrintRepoErr(msg string, repos []string) {
115121 Msg string
116122 Repos []string
117123 }
118- t := template .Must (template .New ("msg" ).Funcs (helpers ).Parse (repoErrTmpl ))
119- err := t .Execute (p .writer , message {Msg : msg , Repos : repos })
120- if err != nil {
124+ if err := repoErrTpl .Execute (p .writer , message {Msg : msg , Repos : repos }); err != nil {
121125 log .Println (err )
122126 }
123127}
124128
125129// Print prints result
126130func (p * Printer ) Print (res Result ) {
127- err := t (true ).Execute (p .writer , res )
128- if err != nil {
131+ if err := successTpl .Execute (p .writer , res ); err != nil {
129132 log .Println (err )
130133 }
131134}
132135
133136// Error prints error
134137func (p * Printer ) Error (res Result ) {
135138 res .Msg = res .Err .Error ()
136- err := t (false ).Execute (p .errWriter , res )
137- if err != nil {
139+ if err := errTpl .Execute (p .errWriter , res ); err != nil {
138140 log .Println (err )
139141 }
140142}
141-
142- func t (isSuccess bool ) * template.Template {
143- if isSuccess {
144- return template .Must (template .New ("item" ).Funcs (helpers ).Parse (successTmpl ))
145- }
146- return template .Must (template .New ("item" ).Funcs (helpers ).Parse (errTmpl ))
147- }
0 commit comments