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 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) }