Skip to content

Commit

Permalink
Add sha1 function
Browse files Browse the repository at this point in the history
  • Loading branch information
md5 committed Oct 29, 2014
1 parent 0bc11e7 commit e354c96
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
Expand All @@ -11,6 +12,7 @@ import (
"strings"
"syscall"
"text/template"
"crypto/sha1"
)

func exists(path string) (bool, error) {
Expand Down Expand Up @@ -72,6 +74,12 @@ func dict(values ...interface{}) (map[string]interface{}, error) {
return dict, nil
}

func hashSha1(input string) string {
h := sha1.New()
io.WriteString(h, input)
return fmt.Sprintf("%x", h.Sum(nil))
}

func generateFile(config Config, containers Context) bool {
templatePath := config.Template
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(template.FuncMap{
Expand All @@ -82,6 +90,7 @@ func generateFile(config Config, containers Context) bool {
"split": strings.Split,
"replace": strings.Replace,
"dict": dict,
"sha1": hashSha1,
}).ParseFiles(templatePath)
if err != nil {
log.Fatalf("unable to parse template: %s", err)
Expand Down
7 changes: 7 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,10 @@ func TestDict(t *testing.T) {
t.Fail()
}
}

func TestSha1(t *testing.T) {
sum := hashSha1("/path")
if sum != "4f26609ad3f5185faaa9edf1e93aa131e2131352" {
t.Fatal("Incorrect SHA1 sum")
}
}

0 comments on commit e354c96

Please sign in to comment.