install.go 3.6 KB

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