util.go 731 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package util
  2. import (
  3. "github.com/klauspost/compress/zstd"
  4. "os/exec"
  5. "strings"
  6. )
  7. func ExpandShellString(s string) (string, error) {
  8. if strings.Contains(s, "$") {
  9. // Start a shell to expand the environment variables
  10. cmd := exec.Command("sh", "-c", "echo "+s)
  11. out, err := cmd.Output()
  12. if err != nil {
  13. return "", err
  14. }
  15. s = strings.TrimSpace(string(out))
  16. }
  17. return s, nil
  18. }
  19. func ExpandShellStringInfallible(s string) string {
  20. expanded, err := ExpandShellString(s)
  21. if err != nil {
  22. return s
  23. }
  24. return expanded
  25. }
  26. func DecompressZstdBuffer(input []byte) ([]byte, error) {
  27. decoder, err := zstd.NewReader(nil)
  28. if err != nil {
  29. return nil, err
  30. }
  31. defer decoder.Close()
  32. return decoder.DecodeAll(input, nil)
  33. }