123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package util
- import (
- "context"
- "fmt"
- "os"
- "os/exec"
- "path/filepath"
- "regexp"
- "strconv"
- "strings"
- "time"
- "github.com/klauspost/compress/zstd"
- "github.com/minio/minio-go/v7"
- )
- func ExpandShellString(s string) (string, error) {
- if strings.Contains(s, "$") {
- // Start a shell to expand the environment variables
- cmd := exec.Command("sh", "-c", "echo "+s)
- out, err := cmd.Output()
- if err != nil {
- return "", err
- }
- s = strings.TrimSpace(string(out))
- }
- return s, nil
- }
- func ExpandShellStringInfallible(s string) string {
- expanded, err := ExpandShellString(s)
- if err != nil {
- return s
- }
- return expanded
- }
- func DecompressZstdBuffer(input []byte) ([]byte, error) {
- decoder, err := zstd.NewReader(nil)
- if err != nil {
- return nil, err
- }
- defer decoder.Close()
- return decoder.DecodeAll(input, nil)
- }
- func FmtMyTime(t time.Time) string {
- return t.Format("2006-01-02 15:04:05.000")
- }
- func FmtMyDate(t time.Time) string {
- return t.Format("2006-01-02")
- }
- func LastElem[T any](s []T) T {
- return s[len(s)-1]
- }
- func MinioObjectExists(minioClient *minio.Client, bucketName string, filePath string) (bool, error) {
- _, err := minioClient.StatObject(context.Background(), bucketName, filePath, minio.StatObjectOptions{})
- if err != nil {
- if minio.ToErrorResponse(err).Code == "NoSuchKey" {
- return false, nil
- }
- return false, fmt.Errorf("stat object failed: %w", err)
- }
- return true, nil
- }
- func MinioUploadFolder(minioClient *minio.Client, bucketName string, objPath string, localPath string) error {
- // Walk the local folder
- err := filepath.WalkDir(localPath, func(path string, d os.DirEntry, err error) error {
- if err != nil {
- return fmt.Errorf("walk dir failed: %w", err)
- }
- if d.IsDir() {
- return nil
- }
- relPath, err := filepath.Rel(localPath, path)
- if err != nil {
- return fmt.Errorf("get relative path failed: %w", err)
- }
- targetPath := filepath.Join(objPath, relPath)
- _, err = minioClient.FPutObject(context.Background(), bucketName, targetPath, path,
- minio.PutObjectOptions{ContentType: "application/octet-stream"})
- if err != nil {
- return fmt.Errorf("fput object failed: %w", err)
- }
- return nil
- })
- return err
- }
- func GetCurrentNanoTimestampUTC() int64 {
- return time.Now().UnixNano()
- }
- func ExtractNumberFromString(filename string) (int64, error) {
- re := regexp.MustCompile(`(\d+)`)
- match := re.FindStringSubmatch(filename)
- if len(match) < 2 {
- return 0, fmt.Errorf("number not found in filename `%s`", filename)
- }
- return strconv.ParseInt(match[1], 10, 64)
- }
- func ToSqlLiteral(s string) string {
- return "'" + strings.ReplaceAll(s, "'", "''") + "'"
- }
- type Pair[T, U any] struct {
- First T
- Second U
- }
|