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.

159 lines
3.6 KiB

9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
  1. package common
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "os/signal"
  9. "strings"
  10. )
  11. var (
  12. GoPath = os.Getenv("GOPATH")
  13. )
  14. func TrapSignal(cb func()) {
  15. c := make(chan os.Signal, 1)
  16. signal.Notify(c, os.Interrupt)
  17. signal.Notify(c, os.Kill)
  18. go func() {
  19. for sig := range c {
  20. fmt.Printf("captured %v, exiting...\n", sig)
  21. if cb != nil {
  22. cb()
  23. }
  24. os.Exit(1)
  25. }
  26. }()
  27. select {}
  28. }
  29. func Exit(s string) {
  30. fmt.Printf(s + "\n")
  31. os.Exit(1)
  32. }
  33. func EnsureDir(dir string, mode os.FileMode) error {
  34. if _, err := os.Stat(dir); os.IsNotExist(err) {
  35. err := os.MkdirAll(dir, mode)
  36. if err != nil {
  37. return fmt.Errorf("Could not create directory %v. %v", dir, err)
  38. }
  39. }
  40. return nil
  41. }
  42. func IsDirEmpty(name string) (bool, error) {
  43. f, err := os.Open(name)
  44. if err != nil {
  45. return true, err //folder is non-existent
  46. }
  47. defer f.Close()
  48. _, err = f.Readdirnames(1) // Or f.Readdir(1)
  49. if err == io.EOF {
  50. return true, nil
  51. }
  52. return false, err // Either not empty or error, suits both cases
  53. }
  54. func FileExists(filePath string) bool {
  55. _, err := os.Stat(filePath)
  56. return !os.IsNotExist(err)
  57. }
  58. func ReadFile(filePath string) ([]byte, error) {
  59. return ioutil.ReadFile(filePath)
  60. }
  61. func MustReadFile(filePath string) []byte {
  62. fileBytes, err := ioutil.ReadFile(filePath)
  63. if err != nil {
  64. Exit(Fmt("MustReadFile failed: %v", err))
  65. return nil
  66. }
  67. return fileBytes
  68. }
  69. func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
  70. err := ioutil.WriteFile(filePath, contents, mode)
  71. if err != nil {
  72. return err
  73. }
  74. // fmt.Printf("File written to %v.\n", filePath)
  75. return nil
  76. }
  77. func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
  78. err := WriteFile(filePath, contents, mode)
  79. if err != nil {
  80. Exit(Fmt("MustWriteFile failed: %v", err))
  81. }
  82. }
  83. // Writes to newBytes to filePath.
  84. // Guaranteed not to lose *both* oldBytes and newBytes,
  85. // (assuming that the OS is perfect)
  86. func WriteFileAtomic(filePath string, newBytes []byte, mode os.FileMode) error {
  87. // If a file already exists there, copy to filePath+".bak" (overwrite anything)
  88. if _, err := os.Stat(filePath); !os.IsNotExist(err) {
  89. fileBytes, err := ioutil.ReadFile(filePath)
  90. if err != nil {
  91. return fmt.Errorf("Could not read file %v. %v", filePath, err)
  92. }
  93. err = ioutil.WriteFile(filePath+".bak", fileBytes, mode)
  94. if err != nil {
  95. return fmt.Errorf("Could not write file %v. %v", filePath+".bak", err)
  96. }
  97. }
  98. // Write newBytes to filePath.new
  99. err := ioutil.WriteFile(filePath+".new", newBytes, mode)
  100. if err != nil {
  101. return fmt.Errorf("Could not write file %v. %v", filePath+".new", err)
  102. }
  103. // Move filePath.new to filePath
  104. err = os.Rename(filePath+".new", filePath)
  105. return err
  106. }
  107. //--------------------------------------------------------------------------------
  108. func Tempfile(prefix string) (*os.File, string) {
  109. file, err := ioutil.TempFile("", prefix)
  110. if err != nil {
  111. PanicCrisis(err)
  112. }
  113. return file, file.Name()
  114. }
  115. func Tempdir(prefix string) (*os.File, string) {
  116. tempDir := os.TempDir() + "/" + prefix + RandStr(12)
  117. err := EnsureDir(tempDir, 0700)
  118. if err != nil {
  119. panic(Fmt("Error creating temp dir: %v", err))
  120. }
  121. dir, err := os.Open(tempDir)
  122. if err != nil {
  123. panic(Fmt("Error opening temp dir: %v", err))
  124. }
  125. return dir, tempDir
  126. }
  127. //--------------------------------------------------------------------------------
  128. func Prompt(prompt string, defaultValue string) (string, error) {
  129. fmt.Print(prompt)
  130. reader := bufio.NewReader(os.Stdin)
  131. line, err := reader.ReadString('\n')
  132. if err != nil {
  133. return defaultValue, err
  134. } else {
  135. line = strings.TrimSpace(line)
  136. if line == "" {
  137. return defaultValue, nil
  138. }
  139. return line, nil
  140. }
  141. }