Skip to content

Commit 570d6a1

Browse files
committed
- [*] v5.0.01: make all template external to expose the API on godoc
1 parent ab508dd commit 570d6a1

File tree

8 files changed

+173
-54
lines changed

8 files changed

+173
-54
lines changed

cmd/easygen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141

4242
var (
4343
progname = "easygen"
44-
version = "4.1.01"
44+
version = "5.0.01"
4545
date = "2020-07-26"
4646
)
4747

egCal/EgCal.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,23 @@ import (
2626
// Constant and data type/structure definitions
2727

2828
// EgCal -- EasyGen Calculation
29+
/*
30+
31+
add is template function for Add
32+
subtract is template function for Subtract
33+
multiply is template function for Multiply
34+
divide is template function for Divide
35+
36+
*/
2937
type EgCal struct {
3038
*easygen.EgBase
3139
}
3240

3341
var egFuncMap = easygen.FuncMap{
34-
"add": add,
35-
"subtract": subtract,
36-
"multiply": multiply,
37-
"divide": divide,
42+
"add": Add,
43+
"subtract": Subtract,
44+
"multiply": Multiply,
45+
"divide": Divide,
3846
}
3947

4048
////////////////////////////////////////////////////////////////////////////

egCal/consul-template_functions.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"reflect"
1313
)
1414

