install.go 3.5 KB

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