author | Mikael Berthe <mikael@lilotux.net> |
Mon, 01 May 2017 12:42:37 +0200 | |
changeset 50 | c6adf9d9e996 |
parent 49 | d6b4e3b7c6c6 |
child 51 | 300ac09051a7 |
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" |
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
17 |
"github.com/mattn/go-isatty" |
40
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
18 |
|
0 | 19 |
"github.com/McKael/madon" |
49
d6b4e3b7c6c6
Add ANSI color support to templates (function "color")
Mikael Berthe <mikael@lilotux.net>
parents:
41
diff
changeset
|
20 |
"github.com/McKael/madonctl/printer/colors" |
0 | 21 |
) |
22 |
||
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
23 |
// DisableColors can be set to true to disable the color template function |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
24 |
var DisableColors bool |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
25 |
|
0 | 26 |
// TemplatePrinter represents a Template printer |
27 |
type TemplatePrinter struct { |
|
28 |
rawTemplate string |
|
29 |
template *template.Template |
|
30 |
} |
|
31 |
||
32 |
// NewPrinterTemplate returns a Template ResourcePrinter |
|
33 |
// For TemplatePrinter, the option parameter contains the template string. |
|
34 |
func NewPrinterTemplate(option string) (*TemplatePrinter, error) { |
|
35 |
tmpl := option |
|
41
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
40
diff
changeset
|
36 |
t, err := template.New("output").Funcs(template.FuncMap{ |
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
40
diff
changeset
|
37 |
"fromhtml": html2string, |
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
40
diff
changeset
|
38 |
"fromunix": unix2string, |
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
39 |
"color": ansiColor, |
41
909c3ddd83f6
Add fromunix filter to templates (converts date from UNIX timestamp)
Mikael Berthe <mikael@lilotux.net>
parents:
40
diff
changeset
|
40 |
}).Parse(tmpl) |
0 | 41 |
if err != nil { |
42 |
return nil, err |
|
43 |
} |
|
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
44 |
|
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
45 |
// Check if stdout is a TTY |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
46 |
if !isatty.IsTerminal(os.Stdout.Fd()) { |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
47 |
DisableColors = true |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
48 |
} |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
49 |
|
0 | 50 |
return &TemplatePrinter{ |
51 |
rawTemplate: tmpl, |
|
52 |
template: t, |
|
53 |
}, nil |
|
54 |
} |
|
55 |
||
56 |
// PrintObj sends the object as text to the writer |
|
57 |
// If the writer w is nil, standard output will be used. |
|
58 |
func (p *TemplatePrinter) PrintObj(obj interface{}, w io.Writer, tmpl string) error { |
|
59 |
if w == nil { |
|
60 |
w = os.Stdout |
|
61 |
} |
|
62 |
||
63 |
if p.template == nil { |
|
64 |
return fmt.Errorf("template not built") |
|
65 |
} |
|
66 |
||
67 |
switch ot := obj.(type) { // I wish I knew a better way... |
|
68 |
case []madon.Account, []madon.Application, []madon.Attachment, []madon.Card, |
|
69 |
[]madon.Client, []madon.Context, []madon.Instance, []madon.Mention, |
|
70 |
[]madon.Notification, []madon.Relationship, []madon.Report, |
|
40
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
71 |
[]madon.Results, []madon.Status, []madon.StreamEvent, []madon.Tag, |
ad148f60dc87
TemplatePrinter: Handle InstanceStatus lists
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
72 |
[]*gomif.InstanceStatus: |
0 | 73 |
return p.templateForeach(ot, w) |
74 |
} |
|
75 |
||
76 |
return p.templatePrintSingleObj(obj, w) |
|
77 |
} |
|
78 |
||
79 |
func (p *TemplatePrinter) templatePrintSingleObj(obj interface{}, w io.Writer) error { |
|
80 |
// This code comes from Kubernetes. |
|
81 |
data, err := json.Marshal(obj) |
|
82 |
if err != nil { |
|
83 |
return err |
|
84 |
} |
|
85 |
out := map[string]interface{}{} |
|
86 |
if err := json.Unmarshal(data, &out); err != nil { |
|
87 |
return err |
|
88 |
} |
|
89 |
if err = p.safeExecute(w, out); err != nil { |
|
90 |
return fmt.Errorf("error executing template %q: %v", p.rawTemplate, err) |
|
91 |
} |
|
92 |
return nil |
|
93 |
} |
|
94 |
||
95 |
// safeExecute tries to execute the template, but catches panics and returns an error |
|
96 |
// should the template engine panic. |
|
97 |
// This code comes from Kubernetes. |
|
98 |
func (p *TemplatePrinter) safeExecute(w io.Writer, obj interface{}) error { |
|
99 |
var panicErr error |
|
100 |
// Sorry for the double anonymous function. There's probably a clever way |
|
101 |
// to do this that has the defer'd func setting the value to be returned, but |
|
102 |
// that would be even less obvious. |
|
103 |
retErr := func() error { |
|
104 |
defer func() { |
|
105 |
if x := recover(); x != nil { |
|
106 |
panicErr = fmt.Errorf("caught panic: %+v", x) |
|
107 |
} |
|
108 |
}() |
|
109 |
return p.template.Execute(w, obj) |
|
110 |
}() |
|
111 |
if panicErr != nil { |
|
112 |
return panicErr |
|
113 |
} |
|
114 |
return retErr |
|
115 |
} |
|
116 |
||
117 |
func (p *TemplatePrinter) templateForeach(ol interface{}, w io.Writer) error { |
|
118 |
switch reflect.TypeOf(ol).Kind() { |
|
119 |
case reflect.Slice: |
|
120 |
s := reflect.ValueOf(ol) |
|
121 |
||
122 |
for i := 0; i < s.Len(); i++ { |
|
123 |
o := s.Index(i).Interface() |
|
124 |
if err := p.templatePrintSingleObj(o, w); err != nil { |
|
125 |
return err |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
return nil |
|
130 |
} |
|
50
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
131 |
|
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
132 |
func ansiColor(desc string) (string, error) { |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
133 |
if DisableColors { |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
134 |
return "", nil |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
135 |
} |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
136 |
return colors.ANSICodeString(desc) |
c6adf9d9e996
Disable ANSI sequences when stdout is not a TTY
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
137 |
} |