-
Notifications
You must be signed in to change notification settings - Fork 22
/
template.go
84 lines (70 loc) · 1.68 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cli
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
)
const indentation = ` `
type exampleData struct {
Software string
}
// LongDesc normalizes a command's long description to follow the conventions.
func LongDesc(s string) string {
if len(s) == 0 {
return s
}
return normalizer{s}.trim().noIndent().string
}
// Examples normalizes a command's examples to follow the conventions.
func Examples(s string) string {
if len(s) == 0 {
return s
}
software := os.Args[0]
// If the wallet cmd is embedded inside vega, we display the software as
// a sub-command in the examples.
if software == "vega" || strings.HasSuffix(software, "/vega") {
software = fmt.Sprintf("%s wallet", software)
}
sweaters := exampleData{
Software: software,
}
tmpl, err := template.New("example").Parse(s)
if err != nil {
panic(err)
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, sweaters)
if err != nil {
panic(err)
}
return normalizer{tpl.String()}.trim().indent().string
}
type normalizer struct {
string
}
func (s normalizer) trim() normalizer {
s.string = strings.TrimSpace(s.string)
return s
}
func (s normalizer) indent() normalizer {
indentedLines := []string{}
for _, line := range strings.Split(s.string, "\n") {
trimmed := strings.TrimSpace(line)
indented := indentation + trimmed
indentedLines = append(indentedLines, indented)
}
s.string = strings.Join(indentedLines, "\n")
return s
}
func (s normalizer) noIndent() normalizer {
indentedLines := []string{}
for _, line := range strings.Split(s.string, "\n") {
trimmed := strings.TrimSpace(line)
indentedLines = append(indentedLines, trimmed)
}
s.string = strings.Join(indentedLines, "\n")
return s
}