rand string method replaced
This commit is contained in:
parent
2ad8e3ba69
commit
2427562245
|
@ -19,19 +19,19 @@ func (s Settings) Stream(file string) ([]byte, error) {
|
|||
|
||||
// Write a file
|
||||
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)
|
||||
}
|
||||
|
||||
// Create a new file and return its pointer
|
||||
func (s Settings) Create(path string, name string) *os.File {
|
||||
var file string
|
||||
if _, err := os.Stat(Directory); err == nil {
|
||||
file = filepath.Join(path, Directory, name)
|
||||
if _, err := os.Stat(directory); err == nil {
|
||||
file = filepath.Join(path, directory, name)
|
||||
} else {
|
||||
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)
|
||||
return out
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package settings
|
||||
|
||||
import (
|
||||
"github.com/labstack/gommon/random"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
@ -9,7 +8,7 @@ import (
|
|||
|
||||
func TestSettings_Stream(t *testing.T) {
|
||||
s := Settings{}
|
||||
filename := random.String(4)
|
||||
filename := Rand(4)
|
||||
if _, err := s.Stream(filename); err == nil {
|
||||
t.Fatal("Error expected, none found", filename, err)
|
||||
}
|
||||
|
|
|
@ -3,13 +3,14 @@ package settings
|
|||
import (
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
// settings const
|
||||
const (
|
||||
Permission = 0775
|
||||
Directory = ".realize/"
|
||||
permission = 0775
|
||||
directory = ".realize"
|
||||
)
|
||||
|
||||
// Settings defines a group of general settings
|
||||
|
@ -52,8 +53,9 @@ type Resources struct {
|
|||
func (s *Settings) Read(out interface{}) error {
|
||||
localConfigPath := s.Resources.Config
|
||||
// backward compatibility
|
||||
if _, err := os.Stat(Directory + s.Resources.Config); err == nil {
|
||||
localConfigPath = Directory + s.Resources.Config
|
||||
path := filepath.Join(directory, s.Resources.Config)
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
localConfigPath = path
|
||||
}
|
||||
content, err := s.Stream(localConfigPath)
|
||||
if err == nil {
|
||||
|
@ -70,12 +72,12 @@ func (s *Settings) Record(out interface{}) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := os.Stat(Directory); os.IsNotExist(err) {
|
||||
if err = os.Mkdir(Directory, Permission); err != nil {
|
||||
if _, err := os.Stat(directory); os.IsNotExist(err) {
|
||||
if err = os.Mkdir(directory, permission); err != nil {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -2,11 +2,20 @@ package settings
|
|||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"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
|
||||
|
@ -47,3 +56,20 @@ func (s Settings) Name(name string, path string) string {
|
|||
func (s Settings) Path(path string) string {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package settings
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/labstack/gommon/random"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -32,8 +31,8 @@ func TestSettings_Validate(t *testing.T) {
|
|||
|
||||
func TestSettings_Name(t *testing.T) {
|
||||
s := Settings{}
|
||||
name := random.String(8)
|
||||
path := random.String(5)
|
||||
name := Rand(8)
|
||||
path := Rand(5)
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -47,7 +46,7 @@ func TestSettings_Name(t *testing.T) {
|
|||
|
||||
func TestSettings_Path(t *testing.T) {
|
||||
s := Settings{}
|
||||
path := random.String(5)
|
||||
path := Rand(5)
|
||||
expected := strings.Replace(filepath.Clean(path), "\\", "/", -1)
|
||||
result := s.Path(path)
|
||||
if result != expected {
|
||||
|
|
Loading…
Reference in New Issue