15-
// add returns the sum of a and b.
16-
func add(a, b interface{}) (interface{}, error) {
15+
// Add returns the sum of a and b.
16+
func Add(a, b interface{}) (interface{}, error) {
1717
av := reflect.ValueOf(a)
1818
bv := reflect.ValueOf(b)
1919

@@ -56,8 +56,8 @@ func add(a, b interface{}) (interface{}, error) {
5656
}
5757
}
5858

59-
// subtract returns the difference of b from a.
60-
func subtract(a, b interface{}) (interface{}, error) {
59+
// Subtract returns the difference of b from a.
60+
func Subtract(a, b interface{}) (interface{}, error) {
6161
av := reflect.ValueOf(a)
6262
bv := reflect.ValueOf(b)
6363

@@ -100,8 +100,8 @@ func subtract(a, b interface{}) (interface{}, error) {
100100
}
101101
}
102102

103-
// multiply returns the product of a and b.
104-
func multiply(a, b interface{}) (interface{}, error) {
103+
// Multiply returns the product of a and b.
104+
func Multiply(a, b interface{}) (interface{}, error) {
105105
av := reflect.ValueOf(a)
106106
bv := reflect.ValueOf(b)
107107

@@ -144,8 +144,8 @@ func multiply(a, b interface{}) (interface{}, error) {
144144
}
145145
}
146146

147-
// divide returns the division of b from a.
148-
func divide(a, b interface{}) (interface{}, error) {
147+
// Divide returns the division of b from a.
148+
func Divide(a, b interface{}) (interface{}, error) {
149149
av := reflect.ValueOf(a)
150150
bv := reflect.ValueOf(b)
151151

egFilePath/EgFilePath.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ import (
2626
// Constant and data type/structure definitions
2727

2828
// EgFilePath -- EasyGen FilePath manupilation functionalities
29+
/*
30+
31+
fpAbs is wrapper for filepath.Abs
32+
fpBase is wrapper for filepath.Base
33+
fpClean is wrapper for filepath.Clean
34+
fpDir is wrapper for filepath.Dir
35+
fpEvalSymlinks is wrapper for filepath.EvalSymlinks
36+
fpExt is wrapper for filepath.Ext
37+
fpFromSlash is wrapper for filepath.FromSlash
38+
fpGlob is wrapper for filepath.Glob
39+
fpHasPrefix is wrapper for filepath.HasPrefix
40+
fpIsAbs is wrapper for filepath.IsAbs
41+
fpJoin is wrapper for filepath.Join
42+
fpMatch is wrapper for filepath.Match
43+
fpRel is wrapper for filepath.Rel
44+
fpSplitList is wrapper for filepath.SplitList
45+
fpToSlash is wrapper for filepath.ToSlash
46+
fpVolumeName is wrapper for filepath.VolumeName
47+
48+
isDir is template function for IsDir
49+
basename is template function for Basename
50+
51+
*/
2952
type EgFilePath struct {
3053
*easygen.EgBase
3154
}
@@ -62,11 +85,13 @@ func FuncDefs() template.FuncMap {
6285
//==========================================================================
6386
// support functions
6487

88+
// IsDir returns true if path is dir
6589
func IsDir(path string) bool {
6690
info, _ := os.Stat(path)
6791
return info.IsDir()
6892
}
6993

94+
// Basename returns basename(path)
7095
func Basename(s string) string {
7196
s = filepath.Base(s)
7297
n := strings.LastIndexByte(s, '.')

template.go

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,79 @@ type Template interface {
2626
}
2727

2828
// EgBase -- EasyGen Template Base
29+
/*
30+
31+
stringsCompare is wrapper for strings.Compare
32+
stringsContains is wrapper for strings.Contains
33+
stringsContainsAny is wrapper for strings.ContainsAny
34+
stringsContainsRune is wrapper for strings.ContainsRune
35+
stringsCount is wrapper for strings.Count
36+
stringsEqualFold is wrapper for strings.EqualFold
37+
stringsFields is wrapper for strings.Fields
38+
stringsFieldsFunc is wrapper for strings.FieldsFunc
39+
stringsHasPrefix is wrapper for strings.HasPrefix
40+
stringsHasSuffix is wrapper for strings.HasSuffix
41+
stringsIndex is wrapper for strings.Index
42+
stringsIndexAny is wrapper for strings.IndexAny
43+
stringsIndexByte is wrapper for strings.IndexByte
44+
stringsIndexFunc is wrapper for strings.IndexFunc
45+
stringsIndexRune is wrapper for strings.IndexRune
46+
stringsJoin is wrapper for strings.Join
47+
stringsLastIndex is wrapper for strings.LastIndex
48+
stringsLastIndexAny is wrapper for strings.LastIndexAny
49+
stringsLastIndexByte is wrapper for strings.LastIndexByte
50+
stringsLastIndexFunc is wrapper for strings.LastIndexFunc
51+
stringsMap is wrapper for strings.Map
52+
stringsRepeat is wrapper for strings.Repeat
53+
stringsReplace is wrapper for strings.Replace
54+
stringsSplit is wrapper for strings.Split
55+
stringsSplitAfter is wrapper for strings.SplitAfter
56+
stringsSplitAfterN is wrapper for strings.SplitAfterN
57+
stringsSplitN is wrapper for strings.SplitN
58+
stringsTitle is wrapper for strings.Title
59+
stringsToLower is wrapper for strings.ToLower
60+
stringsToLowerSpecial is wrapper for strings.ToLowerSpecial
61+
stringsToTitle is wrapper for strings.ToTitle
62+
stringsToTitleSpecial is wrapper for strings.ToTitleSpecial
63+
stringsToUpper is wrapper for strings.ToUpper
64+
stringsToUpperSpecial is wrapper for strings.ToUpperSpecial
65+
stringsTrim is wrapper for strings.Trim
66+
stringsTrimFunc is wrapper for strings.TrimFunc
67+
stringsTrimLeft is wrapper for strings.TrimLeft
68+
stringsTrimLeftFunc is wrapper for strings.TrimLeftFunc
69+
stringsTrimPrefix is wrapper for strings.TrimPrefix
70+
stringsTrimRight is wrapper for strings.TrimRight
71+
stringsTrimRightFunc is wrapper for strings.TrimRightFunc
72+
stringsTrimSpace is wrapper for strings.TrimSpace
73+
stringsTrimSuffix is wrapper for strings.TrimSuffix
74+
75+
eqf is wrapper for strings.EqualFold
76+
split is wrapper for strings.Fields
77+
78+
regexpFindAllString is template function for RegexpFindAllString
79+
regexpFindAllStringIndex is template function for RegexpFindAllStringIndex
80+
regexpFindAllStringSubmatch is template function for RegexpFindAllStringSubmatch
81+
regexpFindAllStringSubmatchIndex is template function for RegexpFindAllStringSubmatchIndex
82+
regexpFindString is template function for RegexpFindString
83+
regexpFindStringIndex is template function for RegexpFindStringIndex
84+
regexpFindStringSubmatch is template function for RegexpFindStringSubmatch
85+
regexpFindStringSubmatchIndex is template function for RegexpFindStringSubmatchIndex
86+
regexpMatchString is template function for RegexpMatchString
87+
regexpReplaceAllLiteralString is template function for RegexpReplaceAllLiteralString
88+
regexpReplaceAllString is template function for RegexpReplaceAllString
89+
regexpReplaceAllStringFunc is template function for RegexpReplaceAllStringFunc
90+
regexpSplit is template function for RegexpSplit
91+
92+
ENV is template function for os.Getenv
93+
substr is template function for Substr
94+
coalesce is template function for Coalesce
95+
quote4shell is template function for Quote4shell
96+
97+
minus1 is template function for Minus1
98+
date is template function for Date
99+
timestamp is template function for Timestamp
100+
101+
*/
29102
type EgBase struct {
30103
*template.Template
31104
}
@@ -85,29 +158,29 @@ var egFuncMap = FuncMap{
85158
"split": strings.Fields,
86159

87160
// == standard regexp function definitions
88-
"regexpFindAllString": regexpFindAllString,
89-
"regexpFindAllStringIndex": regexpFindAllStringIndex,
90-
"regexpFindAllStringSubmatch": regexpFindAllStringSubmatch,
91-
"regexpFindAllStringSubmatchIndex": regexpFindAllStringSubmatchIndex,
92-
"regexpFindString": regexpFindString,
93-
"regexpFindStringIndex": regexpFindStringIndex,
94-
"regexpFindStringSubmatch": regexpFindStringSubmatch,
95-
"regexpFindStringSubmatchIndex": regexpFindStringSubmatchIndex,
96-
"regexpMatchString": regexpMatchString,
97-
"regexpReplaceAllLiteralString": regexpReplaceAllLiteralString,
98-
"regexpReplaceAllString": regexpReplaceAllString,
99-
"regexpReplaceAllStringFunc": regexpReplaceAllStringFunc,
100-
"regexpSplit": regexpSplit,
161+
"regexpFindAllString": RegexpFindAllString,
162+
"regexpFindAllStringIndex": RegexpFindAllStringIndex,
163+
"regexpFindAllStringSubmatch": RegexpFindAllStringSubmatch,
164+
"regexpFindAllStringSubmatchIndex": RegexpFindAllStringSubmatchIndex,
165+
"regexpFindString": RegexpFindString,
166+
"regexpFindStringIndex": RegexpFindStringIndex,
167+
"regexpFindStringSubmatch": RegexpFindStringSubmatch,
168+
"regexpFindStringSubmatchIndex": RegexpFindStringSubmatchIndex,
169+
"regexpMatchString": RegexpMatchString,
170+
"regexpReplaceAllLiteralString": RegexpReplaceAllLiteralString,
171+
"regexpReplaceAllString": RegexpReplaceAllString,
172+
"regexpReplaceAllStringFunc": RegexpReplaceAllStringFunc,
173+
"regexpSplit": RegexpSplit,
101174

102175
// == my added functions
103176
"ENV": os.Getenv,
104177
"substr": Substr,
105-
"coalesce": coalesce,
106-
"quote4shell": quote4shell,
178+
"coalesce": Coalesce,
179+
"quote4shell": Quote4shell,
107180

108-
"minus1": minus1,
109-
"date": date,
110-
"timestamp": timestamp,
181+
"minus1": Minus1,
182+
"date": Date,
183+
"timestamp": Timestamp,
111184
}
112185

113186
////////////////////////////////////////////////////////////////////////////

tf-calc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ package easygen
66
// By Caleb Spare @gmail.com
77
// https://groups.google.com/d/msg/golang-nuts/gzAyBLAeUbU/LwgomgxcjQ8J
88

9-
// Get input less 1
10-
func minus1(n int) int { return n - 1 }
9+
// Minus1 calculates to input less 1
10+
func Minus1(n int) int { return n - 1 }

tf-datetime.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ var now = func() time.Time { return time.Now() }
1515

1616
// https://godoc.org/time#pkg-constants
1717

18-
// date returns the date or year
19-
func date(fmt string) string {
18+
// Date returns the date or year
19+
func Date(fmt string) string {
2020
switch fmt {
2121
case "Y4":
2222
// returns the year string of length 4
@@ -33,9 +33,9 @@ func date(fmt string) string {
3333

3434
// https://github.com/hashicorp/consul-template/blob/de2ebf4/template_functions.go#L666-L682
3535

36-
// timestamp returns the current UNIX timestamp in UTC. If an argument is
36+
// Timestamp returns the current UNIX timestamp in UTC. If an argument is
3737
// specified, it will be used to format the timestamp.
38-
func timestamp(s ...string) (string, error) {
38+
func Timestamp(s ...string) (string, error) {
3939
switch len(s) {
4040
case 0:
4141
return now().Format(time.RFC3339), nil

tf-strings.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,64 +82,77 @@ func Substr(a string, nums ...interface{}) (string, error) {
8282
////////////////////////////////////////////////////////////////////////////
8383
// Regexp Function Definitions
8484

85-
func regexpFindAllString(s string, regExp string, n int) []string {
85+
// RegexpFindAllString is wrapper for regexp.FindAllString
86+
func RegexpFindAllString(s string, regExp string, n int) []string {
8687
return regexp.MustCompile(regExp).FindAllString(s, n)
8788
}
8889

89-
func regexpFindAllStringIndex(s string, regExp string, n int) [][]int {
90+
// RegexpFindAllStringIndex is wrapper for regexp.FindAllStringIndex
91+
func RegexpFindAllStringIndex(s string, regExp string, n int) [][]int {
9092
return regexp.MustCompile(regExp).FindAllStringIndex(s, n)
9193
}
9294

93-
func regexpFindAllStringSubmatch(s string, regExp string, n int) [][]string {
95+
// RegexpFindAllStringSubmatch is wrapper for regexp.FindAllStringSubmatch
96+
func RegexpFindAllStringSubmatch(s string, regExp string, n int) [][]string {
9497
return regexp.MustCompile(regExp).FindAllStringSubmatch(s, n)
9598
}
9699

97-
func regexpFindAllStringSubmatchIndex(s string, regExp string, n int) [][]int {
100+
// RegexpFindAllStringSubmatchIndex is wrapper for regexp.FindAllStringSubmatchIndex
101+
func RegexpFindAllStringSubmatchIndex(s string, regExp string, n int) [][]int {
98102
return regexp.MustCompile(regExp).FindAllStringSubmatchIndex(s, n)
99103
}
100104

101-
func regexpFindString(s string, regExp string) string {
105+
// RegexpFindString is wrapper for regexp.FindString
106+
func RegexpFindString(s string, regExp string) string {
102107
return regexp.MustCompile(regExp).FindString(s)
103108
}
104109

105-
func regexpFindStringIndex(s string, regExp string) (loc []int) {
110+
// RegexpFindStringIndex is wrapper for regexp.FindStringIndex
111+
func RegexpFindStringIndex(s string, regExp string) (loc []int) {
106112
return regexp.MustCompile(regExp).FindStringIndex(s)
107113
}
108114

109-
func regexpFindStringSubmatch(s string, regExp string) []string {
115+
// RegexpFindStringSubmatch is wrapper for regexp.FindStringSubmatch
116+
func RegexpFindStringSubmatch(s string, regExp string) []string {
110117
return regexp.MustCompile(regExp).FindStringSubmatch(s)
111118
}
112119

113-
func regexpFindStringSubmatchIndex(s string, regExp string) []int {
120+
// RegexpFindStringSubmatchIndex is wrapper for regexp.FindStringSubmatchIndex
121+
func RegexpFindStringSubmatchIndex(s string, regExp string) []int {
114122
return regexp.MustCompile(regExp).FindStringSubmatchIndex(s)
115123
}
116124

117-
func regexpMatchString(s string, regExp string) bool {
125+
// RegexpMatchString is wrapper for regexp.MatchString
126+
func RegexpMatchString(s string, regExp string) bool {
118127
return regexp.MustCompile(regExp).MatchString(s)
119128
}
120129

121-
func regexpReplaceAllLiteralString(src, regExp string, repl string) string {
130+
// RegexpReplaceAllLiteralString is wrapper for regexp.ReplaceAllLiteralString
131+
func RegexpReplaceAllLiteralString(src, regExp string, repl string) string {
122132
return regexp.MustCompile(regExp).ReplaceAllLiteralString(src, repl)
123133
}
124134

125-
func regexpReplaceAllString(src, regExp string, repl string) string {
135+
// RegexpReplaceAllString is wrapper for regexp.ReplaceAllString
136+
func RegexpReplaceAllString(src, regExp string, repl string) string {
126137
return regexp.MustCompile(regExp).ReplaceAllString(src, repl)
127138
}
128139

129-
func regexpReplaceAllStringFunc(src string, regExp string, repl func(string) string) string {
140+
// RegexpReplaceAllStringFunc is wrapper for regexp.ReplaceAllStringFunc
141+
func RegexpReplaceAllStringFunc(src string, regExp string, repl func(string) string) string {
130142
return regexp.MustCompile(regExp).ReplaceAllStringFunc(src, repl)
131143
}
132144

133-
func regexpSplit(s string, regExp string, n int) []string {
145+
// RegexpSplit is wrapper for regexp.Split
146+
func RegexpSplit(s string, regExp string, n int) []string {
134147
return regexp.MustCompile(regExp).Split(s, n)
135148
}
136149

137150
////////////////////////////////////////////////////////////////////////////
138151
// Misc
139152

140-
// coalesce function takes two or more string arguments and returns the first argument that is not empty.
153+
// Coalesce function takes two or more string arguments and returns the first argument that is not empty.
141154
// The result is empty only if all the arguments are empty.
142-
func coalesce(s ...string) string {
155+
func Coalesce(s ...string) string {
143156
for _, str := range s {
144157
if len(str) != 0 && str != "<no value>" {
145158
return str
@@ -148,8 +161,8 @@ func coalesce(s ...string) string {
148161
return ""
149162
}
150163

151-
// quote4shell -- quote file name for shell.
164+
// Quote4shell -- quote file name for shell.
152165
// So "%bob's file" will be quoted as '%bob'\''s file'
153-
func quote4shell(s string) string {
166+
func Quote4shell(s string) string {
154167
return "'" + strings.Join(strings.Split(s, "'"), `'\''`) + "'"
155168
}

0 commit comments

Comments
 (0)