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.

143 lines
2.9 KiB

9 years ago
8 years ago
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
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package common
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "os/signal"
  10. "strings"
  11. "syscall"
  12. )
  13. var gopath string
  14. // GoPath returns GOPATH env variable value. If it is not set, this function
  15. // will try to call `go env GOPATH` subcommand.
  16. func GoPath() string {
  17. if gopath != "" {
  18. return gopath
  19. }
  20. path := os.Getenv("GOPATH")
  21. if len(path) == 0 {
  22. goCmd := exec.Command("go", "env", "GOPATH")
  23. out, err := goCmd.Output()
  24. if err != nil {
  25. panic(fmt.Sprintf("failed to determine gopath: %v", err))
  26. }
  27. path = string(out)
  28. }
  29. gopath = path
  30. return path
  31. }
  32. type logger interface {
  33. Info(msg string, keyvals ...interface{})
  34. }
  35. // TrapSignal catches the SIGTERM/SIGINT and executes cb function. After that it exits
  36. // with code 0.
  37. func TrapSignal(logger logger, cb func()) {
  38. c := make(chan os.Signal, 1)
  39. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  40. go func() {
  41. for sig := range c {
  42. logger.Info(fmt.Sprintf("captured %v, exiting...", sig))
  43. if cb != nil {
  44. cb()
  45. }
  46. os.Exit(0)
  47. }
  48. }()
  49. }
  50. // Kill the running process by sending itself SIGTERM.
  51. func Kill() error {
  52. p, err := os.FindProcess(os.Getpid())
  53. if err != nil {
  54. return err
  55. }
  56. return p.Signal(syscall.SIGTERM)
  57. }
  58. func Exit(s string) {
  59. fmt.Printf(s + "\n")
  60. os.Exit(1)
  61. }
  62. func EnsureDir(dir string, mode os.FileMode) error {
  63. if _, err := os.Stat(dir); os.IsNotExist(err) {
  64. err := os.MkdirAll(dir, mode)
  65. if err != nil {
  66. return fmt.Errorf("Could not create directory %v. %v", dir, err)
  67. }
  68. }
  69. return nil
  70. }
  71. func IsDirEmpty(name string) (bool, error) {
  72. f, err := os.Open(name)
  73. if err != nil {
  74. if os.IsNotExist(err) {
  75. return true, err
  76. }
  77. // Otherwise perhaps a permission
  78. // error or some other error.
  79. return false, err
  80. }
  81. defer f.Close()
  82. _, err = f.Readdirnames(1) // Or f.Readdir(1)
  83. if err == io.EOF {
  84. return true, nil
  85. }
  86. return false, err // Either not empty or error, suits both cases
  87. }
  88. func FileExists(filePath string) bool {
  89. _, err := os.Stat(filePath)
  90. return !os.IsNotExist(err)
  91. }
  92. func ReadFile(filePath string) ([]byte, error) {
  93. return ioutil.ReadFile(filePath)
  94. }
  95. func MustReadFile(filePath string) []byte {
  96. fileBytes, err := ioutil.ReadFile(filePath)
  97. if err != nil {
  98. Exit(fmt.Sprintf("MustReadFile failed: %v", err))
  99. return nil
  100. }
  101. return fileBytes
  102. }
  103. func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
  104. return ioutil.WriteFile(filePath, contents, mode)
  105. }
  106. func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
  107. err := WriteFile(filePath, contents, mode)
  108. if err != nil {
  109. Exit(fmt.Sprintf("MustWriteFile failed: %v", err))
  110. }
  111. }
  112. //--------------------------------------------------------------------------------
  113. func Prompt(prompt string, defaultValue string) (string, error) {
  114. fmt.Print(prompt)
  115. reader := bufio.NewReader(os.Stdin)
  116. line, err := reader.ReadString('\n')
  117. if err != nil {
  118. return defaultValue, err
  119. }
  120. line = strings.TrimSpace(line)
  121. if line == "" {
  122. return defaultValue, nil
  123. }
  124. return line, nil
  125. }