From 4663f11c7fcc03a9c467f3f5fa640b31b06105f3 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 8 Aug 2015 00:10:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Container=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/utils.go | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index 56e0b56..73214ef 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -4,18 +4,51 @@ import ( "crypto/md5" "encoding/base64" "encoding/hex" + "errors" "fmt" "os" + "reflect" "regexp" "strings" "time" ) -func Exist(filename string) bool { +func IsDirExist(path string) bool { + fi, err := os.Stat(path) + + if err != nil { + return os.IsExist(err) + } else { + return fi.IsDir() + } + + panic("not reached") +} + +func IsFileExist(filename string) bool { _, err := os.Stat(filename) return err == nil || os.IsExist(err) } +func Contain(obj interface{}, target interface{}) (bool, error) { + targetValue := reflect.ValueOf(target) + + switch reflect.TypeOf(target).Kind() { + case reflect.Slice, reflect.Array: + for i := 0; i < targetValue.Len(); i++ { + if targetValue.Index(i).Interface() == obj { + return true, nil + } + } + case reflect.Map: + if targetValue.MapIndex(reflect.ValueOf(obj)).IsValid() { + return true, nil + } + } + + return false, errors.New("not in array") +} + func EncodeBasicAuth(username string, password string) string { auth := username + ":" + password msg := []byte(auth) @@ -53,18 +86,6 @@ func DecodeBasicAuth(authorization string) (username string, password string, er return username, password, nil } -func IsDirExists(path string) bool { - fi, err := os.Stat(path) - - if err != nil { - return os.IsExist(err) - } else { - return fi.IsDir() - } - - panic("not reached") -} - func ValidatePassword(password string) error { if valida, _ := regexp.MatchString("[:alpha:]", password); valida != true { return fmt.Errorf("No alpha character in the password.")