main.go 3.3 KB

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