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.

111 lines
2.4 KiB

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