diff --git a/README.md b/README.md index 467b0bb..19b6a62 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # essentials - aah framework [![Build Status](https://travis-ci.org/go-aah/essentials.svg?branch=master)](https://travis-ci.org/go-aah/essentials) [![codecov](https://codecov.io/gh/go-aah/essentials/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/essentials/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/essentials.v0)](https://goreportcard.com/report/aahframework.org/essentials.v0) -[![Version](https://img.shields.io/badge/version-0.4-blue.svg)](https://github.com/go-aah/essentials/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/essentials.v0?status.svg)](https://godoc.org/aahframework.org/essentials.v0) [![License](https://img.shields.io/github/license/go-aah/essentials.svg)](LICENSE) +[![Version](https://img.shields.io/badge/version-0.5-blue.svg)](https://github.com/go-aah/essentials/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/essentials.v0?status.svg)](https://godoc.org/aahframework.org/essentials.v0) [![License](https://img.shields.io/github/license/go-aah/essentials.svg)](LICENSE) -***v0.4 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Mar 30, 2017*** +***v0.5 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Apr 07, 2017*** `essentials` contains simple & useful utils methods for Go. aah framework utilizes essentials (aka `ess`) library across. Essentials library complements with handy methods, refer godoc to know more about methods: * filepath * GUID (Globally Unique Identifier) +* random string, random byte generation at fixed length * go * io * os diff --git a/archive_test.go b/archive_test.go new file mode 100644 index 0000000..99a12a1 --- /dev/null +++ b/archive_test.go @@ -0,0 +1,42 @@ +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) +// go-aah/essentials source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package ess + +import ( + "io/ioutil" + "testing" + + "aahframework.org/test.v0/assert" +) + +func TestArchiveZip(t *testing.T) { + // Prepare data for zip file + testdataPath := getTestdataPath() + path1 := join(testdataPath, "dirpaths", "level1", "level2", "level3") + path11 := join(testdataPath, "dirpaths", "level1", "level1-1") + path12 := join(testdataPath, "dirpaths", "level1", "level1-2") + path21 := join(testdataPath, "dirpaths", "level1", "level2", "level2-1") + path22 := join(testdataPath, "dirpaths", "level1", "level2", "level2-2") + defer DeleteFiles(join(testdataPath, "dirpaths")) + + _ = MkDirAll(path1, 0755) + _ = MkDirAll(path11, 0755) + _ = MkDirAll(path12, 0755) + _ = MkDirAll(path21, 0755) + _ = MkDirAll(path22, 0755) + + _ = ioutil.WriteFile(join(path1, "file1.txt"), []byte("file1.txt"), 0600) + _ = ioutil.WriteFile(join(path11, "file11.txt"), []byte("file11.txt"), 0600) + _ = ioutil.WriteFile(join(path12, "file12.txt"), []byte("file12.txt"), 0600) + _ = ioutil.WriteFile(join(path21, "file21.txt"), []byte("file21.txt"), 0600) + _ = ioutil.WriteFile(join(path22, "file22.txt"), []byte("file22.txt"), 0600) + + zipName := join(testdataPath, "testarchive.zip") + defer DeleteFiles(zipName) + + err := Zip(zipName, join(testdataPath, "dirpaths")) + assert.Nil(t, err) + assert.True(t, IsFileExists(zipName)) +} diff --git a/essentials.go b/essentials.go index 27719ee..97bdafe 100644 --- a/essentials.go +++ b/essentials.go @@ -7,4 +7,4 @@ package ess // Version no. of essentials library -var Version = "0.4" +var Version = "0.5" diff --git a/random_key.go b/random_key.go new file mode 100644 index 0000000..dc88003 --- /dev/null +++ b/random_key.go @@ -0,0 +1,97 @@ +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) +// go-aah/essentials source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package ess + +import ( + "crypto/rand" + "encoding/hex" + "io" + mrand "math/rand" + "sync" + "time" +) + +const ( + letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" + letterIdxBits = 6 // 6 bits to represent a letter index + letterIdxMask = 1<= 0; { + if remain == 0 { + cache, remain = randSrc(), letterIdxMax + } + if idx := int(cache & letterIdxMask); idx < len(letterBytes) { + b[i] = letterBytes[idx] + i-- + } + cache >>= letterIdxBits + remain-- + } + + return b +} + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// Unexported methods +//___________________________________ + +func randSrc() int64 { + mr.Lock() + defer mr.Unlock() + return mRandSrc.Int63() +} + +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +// init +//___________________________________ + +func init() { + mRandSrc = mrand.NewSource(time.Now().UnixNano()) + mr = &sync.Mutex{} +} diff --git a/random_key_test.go b/random_key_test.go new file mode 100644 index 0000000..f23222b --- /dev/null +++ b/random_key_test.go @@ -0,0 +1,43 @@ +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) +// go-aah/essentials source code and usage is governed by a MIT style +// license that can be found in the LICENSE file. + +package ess + +import ( + "testing" + + "aahframework.org/test.v0/assert" +) + +func TestEssRandomKey(t *testing.T) { + key1 := GenerateRandomKey(32) + assert.NotNil(t, key1) + assert.True(t, len(key1) == 32) + + key2 := GenerateRandomKeybm(64) + assert.NotNil(t, key2) + assert.True(t, len(key2) == 64) +} + +func TestEssRandomString(t *testing.T) { + str1 := RandomString(32) + assert.True(t, len(str1) == 32) + assert.NotNil(t, str1) + + str2 := RandomStringbm(32) + assert.True(t, len(str2) == 32) + assert.NotNil(t, str2) +} + +func BenchmarkGenerateRandomKey(b *testing.B) { + for i := 0; i < b.N; i++ { + GenerateRandomKey(16) + } +} + +func BenchmarkGenerateRandomKeybm(b *testing.B) { + for i := 0; i < b.N; i++ { + GenerateRandomKeybm(32) + } +}