author | Mikael Berthe <mikael@lilotux.net> |
Sun, 30 Apr 2017 18:17:10 +0200 | |
changeset 40 | ad148f60dc87 |
parent 14 | da2059f2fa6a |
child 41 | 909c3ddd83f6 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
2 |
// |
|
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package printer |
|
7 |
||
8 |
import ( |
|
9 |
"encoding/json" |
|
10 |
"fmt" |
|
11 |
"io" |
|
12 |
"os" |
|
13 |
"reflect" |
|
14 |
"text/template" |
|
15 |
||
40
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
16 |
"github.com/m0t0k1ch1/gomif" |
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
17 |
|
0 | 18 |
"github.com/McKael/madon" |
19 |
) |
|
20 |
||
21 |
// TemplatePrinter represents a Template printer |
|
22 |
type TemplatePrinter struct { |
|
23 |
rawTemplate string |
|
24 |
template *template.Template |
|
25 |
} |
|
26 |
||
27 |
// NewPrinterTemplate returns a Template ResourcePrinter |
|
28 |
// For TemplatePrinter, the option parameter contains the template string. |
|
29 |
func NewPrinterTemplate(option string) (*TemplatePrinter, error) { |
|
30 |
tmpl := option |
|
14
da2059f2fa6a
Add function "fromhtml" for Go templates
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
31 |
t, err := template.New("output").Funcs(template.FuncMap{"fromhtml": html2string}).Parse(tmpl) |
0 | 32 |
if err != nil { |
33 |
return nil, err |
|
34 |
} |
|
35 |
return &TemplatePrinter{ |
|
36 |
rawTemplate: tmpl, |
|
37 |
template: t, |
|
38 |
}, nil |
|
39 |
} |
|
40 |
||
41 |
// PrintObj sends the object as text to the writer |
|
42 |
// If the writer w is nil, standard output will be used. |
|
43 |
func (p *TemplatePrinter) PrintObj(obj interface{}, w io.Writer, tmpl string) error { |
|
44 |
if w == nil { |
|
45 |
w = os.Stdout |
|
46 |
} |
|
47 |
||
48 |
if p.template == nil { |
|
49 |
return fmt.Errorf("template not built") |
|
50 |
} |
|
51 |
||
52 |
switch ot := obj.(type) { // I wish I knew a better way... |
|
53 |
case []madon.Account, []madon.Application, []madon.Attachment, []madon.Card, |
|
54 |
[]madon.Client, []madon.Context, []madon.Instance, []madon.Mention, |
|
55 |
[]madon.Notification, []madon.Relationship, []madon.Report, |
|
40
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
56 |
[]madon.Results, []madon.Status, []madon.StreamEvent, []madon.Tag, |
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
57 |
[]*gomif.InstanceStatus: |
0 | 58 |
return p.templateForeach(ot, w) |
59 |
} |
|
60 |
||
61 |
return p.templatePrintSingleObj(obj, w) |
|
62 |
} |
|
63 |
||
64 |
func (p *TemplatePrinter) templatePrintSingleObj(obj interface{}, w io.Writer) error { |
|
65 |
// This code comes from Kubernetes. |
|
66 |
data, err := json.Marshal(obj) |
|
67 |
if err != nil { |
|
68 |
return err |
|
69 |
} |
|
70 |
out := map[string]interface{}{} |
|
71 |
if err := json.Unmarshal(data, &out); err != nil { |
|
72 |
return err |
|
73 |
} |
|
74 |
if err = p.safeExecute(w, out); err != nil { |
|
75 |
return fmt.Errorf("error executing template %q: %v", p.rawTemplate, err) |
|
76 |
} |
|
77 |
return nil |
|
78 |
} |
|
79 |
||
80 |
// safeExecute tries to execute the template, but catches panics and returns an error |
|
81 |
// should the template engine panic. |
|
82 |
// This code comes from Kubernetes. |
|
83 |
func (p *TemplatePrinter) safeExecute(w io.Writer, obj interface{}) error { |
|
84 |
var panicErr error |
|
85 |
// Sorry for the double anonymous function. There's probably a clever way |
|
86 |
// to do this that has the defer'd func setting the value to be returned, but |
|
87 |
// that would be even less obvious. |
|
88 |
retErr := func() error { |
|
89 |
defer func() { |
|
90 |
if x := recover(); x != nil { |
|
91 |
panicErr = fmt.Errorf("caught panic: %+v", x) |
|
92 |
} |
|
93 |
}() |
|
94 |
return p.template.Execute(w, obj) |
|
95 |
}() |
|
96 |
if panicErr != nil { |
|
97 |
return panicErr |
|
98 |
} |
|
99 |
return retErr |
|
100 |
} |
|
101 |
||
102 |
func (p *TemplatePrinter) templateForeach(ol interface{}, w io.Writer) error { |
|
103 |
switch reflect.TypeOf(ol).Kind() { |
|
104 |
case reflect.Slice: |
|
105 |
s := reflect.ValueOf(ol) |
|
106 |
||
107 |
for i := 0; i < s.Len(); i++ { |
|
108 |
o := s.Index(i).Interface() |
|
109 |
if err := p.templatePrintSingleObj(o, w); err != nil { |
|
110 |
return err |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
return nil |
|
115 |
} |