main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "app-uploader/huawei"
  4. "app-uploader/utils"
  5. "fmt"
  6. "path/filepath"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/jessevdk/go-flags"
  12. )
  13. var GAppOption = &AppOption{}
  14. // short只能是一个字符
  15. type AppOption struct {
  16. Unzip string `short:"d" long:"unzip" description:"unzip file"`
  17. }
  18. func (o *AppOption) Parse() {
  19. _, err := flags.NewParser(o, flags.Default|flags.IgnoreUnknown).Parse()
  20. fmt.Println(err)
  21. }
  22. func main() {
  23. // 读取package.json版本数据
  24. // 复制app.json文件和logo文件
  25. // 读取配置
  26. // 解压工具 app-uploader.exe -d output/queenter-v1.1.2-amd64-windows.zip
  27. // !压缩后的文件在windows上用软件打开有问题,但在linux上解压或者unzip正常
  28. // utils.Unzip(zipPath,filepath.Base(zipFile),nil)
  29. GAppOption.Parse()
  30. if len(GAppOption.Unzip) > 0 {
  31. fileName := filepath.Base(GAppOption.Unzip)
  32. ext := filepath.Ext(fileName)
  33. fileDir := strings.TrimSuffix(fileName, ext)
  34. fmt.Println(GAppOption.Unzip)
  35. dest := fmt.Sprintf("%s/%s", filepath.Dir(GAppOption.Unzip), fileDir)
  36. fmt.Println("unzip dir:", dest)
  37. err := utils.Unzip(GAppOption.Unzip, dest, nil)
  38. if err != nil {
  39. fmt.Println("unzip err:", err)
  40. return
  41. }
  42. return
  43. }
  44. appConf, err := utils.ReadAppConfig()
  45. if err != nil {
  46. fmt.Println("读取配置文件错误")
  47. return
  48. }
  49. uploadConf, err := utils.ReadUpConf()
  50. if err != nil {
  51. fmt.Println("读取配置文件错误")
  52. return
  53. }
  54. if _, ok := uploadConf[appConf.Key]; !ok {
  55. fmt.Println("未找到该配置项")
  56. return
  57. }
  58. // 复制文件到对应目录
  59. dest := uploadConf[appConf.Key].Dest
  60. copyFiles := uploadConf[appConf.Key].Files
  61. modify := uploadConf[appConf.Key].Modify
  62. fmt.Println(uploadConf[appConf.Key])
  63. // 复制文件到目标目录中
  64. for _, file := range copyFiles {
  65. err := utils.Copy(file, fmt.Sprintf("%s/%s", dest, file))
  66. if err != nil {
  67. fmt.Println(err)
  68. return
  69. }
  70. }
  71. // 获取版本信息
  72. version, err := utils.GetVersion()
  73. if err != nil {
  74. fmt.Println(err)
  75. return
  76. }
  77. appConf.Version = version
  78. appConfDest := fmt.Sprintf("%s/%s", dest, modify)
  79. err = utils.WriteAppConfig(appConf, appConfDest)
  80. if err != nil {
  81. fmt.Println(err)
  82. return
  83. }
  84. // 压缩文件
  85. // remoteswift-v1.0.0-x64-windows.zip
  86. zipFile := fmt.Sprintf("%s-v%s-%s-%s.zip", appConf.Key, version, runtime.GOARCH, runtime.GOOS)
  87. zipPath := fmt.Sprintf("output/%s", zipFile)
  88. fmt.Println("压缩文件中...")
  89. err = utils.Zip(zipPath, dest)
  90. if err != nil {
  91. fmt.Println("压缩文件出错")
  92. return
  93. }
  94. fmt.Println("上传中...")
  95. // 上传文件
  96. huawei.InitConfig()
  97. // http://spu3dv1.obs.cn-east-3.myhuaweicloud.com/pkg/lancher1.0.2.windows-amd64.zip
  98. obsDir := "pkg"
  99. obs, err := huawei.UploadFile(zipPath, obsDir, zipFile)
  100. if err != nil {
  101. fmt.Println(err)
  102. return
  103. }
  104. if len(obs.Url) < 1 {
  105. fmt.Println("上传zipfile错误")
  106. return
  107. }
  108. fmt.Println("succ uploaded=>", obs.Url)
  109. // 获取最新版本
  110. // 输出db文件
  111. outputDb := "output/db.json"
  112. sizeM, err := strconv.Atoi(strings.Replace(appConf.Size, "MB", "", -1))
  113. if err != nil {
  114. fmt.Println("转换size string->int 错误:", err)
  115. }
  116. err = utils.WriteVersionDb(&utils.Version{
  117. Name: appConf.Key,
  118. Version: version,
  119. SizeM: sizeM,
  120. Url: fmt.Sprintf("https:%s", obs.Url),
  121. }, outputDb)
  122. if err != nil {
  123. fmt.Println(err)
  124. return
  125. }
  126. fmt.Println("上传成功")
  127. fmt.Println("输出文件目录=>: output")
  128. time.Sleep(3 * time.Second)
  129. }