Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gzip and ungzip functions #379

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sprig

import (
"bytes"
"compress/gzip"
"crypto"
"crypto/aes"
"crypto/cipher"
Expand Down Expand Up @@ -657,3 +658,37 @@ func decryptAES(password string, crypt64 string) (string, error) {

return string(decrypted[:len(decrypted)-int(decrypted[len(decrypted)-1])]), nil
}

func gzipCompress(input string) (string, error) {
var buffer bytes.Buffer
gzipWriter := gzip.NewWriter(&buffer)
_, err := gzipWriter.Write([]byte(input))
if err != nil {
return "", err
}

if err := gzipWriter.Close(); err != nil {
return "", err
}

return buffer.String(), nil
}

func gzipDecompress(input string) (string, error) {
gzipReader, err := gzip.NewReader(bytes.NewReader([]byte(input)))
if err != nil {
return "", err
}

var buffer bytes.Buffer
_, err = buffer.ReadFrom(gzipReader)
if err != nil {
return "", err
}

if err := gzipReader.Close(); err != nil {
return "", err
}

return buffer.String(), nil
}
7 changes: 7 additions & 0 deletions crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,10 @@ func TestEncryptDecryptAES(t *testing.T) {
t.Error(err)
}
}

func TestCompressDecompressGzip(t *testing.T) {
tpl := `{{"plaintext" | gzip | ungzip }}`
if err := runt(tpl, "plaintext"); err != nil {
t.Error(err)
}
}
2 changes: 2 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ var genericMap = map[string]interface{}{
"encryptAES": encryptAES,
"decryptAES": decryptAES,
"randBytes": randBytes,
"gzip": gzipCompress,
"ungzip": gzipDecompress,

// UUIDs:
"uuidv4": uuidv4,
Expand Down