You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package os
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. )
  10. type logger interface {
  11. Info(msg string, keyvals ...interface{})
  12. }
  13. // TrapSignal catches the SIGTERM/SIGINT and executes cb function. After that it exits
  14. // with code 0.
  15. func TrapSignal(logger logger, cb func()) {
  16. c := make(chan os.Signal, 1)
  17. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  18. go func() {
  19. for sig := range c {
  20. logger.Info(fmt.Sprintf("captured %v, exiting...", sig))
  21. if cb != nil {
  22. cb()
  23. }
  24. os.Exit(0)
  25. }
  26. }()
  27. }
  28. // Kill the running process by sending itself SIGTERM.
  29. func Kill() error {
  30. p, err := os.FindProcess(os.Getpid())
  31. if err != nil {
  32. return err
  33. }
  34. return p.Signal(syscall.SIGTERM)
  35. }
  36. func Exit(s string) {
  37. fmt.Printf(s + "\n")
  38. os.Exit(1)
  39. }
  40. // EnsureDir ensures the given directory exists, creating it if necessary.
  41. // Errors if the path already exists as a non-directory.
  42. func EnsureDir(dir string, mode os.FileMode) error {
  43. err := os.MkdirAll(dir, mode)
  44. if err != nil {
  45. return fmt.Errorf("could not create directory %q: %w", dir, err)
  46. }
  47. return nil
  48. }
  49. func FileExists(filePath string) bool {
  50. _, err := os.Stat(filePath)
  51. return !os.IsNotExist(err)
  52. }
  53. func ReadFile(filePath string) ([]byte, error) {
  54. return ioutil.ReadFile(filePath)
  55. }
  56. func MustReadFile(filePath string) []byte {
  57. fileBytes, err := ioutil.ReadFile(filePath)
  58. if err != nil {
  59. Exit(fmt.Sprintf("MustReadFile failed: %v", err))
  60. return nil
  61. }
  62. return fileBytes
  63. }
  64. func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
  65. return ioutil.WriteFile(filePath, contents, mode)
  66. }
  67. func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
  68. err := WriteFile(filePath, contents, mode)
  69. if err != nil {
  70. Exit(fmt.Sprintf("MustWriteFile failed: %v", err))
  71. }
  72. }
  73. // CopyFile copies a file. It truncates the destination file if it exists.
  74. func CopyFile(src, dst string) error {
  75. info, err := os.Stat(src)
  76. if err != nil {
  77. return err
  78. }
  79. srcfile, err := os.Open(src)
  80. if err != nil {
  81. return err
  82. }
  83. defer srcfile.Close()
  84. // create new file, truncate if exists and apply same permissions as the original one
  85. dstfile, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, info.Mode().Perm())
  86. if err != nil {
  87. return err
  88. }
  89. defer dstfile.Close()
  90. _, err = io.Copy(dstfile, srcfile)
  91. return err
  92. }