2016-11-01 09:56:12 +00:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2017-08-27 11:45:46 +00:00
|
|
|
"math/rand"
|
2016-11-01 09:56:12 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-12 22:46:58 +00:00
|
|
|
"strings"
|
2017-04-13 13:49:51 +00:00
|
|
|
|
|
|
|
"github.com/tockins/realize/style"
|
2017-08-27 11:45:46 +00:00
|
|
|
"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"
|
2016-11-01 09:56:12 +00:00
|
|
|
)
|
|
|
|
|
2017-08-12 17:48:06 +00:00
|
|
|
// Wdir return the current working Directory
|
2016-11-01 09:56:12 +00:00
|
|
|
func (s Settings) Wdir() string {
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
s.Validate(err)
|
|
|
|
return filepath.Base(dir)
|
|
|
|
}
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Validate checks a fatal error
|
2016-11-01 09:56:12 +00:00
|
|
|
func (s Settings) Validate(err error) error {
|
|
|
|
if err != nil {
|
2016-12-16 22:53:27 +00:00
|
|
|
s.Fatal(err, "")
|
2016-11-01 09:56:12 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Fatal prints a fatal error with its additional messages
|
2016-12-16 22:53:27 +00:00
|
|
|
func (s Settings) Fatal(err error, msg ...interface{}) {
|
2016-12-25 15:32:17 +00:00
|
|
|
if len(msg) > 0 && err != nil {
|
2017-04-13 13:49:51 +00:00
|
|
|
log.Fatalln(style.Red.Regular(msg...), err.Error())
|
2016-12-25 15:32:17 +00:00
|
|
|
} else if err != nil {
|
|
|
|
log.Fatalln(err.Error())
|
2016-11-01 09:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-12 22:46:58 +00:00
|
|
|
|
2017-04-01 18:50:06 +00:00
|
|
|
// Name return the project name or the path of the working dir
|
|
|
|
func (s Settings) Name(name string, path string) string {
|
2017-03-12 22:46:58 +00:00
|
|
|
if name == "" && path == "" {
|
2017-04-01 18:50:06 +00:00
|
|
|
return s.Wdir()
|
2017-03-12 22:46:58 +00:00
|
|
|
} else if path != "/" {
|
|
|
|
return filepath.Base(path)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2017-04-01 18:50:06 +00:00
|
|
|
// Path cleaner
|
|
|
|
func (s Settings) Path(path string) string {
|
|
|
|
return strings.Replace(filepath.Clean(path), "\\", "/", -1)
|
2017-03-12 22:46:58 +00:00
|
|
|
}
|
2017-08-27 11:45:46 +00:00
|
|
|
|
2017-08-27 12:39:37 +00:00
|
|
|
// Rand is used for generate a random string
|
2017-08-27 11:45:46 +00:00
|
|
|
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)
|
|
|
|
}
|