mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-12-01 12:50:00 +01:00
Clean code
This commit is contained in:
parent
f0d5392e9e
commit
8b4834929d
5 changed files with 287 additions and 269 deletions
7
util/sprig/flow_control.go
Normal file
7
util/sprig/flow_control.go
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package sprig
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
func fail(msg string) (string, error) {
|
||||||
|
return "", errors.New(msg)
|
||||||
|
}
|
||||||
|
|
@ -1,14 +1,9 @@
|
||||||
package sprig
|
package sprig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"golang.org/x/text/cases"
|
|
||||||
"golang.org/x/text/language"
|
|
||||||
"math/rand"
|
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -27,14 +22,7 @@ const (
|
||||||
//
|
//
|
||||||
// TxtFuncMap returns a 'text/template'.FuncMap
|
// TxtFuncMap returns a 'text/template'.FuncMap
|
||||||
func TxtFuncMap() template.FuncMap {
|
func TxtFuncMap() template.FuncMap {
|
||||||
gfm := make(map[string]any, len(genericMap))
|
return map[string]any{
|
||||||
for k, v := range genericMap {
|
|
||||||
gfm[k] = v
|
|
||||||
}
|
|
||||||
return gfm
|
|
||||||
}
|
|
||||||
|
|
||||||
var genericMap = map[string]any{
|
|
||||||
// Date functions
|
// Date functions
|
||||||
"ago": dateAgo,
|
"ago": dateAgo,
|
||||||
"date": date,
|
"date": date,
|
||||||
|
|
@ -58,19 +46,15 @@ var genericMap = map[string]any{
|
||||||
"trim": strings.TrimSpace,
|
"trim": strings.TrimSpace,
|
||||||
"upper": strings.ToUpper,
|
"upper": strings.ToUpper,
|
||||||
"lower": strings.ToLower,
|
"lower": strings.ToLower,
|
||||||
"title": func(s string) string {
|
"title": title,
|
||||||
return cases.Title(language.English).String(s)
|
|
||||||
},
|
|
||||||
"substr": substring,
|
"substr": substring,
|
||||||
// Switch order so that "foo" | repeat 5
|
|
||||||
"repeat": repeat,
|
"repeat": repeat,
|
||||||
"trimAll": func(a, b string) string { return strings.Trim(b, a) },
|
"trimAll": trimAll,
|
||||||
"trimSuffix": func(a, b string) string { return strings.TrimSuffix(b, a) },
|
"trimPrefix": trimPrefix,
|
||||||
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) },
|
"trimSuffix": trimSuffix,
|
||||||
// Switch order so that "foobar" | contains "foo"
|
"contains": contains,
|
||||||
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
|
"hasPrefix": hasPrefix,
|
||||||
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
|
"hasSuffix": hasSuffix,
|
||||||
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
|
|
||||||
"quote": quote,
|
"quote": quote,
|
||||||
"squote": squote,
|
"squote": squote,
|
||||||
"cat": cat,
|
"cat": cat,
|
||||||
|
|
@ -85,45 +69,30 @@ var genericMap = map[string]any{
|
||||||
"toString": strval,
|
"toString": strval,
|
||||||
|
|
||||||
// Wrap Atoi to stop errors.
|
// Wrap Atoi to stop errors.
|
||||||
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },
|
"atoi": atoi,
|
||||||
"seq": seq,
|
"seq": seq,
|
||||||
"toDecimal": toDecimal,
|
"toDecimal": toDecimal,
|
||||||
|
|
||||||
// split "/" foo/bar returns map[int]string{0: foo, 1: bar}
|
|
||||||
"split": split,
|
"split": split,
|
||||||
"splitList": func(sep, orig string) []string { return strings.Split(orig, sep) },
|
"splitList": splitList,
|
||||||
// splitn "/" foo/bar/fuu returns map[int]string{0: foo, 1: bar/fuu}
|
|
||||||
"splitn": splitn,
|
"splitn": splitn,
|
||||||
"toStrings": strslice,
|
"toStrings": strslice,
|
||||||
|
|
||||||
"until": until,
|
"until": until,
|
||||||
"untilStep": untilStep,
|
"untilStep": untilStep,
|
||||||
|
|
||||||
// VERY basic arithmetic.
|
// Basic arithmetic
|
||||||
"add1": func(i any) int64 { return toInt64(i) + 1 },
|
"add1": add1,
|
||||||
"add": func(i ...any) int64 {
|
"add": add,
|
||||||
var a int64 = 0
|
"sub": sub,
|
||||||
for _, b := range i {
|
"div": div,
|
||||||
a += toInt64(b)
|
"mod": mod,
|
||||||
}
|
"mul": mul,
|
||||||
return a
|
"randInt": randInt,
|
||||||
},
|
"biggest": maxAsInt64,
|
||||||
"sub": func(a, b any) int64 { return toInt64(a) - toInt64(b) },
|
"max": maxAsInt64,
|
||||||
"div": func(a, b any) int64 { return toInt64(a) / toInt64(b) },
|
"min": minAsInt64,
|
||||||
"mod": func(a, b any) int64 { return toInt64(a) % toInt64(b) },
|
"maxf": maxAsFloat64,
|
||||||
"mul": func(a any, v ...any) int64 {
|
"minf": minAsFloat64,
|
||||||
val := toInt64(a)
|
|
||||||
for _, b := range v {
|
|
||||||
val = val * toInt64(b)
|
|
||||||
}
|
|
||||||
return val
|
|
||||||
},
|
|
||||||
"randInt": func(min, max int) int { return rand.Intn(max-min) + min },
|
|
||||||
"biggest": max,
|
|
||||||
"max": max,
|
|
||||||
"min": min,
|
|
||||||
"maxf": maxf,
|
|
||||||
"minf": minf,
|
|
||||||
"ceil": ceil,
|
"ceil": ceil,
|
||||||
"floor": floor,
|
"floor": floor,
|
||||||
"round": round,
|
"round": round,
|
||||||
|
|
@ -159,27 +128,27 @@ var genericMap = map[string]any{
|
||||||
"kindIs": kindIs,
|
"kindIs": kindIs,
|
||||||
"deepEqual": reflect.DeepEqual,
|
"deepEqual": reflect.DeepEqual,
|
||||||
|
|
||||||
// Paths:
|
// Paths
|
||||||
"base": path.Base,
|
"base": path.Base,
|
||||||
"dir": path.Dir,
|
"dir": path.Dir,
|
||||||
"clean": path.Clean,
|
"clean": path.Clean,
|
||||||
"ext": path.Ext,
|
"ext": path.Ext,
|
||||||
"isAbs": path.IsAbs,
|
"isAbs": path.IsAbs,
|
||||||
|
|
||||||
// Filepaths:
|
// Filepaths
|
||||||
"osBase": filepath.Base,
|
"osBase": filepath.Base,
|
||||||
"osClean": filepath.Clean,
|
"osClean": filepath.Clean,
|
||||||
"osDir": filepath.Dir,
|
"osDir": filepath.Dir,
|
||||||
"osExt": filepath.Ext,
|
"osExt": filepath.Ext,
|
||||||
"osIsAbs": filepath.IsAbs,
|
"osIsAbs": filepath.IsAbs,
|
||||||
|
|
||||||
// Encoding:
|
// Encoding
|
||||||
"b64enc": base64encode,
|
"b64enc": base64encode,
|
||||||
"b64dec": base64decode,
|
"b64dec": base64decode,
|
||||||
"b32enc": base32encode,
|
"b32enc": base32encode,
|
||||||
"b32dec": base32decode,
|
"b32dec": base32decode,
|
||||||
|
|
||||||
// Data Structures:
|
// Data Structures
|
||||||
"tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.
|
"tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.
|
||||||
"list": list,
|
"list": list,
|
||||||
"dict": dict,
|
"dict": dict,
|
||||||
|
|
@ -222,8 +191,8 @@ var genericMap = map[string]any{
|
||||||
"chunk": chunk,
|
"chunk": chunk,
|
||||||
"mustChunk": mustChunk,
|
"mustChunk": mustChunk,
|
||||||
|
|
||||||
// Flow Control:
|
// Flow Control
|
||||||
"fail": func(msg string) (string, error) { return "", errors.New(msg) },
|
"fail": fail,
|
||||||
|
|
||||||
// Regex
|
// Regex
|
||||||
"regexMatch": regexMatch,
|
"regexMatch": regexMatch,
|
||||||
|
|
@ -240,7 +209,8 @@ var genericMap = map[string]any{
|
||||||
"mustRegexSplit": mustRegexSplit,
|
"mustRegexSplit": mustRegexSplit,
|
||||||
"regexQuoteMeta": regexQuoteMeta,
|
"regexQuoteMeta": regexQuoteMeta,
|
||||||
|
|
||||||
// URLs:
|
// URLs
|
||||||
"urlParse": urlParse,
|
"urlParse": urlParse,
|
||||||
"urlJoin": urlJoin,
|
"urlJoin": urlJoin,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
package sprig
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestOsBase(t *testing.T) {
|
|
||||||
assert.NoError(t, runt(`{{ osBase "C:\\foo\\bar" }}`, "bar"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestOsDir(t *testing.T) {
|
|
||||||
assert.NoError(t, runt(`{{ osDir "C:\\foo\\bar\\baz" }}`, "C:\\foo\\bar"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestOsIsAbs(t *testing.T) {
|
|
||||||
assert.NoError(t, runt(`{{ osIsAbs "C:\\foo" }}`, "true"))
|
|
||||||
assert.NoError(t, runt(`{{ osIsAbs "foo" }}`, "false"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestOsClean(t *testing.T) {
|
|
||||||
assert.NoError(t, runt(`{{ osClean "C:\\foo\\..\\foo\\..\\bar" }}`, "C:\\bar"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestOsExt(t *testing.T) {
|
|
||||||
assert.NoError(t, runt(`{{ osExt "C:\\foo\\bar\\baz.txt" }}`, ".txt"))
|
|
||||||
}
|
|
||||||
|
|
@ -3,6 +3,7 @@ package sprig
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -78,7 +79,43 @@ func toInt64(v any) int64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func max(a any, i ...any) int64 {
|
func add1(i any) int64 {
|
||||||
|
return toInt64(i) + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func add(i ...any) int64 {
|
||||||
|
var a int64
|
||||||
|
for _, b := range i {
|
||||||
|
a += toInt64(b)
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func sub(a, b any) int64 {
|
||||||
|
return toInt64(a) - toInt64(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func div(a, b any) int64 {
|
||||||
|
return toInt64(a) / toInt64(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mod(a, b any) int64 {
|
||||||
|
return toInt64(a) % toInt64(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mul(a any, v ...any) int64 {
|
||||||
|
val := toInt64(a)
|
||||||
|
for _, b := range v {
|
||||||
|
val = val * toInt64(b)
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func randInt(min, max int) int {
|
||||||
|
return rand.Intn(max-min) + min
|
||||||
|
}
|
||||||
|
|
||||||
|
func maxAsInt64(a any, i ...any) int64 {
|
||||||
aa := toInt64(a)
|
aa := toInt64(a)
|
||||||
for _, b := range i {
|
for _, b := range i {
|
||||||
bb := toInt64(b)
|
bb := toInt64(b)
|
||||||
|
|
@ -89,16 +126,15 @@ func max(a any, i ...any) int64 {
|
||||||
return aa
|
return aa
|
||||||
}
|
}
|
||||||
|
|
||||||
func maxf(a any, i ...any) float64 {
|
func maxAsFloat64(a any, i ...any) float64 {
|
||||||
aa := toFloat64(a)
|
m := toFloat64(a)
|
||||||
for _, b := range i {
|
for _, b := range i {
|
||||||
bb := toFloat64(b)
|
m = math.Max(m, toFloat64(b))
|
||||||
aa = math.Max(aa, bb)
|
|
||||||
}
|
}
|
||||||
return aa
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func min(a any, i ...any) int64 {
|
func minAsInt64(a any, i ...any) int64 {
|
||||||
aa := toInt64(a)
|
aa := toInt64(a)
|
||||||
for _, b := range i {
|
for _, b := range i {
|
||||||
bb := toInt64(b)
|
bb := toInt64(b)
|
||||||
|
|
@ -109,13 +145,12 @@ func min(a any, i ...any) int64 {
|
||||||
return aa
|
return aa
|
||||||
}
|
}
|
||||||
|
|
||||||
func minf(a any, i ...any) float64 {
|
func minAsFloat64(a any, i ...any) float64 {
|
||||||
aa := toFloat64(a)
|
m := toFloat64(a)
|
||||||
for _, b := range i {
|
for _, b := range i {
|
||||||
bb := toFloat64(b)
|
m = math.Min(m, toFloat64(b))
|
||||||
aa = math.Min(aa, bb)
|
|
||||||
}
|
}
|
||||||
return aa
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func until(count int) []int {
|
func until(count int) []int {
|
||||||
|
|
@ -131,12 +166,10 @@ func untilStep(start, stop, step int) []int {
|
||||||
if step == 0 {
|
if step == 0 {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
iterations := math.Abs(float64(stop)-float64(start)) / float64(step)
|
iterations := math.Abs(float64(stop)-float64(start)) / float64(step)
|
||||||
if iterations > loopExecutionLimit {
|
if iterations > loopExecutionLimit {
|
||||||
panic(fmt.Sprintf("too many iterations in untilStep; max allowed is %d, got %f", loopExecutionLimit, iterations))
|
panic(fmt.Sprintf("too many iterations in untilStep; max allowed is %d, got %f", loopExecutionLimit, iterations))
|
||||||
}
|
}
|
||||||
|
|
||||||
if stop < start {
|
if stop < start {
|
||||||
if step >= 0 {
|
if step >= 0 {
|
||||||
return v
|
return v
|
||||||
|
|
@ -146,7 +179,6 @@ func untilStep(start, stop, step int) []int {
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
if step <= 0 {
|
if step <= 0 {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
@ -157,13 +189,11 @@ func untilStep(start, stop, step int) []int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func floor(a any) float64 {
|
func floor(a any) float64 {
|
||||||
aa := toFloat64(a)
|
return math.Floor(toFloat64(a))
|
||||||
return math.Floor(aa)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ceil(a any) float64 {
|
func ceil(a any) float64 {
|
||||||
aa := toFloat64(a)
|
return math.Ceil(toFloat64(a))
|
||||||
return math.Ceil(aa)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func round(a any, p int, rOpt ...float64) float64 {
|
func round(a any, p int, rOpt ...float64) float64 {
|
||||||
|
|
@ -195,6 +225,11 @@ func toDecimal(v any) int64 {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func atoi(a string) int {
|
||||||
|
i, _ := strconv.Atoi(a)
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
func seq(params ...int) string {
|
func seq(params ...int) string {
|
||||||
increment := 1
|
increment := 1
|
||||||
switch len(params) {
|
switch len(params) {
|
||||||
|
|
@ -231,6 +266,6 @@ func seq(params ...int) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func intArrayToString(slice []int, delimeter string) string {
|
func intArrayToString(slice []int, delimiter string) string {
|
||||||
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(slice)), delimeter), "[]")
|
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(slice)), delimiter), "[]")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"encoding/base32"
|
"encoding/base32"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"golang.org/x/text/cases"
|
||||||
|
"golang.org/x/text/language"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -149,6 +151,10 @@ func trunc(c int, s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func title(s string) string {
|
||||||
|
return cases.Title(language.English).String(s)
|
||||||
|
}
|
||||||
|
|
||||||
func join(sep string, v any) string {
|
func join(sep string, v any) string {
|
||||||
return strings.Join(strslice(v), sep)
|
return strings.Join(strslice(v), sep)
|
||||||
}
|
}
|
||||||
|
|
@ -162,6 +168,10 @@ func split(sep, orig string) map[string]string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func splitList(sep, orig string) []string {
|
||||||
|
return strings.Split(orig, sep)
|
||||||
|
}
|
||||||
|
|
||||||
func splitn(sep string, n int, orig string) map[string]string {
|
func splitn(sep string, n int, orig string) map[string]string {
|
||||||
parts := strings.SplitN(orig, sep, n)
|
parts := strings.SplitN(orig, sep, n)
|
||||||
res := make(map[string]string, len(parts))
|
res := make(map[string]string, len(parts))
|
||||||
|
|
@ -196,3 +206,27 @@ func repeat(count int, str string) string {
|
||||||
}
|
}
|
||||||
return strings.Repeat(str, count)
|
return strings.Repeat(str, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func trimAll(a, b string) string {
|
||||||
|
return strings.Trim(b, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimPrefix(a, b string) string {
|
||||||
|
return strings.TrimPrefix(b, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimSuffix(a, b string) string {
|
||||||
|
return strings.TrimSuffix(b, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func contains(substr string, str string) bool {
|
||||||
|
return strings.Contains(str, substr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasPrefix(substr string, str string) bool {
|
||||||
|
return strings.HasPrefix(str, substr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasSuffix(substr string, str string) bool {
|
||||||
|
return strings.HasSuffix(str, substr)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue