utils.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package misc
  2. import (
  3. "bytes"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. )
  8. var (
  9. keyValue = regexp.MustCompile(`^.+(=|:) *(.+)?$`)
  10. )
  11. // TwoSpotValue returns value from "key:value" or "key: value" line. The value will be not contains spaces
  12. //
  13. // Deprecated: use RexKeyValue instead
  14. func TwoSpotValue(line string) string {
  15. return strings.TrimSpace(strings.SplitN(line, ":", 2)[1])
  16. }
  17. // RexKeyValue returns value from "key:value" or "key: value" or "key=value" or "key = value" line. The value will be not contains spaces
  18. func RexKeyValue(line string) string {
  19. res := keyValue.FindStringSubmatch(strings.TrimSpace(line))
  20. return res[len(res)-1]
  21. }
  22. // RexSubStrSingleVal returns value in last position of out
  23. func RexSubStrSingleVal(out []string) string {
  24. if len(out) != 2 {
  25. return ""
  26. } else {
  27. return out[1]
  28. }
  29. }
  30. // RemoveQuoteMark removes quote marks
  31. func RemoveQuoteMark(line string) string {
  32. return strings.ReplaceAll(strings.ReplaceAll(line, "\"", ""), "'", "")
  33. }
  34. // IntExtractor returns int value from string represents of int
  35. func IntExtractor(val string) int {
  36. if res, err := strconv.Atoi(val); err != nil {
  37. return -1
  38. } else {
  39. return res
  40. }
  41. }
  42. // RemoveBraces remove braces from line
  43. func RemoveBraces(line string) string {
  44. line = strings.ReplaceAll(strings.ReplaceAll(line, "(", ""), ")", "")
  45. line = strings.ReplaceAll(strings.ReplaceAll(line, "{", ""), "}", "")
  46. line = strings.ReplaceAll(strings.ReplaceAll(line, "[", ""), "]", "")
  47. return line
  48. }
  49. func TrimOut(out []byte, err error) ([]byte, error) {
  50. out = bytes.TrimSpace(out)
  51. return out, err
  52. }