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