util.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package util
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/klauspost/compress/zstd"
  13. "github.com/minio/minio-go/v7"
  14. )
  15. func ExpandShellString(s string) (string, error) {
  16. if strings.Contains(s, "$") {
  17. // Start a shell to expand the environment variables
  18. cmd := exec.Command("sh", "-c", "echo "+s)
  19. out, err := cmd.Output()
  20. if err != nil {
  21. return "", err
  22. }
  23. s = strings.TrimSpace(string(out))
  24. }
  25. return s, nil
  26. }
  27. func ExpandShellStringInfallible(s string) string {
  28. expanded, err := ExpandShellString(s)
  29. if err != nil {
  30. return s
  31. }
  32. return expanded
  33. }
  34. func DecompressZstdBuffer(input []byte) ([]byte, error) {
  35. decoder, err := zstd.NewReader(nil)
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer decoder.Close()
  40. return decoder.DecodeAll(input, nil)
  41. }
  42. func FmtMyTime(t time.Time) string {
  43. return t.Format("2006-01-02 15:04:05.000")
  44. }
  45. func FmtMyDate(t time.Time) string {
  46. return t.Format("2006-01-02")
  47. }
  48. func LastElem[T any](s []T) T {
  49. return s[len(s)-1]
  50. }
  51. func MinioObjectExists(minioClient *minio.Client, bucketName string, filePath string) (bool, error) {
  52. _, err := minioClient.StatObject(context.Background(), bucketName, filePath, minio.StatObjectOptions{})
  53. if err != nil {
  54. if minio.ToErrorResponse(err).Code == "NoSuchKey" {
  55. return false, nil
  56. }
  57. return false, fmt.Errorf("stat object failed: %w", err)
  58. }
  59. return true, nil
  60. }
  61. func MinioUploadFolder(minioClient *minio.Client, bucketName string, objPath string, localPath string) error {
  62. // Walk the local folder
  63. err := filepath.WalkDir(localPath, func(path string, d os.DirEntry, err error) error {
  64. if err != nil {
  65. return fmt.Errorf("walk dir failed: %w", err)
  66. }
  67. if d.IsDir() {
  68. return nil
  69. }
  70. relPath, err := filepath.Rel(localPath, path)
  71. if err != nil {
  72. return fmt.Errorf("get relative path failed: %w", err)
  73. }
  74. targetPath := filepath.Join(objPath, relPath)
  75. _, err = minioClient.FPutObject(context.Background(), bucketName, targetPath, path,
  76. minio.PutObjectOptions{ContentType: "application/octet-stream"})
  77. if err != nil {
  78. return fmt.Errorf("fput object failed: %w", err)
  79. }
  80. return nil
  81. })
  82. return err
  83. }
  84. func GetCurrentNanoTimestampUTC() int64 {
  85. return time.Now().UnixNano()
  86. }
  87. func ExtractNumberFromString(filename string) (int64, error) {
  88. re := regexp.MustCompile(`(\d+)`)
  89. match := re.FindStringSubmatch(filename)
  90. if len(match) < 2 {
  91. return 0, fmt.Errorf("number not found in filename `%s`", filename)
  92. }
  93. return strconv.ParseInt(match[1], 10, 64)
  94. }
  95. func ToSqlLiteral(s string) string {
  96. return "'" + strings.ReplaceAll(s, "'", "''") + "'"
  97. }