util.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package awsutil
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/base64"
  6. "encoding/xml"
  7. "fmt"
  8. "github.com/ks3sdklib/aws-sdk-go/internal/protocol/xml/xmlutil"
  9. "go/format"
  10. "io"
  11. "os"
  12. "path/filepath"
  13. "reflect"
  14. "regexp"
  15. "strings"
  16. )
  17. // GoFmt returns the Go formated string of the input.
  18. //
  19. // Panics if the format fails.
  20. func GoFmt(buf string) string {
  21. formatted, err := format.Source([]byte(buf))
  22. if err != nil {
  23. panic(fmt.Errorf("%s\nOriginal code:\n%s", err.Error(), buf))
  24. }
  25. return string(formatted)
  26. }
  27. var reTrim = regexp.MustCompile(`\s{2,}`)
  28. // Trim removes all leading and trailing white space.
  29. //
  30. // All consecutive spaces will be reduced to a single space.
  31. func Trim(s string) string {
  32. return strings.TrimSpace(reTrim.ReplaceAllString(s, " "))
  33. }
  34. // Capitalize capitalizes the first character of the string.
  35. func Capitalize(s string) string {
  36. if len(s) == 1 {
  37. return strings.ToUpper(s)
  38. }
  39. return strings.ToUpper(s[0:1]) + s[1:]
  40. }
  41. // SortXML sorts the reader's XML elements
  42. func SortXML(r io.Reader) string {
  43. var buf bytes.Buffer
  44. d := xml.NewDecoder(r)
  45. root, _ := xmlutil.XMLToStruct(d, nil)
  46. e := xml.NewEncoder(&buf)
  47. xmlutil.StructToXML(e, root, true)
  48. return buf.String()
  49. }
  50. // PrettyPrint generates a human readable representation of the value v.
  51. // All values of v are recursively found and pretty printed also.
  52. func PrettyPrint(v interface{}) string {
  53. value := reflect.ValueOf(v)
  54. switch value.Kind() {
  55. case reflect.Struct:
  56. str := fullName(value.Type()) + "{\n"
  57. for i := 0; i < value.NumField(); i++ {
  58. l := string(value.Type().Field(i).Name[0])
  59. if strings.ToUpper(l) == l {
  60. str += value.Type().Field(i).Name + ": "
  61. str += PrettyPrint(value.Field(i).Interface())
  62. str += ",\n"
  63. }
  64. }
  65. str += "}"
  66. return str
  67. case reflect.Map:
  68. str := "map[" + fullName(value.Type().Key()) + "]" + fullName(value.Type().Elem()) + "{\n"
  69. for _, k := range value.MapKeys() {
  70. str += "\"" + k.String() + "\": "
  71. str += PrettyPrint(value.MapIndex(k).Interface())
  72. str += ",\n"
  73. }
  74. str += "}"
  75. return str
  76. case reflect.Ptr:
  77. if e := value.Elem(); e.IsValid() {
  78. return "&" + PrettyPrint(e.Interface())
  79. }
  80. return "nil"
  81. case reflect.Slice:
  82. str := "[]" + fullName(value.Type().Elem()) + "{\n"
  83. for i := 0; i < value.Len(); i++ {
  84. str += PrettyPrint(value.Index(i).Interface())
  85. str += ",\n"
  86. }
  87. str += "}"
  88. return str
  89. default:
  90. return fmt.Sprintf("%#v", v)
  91. }
  92. }
  93. func pkgName(t reflect.Type) string {
  94. pkg := t.PkgPath()
  95. c := strings.Split(pkg, "/")
  96. return c[len(c)-1]
  97. }
  98. func fullName(t reflect.Type) string {
  99. if pkg := pkgName(t); pkg != "" {
  100. return pkg + "." + t.Name()
  101. }
  102. return t.Name()
  103. }
  104. //获取指定目录及所有子目录下的所有文件,可以匹配后缀过滤。
  105. func WalkDir(dirPth, suffix string) (files []string, err error) {
  106. files = make([]string, 0, 30)
  107. suffix = strings.ToUpper(suffix) //忽略后缀匹配的大小写
  108. err = filepath.Walk(dirPth, func(filename string, fi os.FileInfo, err error) error { //遍历目录
  109. //if err != nil { //忽略错误
  110. // return err
  111. //}
  112. if fi.IsDir() { // 忽略目录
  113. return nil
  114. }
  115. if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
  116. files = append(files, filename)
  117. }
  118. return nil
  119. })
  120. return files, err
  121. }
  122. func IsHidden(path string) bool {
  123. fileInfo, err := os.Stat(path)
  124. if err != nil {
  125. return false
  126. }
  127. return fileInfo.Name()[0] == '.'
  128. }
  129. func ComputeMD5Hash(input []byte) []byte {
  130. h := md5.New()
  131. h.Write(input)
  132. return h.Sum(nil)
  133. }
  134. func EncodeAsString(bytes []byte) string {
  135. return base64.StdEncoding.EncodeToString(bytes)
  136. }