diff --git a/settings/io.go b/settings/io.go index 2124117..f59e6db 100644 --- a/settings/io.go +++ b/settings/io.go @@ -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 } diff --git a/settings/io_test.go b/settings/io_test.go index a45d6e0..9224ba4 100755 --- a/settings/io_test.go +++ b/settings/io_test.go @@ -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) } diff --git a/settings/settings.go b/settings/settings.go index 2812352..ad4baa2 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -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 } diff --git a/settings/utils.go b/settings/utils.go index bf2a41a..62b570b 100644 --- a/settings/utils.go +++ b/settings/utils.go @@ -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<= 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) +} diff --git a/settings/utils_test.go b/settings/utils_test.go index 5a00f45..612c502 100644 --- a/settings/utils_test.go +++ b/settings/utils_test.go @@ -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 {