rand string method replaced

This commit is contained in:
alessio 2017-08-27 13:45:46 +02:00
parent 2ad8e3ba69
commit 2427562245
5 changed files with 43 additions and 17 deletions

View File

@ -19,19 +19,19 @@ func (s Settings) Stream(file string) ([]byte, error) {
// Write a file // Write a file
func (s Settings) Write(name string, data []byte) error { func (s Settings) Write(name string, data []byte) error {
err := ioutil.WriteFile(name, data, Permission) err := ioutil.WriteFile(name, data, permission)
return s.Validate(err) return s.Validate(err)
} }
// Create a new file and return its pointer // Create a new file and return its pointer
func (s Settings) Create(path string, name string) *os.File { func (s Settings) Create(path string, name string) *os.File {
var file string var file string
if _, err := os.Stat(Directory); err == nil { if _, err := os.Stat(directory); err == nil {
file = filepath.Join(path, Directory, name) file = filepath.Join(path, directory, name)
} else { } else {
file = filepath.Join(path, name) file = filepath.Join(path, name)
} }
out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, Permission) out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, permission)
s.Validate(err) s.Validate(err)
return out return out
} }

View File

@ -1,7 +1,6 @@
package settings package settings
import ( import (
"github.com/labstack/gommon/random"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
@ -9,7 +8,7 @@ import (
func TestSettings_Stream(t *testing.T) { func TestSettings_Stream(t *testing.T) {
s := Settings{} s := Settings{}
filename := random.String(4) filename := Rand(4)
if _, err := s.Stream(filename); err == nil { if _, err := s.Stream(filename); err == nil {
t.Fatal("Error expected, none found", filename, err) t.Fatal("Error expected, none found", filename, err)
} }

View File

@ -3,13 +3,14 @@ package settings
import ( import (
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
"os" "os"
"path/filepath"
"time" "time"
) )
// settings const // settings const
const ( const (
Permission = 0775 permission = 0775
Directory = ".realize/" directory = ".realize"
) )
// Settings defines a group of general settings // Settings defines a group of general settings
@ -52,8 +53,9 @@ type Resources struct {
func (s *Settings) Read(out interface{}) error { func (s *Settings) Read(out interface{}) error {
localConfigPath := s.Resources.Config localConfigPath := s.Resources.Config
// backward compatibility // backward compatibility
if _, err := os.Stat(Directory + s.Resources.Config); err == nil { path := filepath.Join(directory, s.Resources.Config)
localConfigPath = Directory + s.Resources.Config if _, err := os.Stat(path); err == nil {
localConfigPath = path
} }
content, err := s.Stream(localConfigPath) content, err := s.Stream(localConfigPath)
if err == nil { if err == nil {
@ -70,12 +72,12 @@ func (s *Settings) Record(out interface{}) error {
if err != nil { if err != nil {
return err return err
} }
if _, err := os.Stat(Directory); os.IsNotExist(err) { if _, err := os.Stat(directory); os.IsNotExist(err) {
if err = os.Mkdir(Directory, Permission); err != nil { if err = os.Mkdir(directory, permission); err != nil {
return s.Write(s.Resources.Config, y) return s.Write(s.Resources.Config, y)
} }
} }
return s.Write(Directory+s.Resources.Config, y) return s.Write(filepath.Join(directory, s.Resources.Config), y)
} }
return nil return nil
} }

View File

@ -2,11 +2,20 @@ package settings
import ( import (
"log" "log"
"math/rand"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/tockins/realize/style" "github.com/tockins/realize/style"
"time"
)
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
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
) )
// Wdir return the current working Directory // Wdir return the current working Directory
@ -47,3 +56,20 @@ func (s Settings) Name(name string, path string) string {
func (s Settings) Path(path string) string { func (s Settings) Path(path string) string {
return strings.Replace(filepath.Clean(path), "\\", "/", -1) return strings.Replace(filepath.Clean(path), "\\", "/", -1)
} }
func Rand(n int) string {
src := rand.NewSource(time.Now().UnixNano())
b := make([]byte, n)
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(b)
}

View File

@ -2,7 +2,6 @@ package settings
import ( import (
"errors" "errors"
"github.com/labstack/gommon/random"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -32,8 +31,8 @@ func TestSettings_Validate(t *testing.T) {
func TestSettings_Name(t *testing.T) { func TestSettings_Name(t *testing.T) {
s := Settings{} s := Settings{}
name := random.String(8) name := Rand(8)
path := random.String(5) path := Rand(5)
dir, err := os.Getwd() dir, err := os.Getwd()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -47,7 +46,7 @@ func TestSettings_Name(t *testing.T) {
func TestSettings_Path(t *testing.T) { func TestSettings_Path(t *testing.T) {
s := Settings{} s := Settings{}
path := random.String(5) path := Rand(5)
expected := strings.Replace(filepath.Clean(path), "\\", "/", -1) expected := strings.Replace(filepath.Clean(path), "\\", "/", -1)
result := s.Path(path) result := s.Path(path)
if result != expected { if result != expected {