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.

140 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
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. // TrapSignal catches the SIGTERM and executes cb function. After that it exits
  33. // with code 1.
  34. func TrapSignal(cb func()) {
  35. c := make(chan os.Signal, 1)
  36. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  37. go func() {
  38. for sig := range c {
  39. fmt.Printf("captured %v, exiting...\n", sig)
  40. if cb != nil {
  41. cb()
  42. }
  43. os.Exit(1)
  44. }
  45. }()
  46. select {}
  47. }
  48. // Kill the running process by sending itself SIGTERM.
  49. func Kill() error {
  50. p, err := os.FindProcess(os.Getpid())
  51. if err != nil {
  52. return err
  53. }
  54. return p.Signal(syscall.SIGTERM)
  55. }
  56. func Exit(s string) {
  57. fmt.Printf(s + "\n")
  58. os.Exit(1)
  59. }
  60. func EnsureDir(dir string, mode os.FileMode) error {
  61. if _, err := os.Stat(dir); os.IsNotExist(err) {
  62. err := os.MkdirAll(dir, mode)
  63. if err != nil {
  64. return fmt.Errorf("Could not create directory %v. %v", dir, err)
  65. }
  66. }
  67. return nil
  68. }
  69. func IsDirEmpty(name string) (bool, error) {
  70. f, err := os.Open(name)
  71. if err != nil {
  72. if os.IsNotExist(err) {
  73. return true, err
  74. }
  75. // Otherwise perhaps a permission
  76. // error or some other error.
  77. return false, err
  78. }
  79. defer f.Close()
  80. _, err = f.Readdirnames(1) // Or f.Readdir(1)
  81. if err == io.EOF {
  82. return true, nil
  83. }
  84. return false, err // Either not empty or error, suits both cases
  85. }
  86. func FileExists(filePath string) bool {
  87. _, err := os.Stat(filePath)
  88. return !os.IsNotExist(err)
  89. }
  90. func ReadFile(filePath string) ([]byte, error) {
  91. return ioutil.ReadFile(filePath)
  92. }
  93. func MustReadFile(filePath string) []byte {
  94. fileBytes, err := ioutil.ReadFile(filePath)
  95. if err != nil {
  96. Exit(fmt.Sprintf("MustReadFile failed: %v", err))
  97. return nil
  98. }
  99. return fileBytes
  100. }
  101. func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
  102. return ioutil.WriteFile(filePath, contents, mode)
  103. }
  104. func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
  105. err := WriteFile(filePath, contents, mode)
  106. if err != nil {
  107. Exit(fmt.Sprintf("MustWriteFile failed: %v", err))
  108. }
  109. }
  110. //--------------------------------------------------------------------------------
  111. func Prompt(prompt string, defaultValue string) (string, error) {
  112. fmt.Print(prompt)
  113. reader := bufio.NewReader(os.Stdin)
  114. line, err := reader.ReadString('\n')
  115. if err != nil {
  116. return defaultValue, err
  117. }
  118. line = strings.TrimSpace(line)
  119. if line == "" {
  120. return defaultValue, nil
  121. }
  122. return line, nil
  123. }