Skip to content

Commit e354c96

Browse files
committed
Add sha1 function
1 parent 0bc11e7 commit e354c96

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

template.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"errors"
6+
"fmt"
67
"io"
78
"io/ioutil"
89
"log"
@@ -11,6 +12,7 @@ import (
1112
"strings"
1213
"syscall"
1314
"text/template"
15+
"crypto/sha1"
1416
)
1517

1618
func exists(path string) (bool, error) {
@@ -72,6 +74,12 @@ func dict(values ...interface{}) (map[string]interface{}, error) {
7274
return dict, nil
7375
}
7476

77+
func hashSha1(input string) string {
78+
h := sha1.New()
79+
io.WriteString(h, input)
80+
return fmt.Sprintf("%x", h.Sum(nil))
81+
}
82+
7583
func generateFile(config Config, containers Context) bool {
7684
templatePath := config.Template
7785
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(template.FuncMap{
@@ -82,6 +90,7 @@ func generateFile(config Config, containers Context) bool {
8290
"split": strings.Split,
8391
"replace": strings.Replace,
8492
"dict": dict,
93+
"sha1": hashSha1,
8594
}).ParseFiles(templatePath)
8695
if err != nil {
8796
log.Fatalf("unable to parse template: %s", err)

template_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,10 @@ func TestDict(t *testing.T) {
132132
t.Fail()
133133
}
134134
}
135+
136+
func TestSha1(t *testing.T) {
137+
sum := hashSha1("/path")
138+
if sum != "4f26609ad3f5185faaa9edf1e93aa131e2131352" {
139+
t.Fatal("Incorrect SHA1 sum")
140+
}
141+
}

0 commit comments

Comments
 (0)