1
0

install.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package lib
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "time"
  12. )
  13. func InstallNps() {
  14. var path string
  15. switch runtime.GOOS {
  16. case "windows":
  17. path = "C:/"
  18. case "linux", "darwin":
  19. path = "/etc/nps/"
  20. }
  21. if err := os.Mkdir(path, 0755); err != nil {
  22. log.Fatalf("创建目录%s失败:%s", path, err.Error())
  23. }
  24. //复制文件到对应目录
  25. if err := CopyDir("./web", path); err != nil {
  26. log.Fatalln(err)
  27. }
  28. if err := CopyDir("./conf", path); err != nil {
  29. log.Fatalln(err)
  30. }
  31. //linux加入到/etc/init.d
  32. //windows处理
  33. //darwin处理
  34. }
  35. func CopyDir(srcPath string, destPath string) error {
  36. //检测目录正确性
  37. if srcInfo, err := os.Stat(srcPath); err != nil {
  38. fmt.Println(err.Error())
  39. return err
  40. } else {
  41. if !srcInfo.IsDir() {
  42. e := errors.New("srcPath不是一个正确的目录!")
  43. fmt.Println(e.Error())
  44. return e
  45. }
  46. }
  47. if destInfo, err := os.Stat(destPath); err != nil {
  48. fmt.Println(err.Error())
  49. return err
  50. } else {
  51. if !destInfo.IsDir() {
  52. e := errors.New("destInfo不是一个正确的目录!")
  53. fmt.Println(e.Error())
  54. return e
  55. }
  56. }
  57. //加上拷贝时间:不用可以去掉
  58. destPath = destPath + "_" + time.Now().Format("20060102150405")
  59. err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
  60. if f == nil {
  61. return err
  62. }
  63. if !f.IsDir() {
  64. path := strings.Replace(path, "\\", "/", -1)
  65. destNewPath := strings.Replace(path, srcPath, destPath, -1)
  66. fmt.Println("复制文件:" + path + " 到 " + destNewPath)
  67. copyFile(path, destNewPath)
  68. }
  69. return nil
  70. })
  71. if err != nil {
  72. fmt.Printf(err.Error())
  73. }
  74. return err
  75. }
  76. //生成目录并拷贝文件
  77. func copyFile(src, dest string) (w int64, err error) {
  78. srcFile, err := os.Open(src)
  79. if err != nil {
  80. fmt.Println(err.Error())
  81. return
  82. }
  83. defer srcFile.Close()
  84. //分割path目录
  85. destSplitPathDirs := strings.Split(dest, "/")
  86. //检测时候存在目录
  87. destSplitPath := ""
  88. for index, dir := range destSplitPathDirs {
  89. if index < len(destSplitPathDirs)-1 {
  90. destSplitPath = destSplitPath + dir + "/"
  91. b, _ := pathExists(destSplitPath)
  92. if b == false {
  93. fmt.Println("创建目录:" + destSplitPath)
  94. //创建目录
  95. err := os.Mkdir(destSplitPath, os.ModePerm)
  96. if err != nil {
  97. fmt.Println(err)
  98. }
  99. }
  100. }
  101. }
  102. dstFile, err := os.Create(dest)
  103. if err != nil {
  104. fmt.Println(err.Error())
  105. return
  106. }
  107. defer dstFile.Close()
  108. return io.Copy(dstFile, srcFile)
  109. }
  110. //检测文件夹路径时候存在
  111. func pathExists(path string) (bool, error) {
  112. _, err := os.Stat(path)
  113. if err == nil {
  114. return true, nil
  115. }
  116. if os.IsNotExist(err) {
  117. return false, nil
  118. }
  119. return false, err
  120. }