Skip to content

Commit

Permalink
Add missing files.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio committed Apr 26, 2019
1 parent 746e8f6 commit a11dccc
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
7 changes: 7 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

type enotfounderr struct{ name string }

func (e *enotfounderr) Error() string {
return "strict mode on: environment variable not found: $" + e.name
}
92 changes: 92 additions & 0 deletions template_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"fmt"
"html/template"
"math/rand"
"os"
"strings"
"time"
"unsafe"
)

var templateFunctions = template.FuncMap{
"env": envfunc,
"raw": func(s string) string {
return s
},

"sprintf": func(s string, args ...interface{}) string {
return fmt.Sprintf(s, args...)
},

"envdefault": func(k, defval string) (string, error) {
s, err := envfunc(k)

if err != nil {
if _, ok := err.(*enotfounderr); ok {
return defval, nil
}

return "", err
}

if s != "" {
return s, nil
}

return defval, nil
},

"rndstring": rndgen,
"lowercase": strings.ToLower,
"uppercase": strings.ToUpper,
"title": strings.Title,
}

func envfunc(k string) (string, error) {
k = strings.ToUpper(k)

if v, found := os.LookupEnv(k); found {
return v, nil
}

if v, found := envvars[k]; found {
return v, nil
}

if strict {
return "", &enotfounderr{name: k}
}

return "", nil
}

// from: https://stackoverflow.com/a/31832326

var src = rand.NewSource(time.Now().UnixNano())

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)

func rndgen(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}

return *(*string)(unsafe.Pointer(&b))
}

0 comments on commit a11dccc

Please sign in to comment.