Skip to content

Commit

Permalink
Merge pull request #11 from maquanyi/develop
Browse files Browse the repository at this point in the history
Add Container function
  • Loading branch information
genedna committed Aug 7, 2015
2 parents 739791b + 4663f11 commit 9e1b40e
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit 9e1b40e

Please sign in to comment.