-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from woodpecker-kit/28-feature-support-templat…
…e-as-handlebarsjs feat: add wd_template package for support Handlebars.js for golang
- Loading branch information
Showing
9 changed files
with
495 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package wd_template | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aymerick/raymond" | ||
_ "github.com/aymerick/raymond" | ||
"math" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
"time" | ||
"unicode" | ||
"unicode/utf8" | ||
) | ||
|
||
var ( | ||
DefaultHelpers = map[string]interface{}{ | ||
"duration": toDuration, | ||
"datetime": toDatetime, | ||
"success": isSuccess, | ||
"failure": isFailure, | ||
"truncate": truncate, | ||
"urlencode": urlencode, | ||
"since": since, | ||
"uppercasefirst": uppercaseFirst, | ||
"uppercase": strings.ToUpper, | ||
"lowercase": strings.ToLower, | ||
"regexReplace": regexReplace, | ||
} | ||
) | ||
|
||
func toDuration(started, finished int64) string { | ||
return fmt.Sprint(time.Duration(finished-started) * time.Second) | ||
} | ||
|
||
func toDatetime(timestamp int64, layout, zone string) string { | ||
if len(zone) == 0 { | ||
return time.Unix(timestamp, 0).Format(layout) | ||
} | ||
|
||
loc, err := time.LoadLocation(zone) | ||
|
||
if err != nil { | ||
return time.Unix(timestamp, 0).Local().Format(layout) | ||
} | ||
|
||
return time.Unix(timestamp, 0).In(loc).Format(layout) | ||
} | ||
|
||
func isSuccess(conditional bool, options *raymond.Options) string { | ||
if !conditional { | ||
return options.Inverse() | ||
} | ||
|
||
switch options.ParamStr(0) { | ||
case "success": | ||
return options.Fn() | ||
default: | ||
return options.Inverse() | ||
} | ||
} | ||
|
||
func isFailure(conditional bool, options *raymond.Options) string { | ||
if !conditional { | ||
return options.Inverse() | ||
} | ||
|
||
switch options.ParamStr(0) { | ||
case "failure", "error", "killed": | ||
return options.Fn() | ||
default: | ||
return options.Inverse() | ||
} | ||
} | ||
|
||
func truncate(s string, len int) string { | ||
if utf8.RuneCountInString(s) <= int(math.Abs(float64(len))) { | ||
return s | ||
} | ||
|
||
runes := []rune(s) | ||
|
||
if len < 0 { | ||
len = -len | ||
return string(runes[len:]) | ||
} | ||
|
||
return string(runes[:len]) | ||
} | ||
|
||
func urlencode(options *raymond.Options) string { | ||
return url.QueryEscape(options.Fn()) | ||
} | ||
|
||
func since(start int64) string { | ||
now := time.Unix(time.Now().Unix(), 0) | ||
return fmt.Sprint(now.Sub(time.Unix(start, 0))) | ||
} | ||
|
||
func uppercaseFirst(s string) string { | ||
a := []rune(s) | ||
|
||
a[0] = unicode.ToUpper(a[0]) | ||
s = string(a) | ||
|
||
return s | ||
} | ||
|
||
func regexReplace(pattern string, input string, replacement string) string { | ||
re := regexp.MustCompile(pattern) | ||
return re.ReplaceAllString(input, replacement) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package wd_template | ||
|
||
import ( | ||
"github.com/Masterminds/sprig/v3" | ||
"github.com/aymerick/raymond" | ||
"reflect" | ||
) | ||
|
||
// RegisterSettings | ||
// most of this can use wd_template.RegisterSettings(DefaultFunctions) | ||
func RegisterSettings(funcSettings map[string]interface{}) { | ||
if len(funcSettings) == 0 { | ||
funcSettings = map[string]interface{}{} | ||
} | ||
for name, function := range sprig.GenericFuncMap() { | ||
if invalidHelper(name) { | ||
continue | ||
} | ||
val := reflect.ValueOf(function) | ||
funcType := val.Type() | ||
if funcType.NumOut() != 1 { | ||
continue | ||
} | ||
|
||
funcSettings[name] = function | ||
} | ||
|
||
raymond.RegisterHelpers(funcSettings) | ||
} | ||
|
||
func invalidHelper(name string) bool { | ||
invalids := []string{ | ||
"buildCustomCert", | ||
"decryptAES", | ||
"derivePassword", | ||
"encryptAES", | ||
"fail", | ||
"genCA", | ||
"genPrivateKey", | ||
"genSelfSignedCert", | ||
"genSignedCert", | ||
"hello", | ||
"mustAppend", | ||
"mustCompact", | ||
"mustDateModify", | ||
"mustDeepCopy", | ||
"mustFirst", | ||
"mustHas", | ||
"mustInitial", | ||
"mustLast", | ||
"mustMerge", | ||
"mustMergeOverwrite", | ||
"mustPrepend", | ||
"mustPush", | ||
"mustRegexFind", | ||
"mustRegexFindAll", | ||
"mustRegexMatch", | ||
"mustRegexReplaceAll", | ||
"mustRegexReplaceAllLiteral", | ||
"mustRegexSplit", | ||
"mustRest", | ||
"mustReverse", | ||
"mustSlice", | ||
"mustToDate", | ||
"mustToJson", | ||
"mustToPrettyJson", | ||
"mustToRawJson", | ||
"mustUniq", | ||
"mustWithout", | ||
"must_date_modify", | ||
"semver", | ||
"semverCompare", | ||
"trimall", | ||
} | ||
|
||
for _, invalid := range invalids { | ||
if name == invalid { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.