You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.6 KiB

  1. package util
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strconv"
  6. "strings"
  7. )
  8. func ParseInt(text string, defaultValue int) int {
  9. count, parseError := strconv.ParseInt(text, 10, 64)
  10. if parseError != nil {
  11. if len(text) > 0 {
  12. return 0
  13. }
  14. return defaultValue
  15. }
  16. return int(count)
  17. }
  18. func ParseUint64(text string, defaultValue uint64) uint64 {
  19. count, parseError := strconv.ParseUint(text, 10, 64)
  20. if parseError != nil {
  21. if len(text) > 0 {
  22. return 0
  23. }
  24. return defaultValue
  25. }
  26. return count
  27. }
  28. func ParseBool(s string, defaultValue bool) bool {
  29. value, err := strconv.ParseBool(s)
  30. if err != nil {
  31. return defaultValue
  32. }
  33. return value
  34. }
  35. func BoolToString(b bool) string {
  36. return strconv.FormatBool(b)
  37. }
  38. func ParseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  39. if !strings.HasPrefix(entryPath, "http://") && !strings.HasPrefix(entryPath, "https://") {
  40. entryPath = "http://" + entryPath
  41. }
  42. var u *url.URL
  43. u, err = url.Parse(entryPath)
  44. if err != nil {
  45. return
  46. }
  47. filerServer = u.Hostname()
  48. portString := u.Port()
  49. if portString != "" {
  50. filerPort, err = strconv.ParseInt(portString, 10, 32)
  51. }
  52. path = u.Path
  53. return
  54. }
  55. func ParseHostPort(hostPort string) (filerServer string, filerPort int64, err error) {
  56. parts := strings.Split(hostPort, ":")
  57. if len(parts) != 2 {
  58. err = fmt.Errorf("failed to parse %s\n", hostPort)
  59. return
  60. }
  61. filerPort, err = strconv.ParseInt(parts[1], 10, 64)
  62. if err == nil {
  63. filerServer = parts[0]
  64. }
  65. return
  66. }
  67. func CanonicalizeETag(etag string) string {
  68. canonicalETag := strings.TrimPrefix(etag, "\"")
  69. return strings.TrimSuffix(canonicalETag, "\"")
  70. }