install.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package install
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/cnlh/nps/lib/common"
  6. "io"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. func InstallNps() {
  13. path := common.GetInstallPath()
  14. if common.FileExists(path) {
  15. log.Fatalf("the path %s has exist, does not support install", path)
  16. }
  17. MkidrDirAll(path, "conf", "web/static", "web/views")
  18. //复制文件到对应目录
  19. if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "views"), filepath.Join(path, "web", "views")); err != nil {
  20. log.Fatalln(err)
  21. }
  22. if err := CopyDir(filepath.Join(common.GetAppPath(), "web", "static"), filepath.Join(path, "web", "static")); err != nil {
  23. log.Fatalln(err)
  24. }
  25. if err := CopyDir(filepath.Join(common.GetAppPath(), "conf"), filepath.Join(path, "conf")); err != nil {
  26. log.Fatalln(err)
  27. }
  28. if !common.IsWindows() {
  29. if _, err := copyFile(filepath.Join(common.GetAppPath(), "nps"), "/usr/bin/nps"); err != nil {
  30. if _, err := copyFile(filepath.Join(common.GetAppPath(), "nps"), "/usr/local/bin/nps"); err != nil {
  31. log.Fatalln(err)
  32. } else {
  33. os.Chmod("/usr/local/bin/nps", 0755)
  34. log.Println("Executable files have been copied to", "/usr/local/bin/nps")
  35. }
  36. } else {
  37. os.Chmod("/usr/bin/nps", 0755)
  38. log.Println("Executable files have been copied to", "/usr/bin/nps")
  39. }
  40. }
  41. log.Println("install ok!")
  42. log.Println("Static files and configuration files in the current directory will be useless")
  43. log.Println("The new configuration file is located in", path, "you can edit them")
  44. if !common.IsWindows() {
  45. log.Println("You can start with nps test|start|stop|restart|status anywhere")
  46. } else {
  47. log.Println("You can copy executable files to any directory and start working with nps.exe test|start|stop|restart|status")
  48. }
  49. }
  50. func MkidrDirAll(path string, v ...string) {
  51. for _, item := range v {
  52. if err := os.MkdirAll(filepath.Join(path, item), 0755); err != nil {
  53. log.Fatalf("Failed to create directory %s error:%s", path, err.Error())
  54. }
  55. }
  56. }
  57. func CopyDir(srcPath string, destPath string) error {
  58. //检测目录正确性
  59. if srcInfo, err := os.Stat(srcPath); err != nil {
  60. fmt.Println(err.Error())
  61. return err
  62. } else {
  63. if !srcInfo.IsDir() {
  64. e := errors.New("SrcPath is not the right directory!")
  65. return e
  66. }
  67. }
  68. if destInfo, err := os.Stat(destPath); err != nil {
  69. return err
  70. } else {
  71. if !destInfo.IsDir() {
  72. e := errors.New("DestInfo is not the right directory!")
  73. return e
  74. }
  75. }
  76. err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
  77. if f == nil {
  78. return err
  79. }
  80. if !f.IsDir() {
  81. destNewPath := strings.Replace(path, srcPath, destPath, -1)
  82. log.Println("copy file ::" + path + " to " + destNewPath)
  83. copyFile(path, destNewPath)
  84. }
  85. return nil
  86. })
  87. return err
  88. }
  89. //生成目录并拷贝文件
  90. func copyFile(src, dest string) (w int64, err error) {
  91. srcFile, err := os.Open(src)
  92. if err != nil {
  93. return
  94. }
  95. defer srcFile.Close()
  96. //分割path目录
  97. destSplitPathDirs := strings.Split(dest, string(filepath.Separator))
  98. //检测时候存在目录
  99. destSplitPath := ""
  100. for index, dir := range destSplitPathDirs {
  101. if index < len(destSplitPathDirs)-1 {
  102. destSplitPath = destSplitPath + dir + string(filepath.Separator)
  103. b, _ := pathExists(destSplitPath)
  104. if b == false {
  105. log.Println("mkdir:" + destSplitPath)
  106. //创建目录
  107. err := os.Mkdir(destSplitPath, os.ModePerm)
  108. if err != nil {
  109. log.Fatalln(err)
  110. }
  111. }
  112. }
  113. }
  114. dstFile, err := os.Create(dest)
  115. if err != nil {
  116. return
  117. }
  118. defer dstFile.Close()
  119. return io.Copy(dstFile, srcFile)
  120. }
  121. //检测文件夹路径时候存在
  122. func pathExists(path string) (bool, error) {
  123. _, err := os.Stat(path)
  124. if err == nil {
  125. return true, nil
  126. }
  127. if os.IsNotExist(err) {
  128. return false, nil
  129. }
  130. return false, err
  131. }