main.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package main
  2. import (
  3. "app-uploader/huawei"
  4. "app-uploader/utils"
  5. "bufio"
  6. "encoding/json"
  7. "fmt"
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/jessevdk/go-flags"
  15. )
  16. var GAppOption = &AppOption{}
  17. // short只能是一个字符
  18. type AppOption struct {
  19. Unzip string `short:"d" long:"unzip" description:"unzip file"`
  20. }
  21. func (o *AppOption) Parse() {
  22. _, err := flags.NewParser(o, flags.Default|flags.IgnoreUnknown).Parse()
  23. fmt.Println(err)
  24. }
  25. func main() {
  26. // 读取package.json版本数据
  27. // 复制app.json文件和logo文件
  28. // 读取配置
  29. // 解压工具 app-uploader.exe -d output/queenter-v1.1.2-amd64-windows.zip
  30. // !压缩后的文件在windows上用软件打开有问题,但在linux上解压或者unzip正常
  31. // utils.Unzip(zipPath,filepath.Base(zipFile),nil)
  32. GAppOption.Parse()
  33. if len(GAppOption.Unzip) > 0 {
  34. fileName := filepath.Base(GAppOption.Unzip)
  35. ext := filepath.Ext(fileName)
  36. fileDir := strings.TrimSuffix(fileName, ext)
  37. fmt.Println(GAppOption.Unzip)
  38. dest := fmt.Sprintf("%s/%s", filepath.Dir(GAppOption.Unzip), fileDir)
  39. fmt.Println("unzip dir:", dest)
  40. err := utils.Unzip(GAppOption.Unzip, dest, nil)
  41. if err != nil {
  42. fmt.Println("unzip err:", err)
  43. return
  44. }
  45. return
  46. }
  47. appConf, err := utils.ReadAppConfig()
  48. if err != nil {
  49. fmt.Println("读取配置文件错误")
  50. return
  51. }
  52. uploadConf, err := utils.ReadUpConf()
  53. if err != nil {
  54. fmt.Println("读取配置文件错误")
  55. return
  56. }
  57. if _, ok := uploadConf[appConf.Key]; !ok {
  58. fmt.Println("未找到该配置项")
  59. return
  60. }
  61. // 复制文件到对应目录
  62. dest := uploadConf[appConf.Key].Dest
  63. copyFiles := uploadConf[appConf.Key].Files
  64. modify := uploadConf[appConf.Key].Modify
  65. fmt.Println(uploadConf[appConf.Key])
  66. // 复制文件到目标目录中
  67. for _, file := range copyFiles {
  68. err := utils.Copy(file, fmt.Sprintf("%s/%s", dest, file))
  69. if err != nil {
  70. fmt.Println(err)
  71. return
  72. }
  73. }
  74. // 获取版本信息
  75. version, err := utils.GetVersion()
  76. if err != nil {
  77. fmt.Println(err)
  78. return
  79. }
  80. appConf.Version = version
  81. appConfDest := fmt.Sprintf("%s/%s", dest, modify)
  82. err = utils.WriteAppConfig(appConf, appConfDest)
  83. if err != nil {
  84. fmt.Println(err)
  85. return
  86. }
  87. // 压缩文件
  88. // remoteswift-v1.0.0-x64-windows.zip
  89. zipFile := fmt.Sprintf("%s-v%s-%s-%s.zip", appConf.Key, version, runtime.GOARCH, runtime.GOOS)
  90. zipPath := fmt.Sprintf("output/%s", zipFile)
  91. fmt.Println("压缩文件中...")
  92. err = utils.Zip(zipPath, dest)
  93. if err != nil {
  94. fmt.Println("压缩文件出错")
  95. return
  96. }
  97. // 检测zip文件是否存在
  98. ok, err := utils.CheckZipExist("http://spu3dv1.obs.cn-east-3.myhuaweicloud.com/pkg/" + zipFile)
  99. if err != nil {
  100. fmt.Println(err)
  101. fmt.Println("检测打包文件失败")
  102. }
  103. if ok {
  104. inputReader := bufio.NewReader(os.Stdin)
  105. fmt.Printf("obs已存在该zip文件,是否覆盖【%s】?\n发布:【yes】,不发布【no】:", zipFile)
  106. input, err := inputReader.ReadString('\n')
  107. if err != nil {
  108. fmt.Println(err)
  109. return
  110. }
  111. switch input {
  112. case "yes\n":
  113. fmt.Println("上传中...")
  114. case "no\n":
  115. fmt.Println("取消上传")
  116. time.Sleep(3 * time.Second)
  117. return
  118. default:
  119. fmt.Println("上传中...")
  120. return
  121. }
  122. }
  123. // 上传文件
  124. huawei.InitConfig()
  125. // http://spu3dv1.obs.cn-east-3.myhuaweicloud.com/pkg/lancher1.0.2.windows-amd64.zip
  126. obsDir := "pkg"
  127. obs, err := huawei.UploadFile(zipPath, obsDir, zipFile)
  128. if err != nil {
  129. fmt.Println(err)
  130. return
  131. }
  132. if len(obs.Url) < 1 {
  133. fmt.Println("上传zipfile错误")
  134. return
  135. }
  136. fmt.Println("succ uploaded=>", obs.Url)
  137. // 获取最新版本
  138. // 输出db文件
  139. outputDb := "output/db.json"
  140. sizeM, err := strconv.Atoi(strings.Replace(appConf.Size, "MB", "", -1))
  141. if err != nil {
  142. fmt.Println("转换size string->int 错误:", err)
  143. }
  144. obsUrl := fmt.Sprintf("https:%s", obs.Url)
  145. fmt.Println("上传成功")
  146. // 是否发布
  147. inputReader := bufio.NewReader(os.Stdin)
  148. fmt.Printf("是否发布【%s】,version【%s】?\n发布:【yes】,不发布【no】:", appConf.Key, version)
  149. input, err := inputReader.ReadString('\n')
  150. if err != nil {
  151. fmt.Println(err)
  152. return
  153. }
  154. switch input {
  155. case "yes\n":
  156. fmt.Println("发布中...")
  157. case "no\n":
  158. fmt.Println("取消发布")
  159. time.Sleep(3 * time.Second)
  160. return
  161. default:
  162. fmt.Println("发布中...")
  163. return
  164. }
  165. // 获取对应应用配置
  166. // "platform": "windows-amd64",
  167. // platform := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
  168. platform := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
  169. query := fmt.Sprintf("?name=%s&platform=%s", appConf.Key, platform)
  170. appVersion, err := utils.GetAppVersion(query)
  171. if err != nil {
  172. fmt.Println(err)
  173. fmt.Println("获取版本信息错误")
  174. }
  175. fmt.Println(appVersion)
  176. // 调用接口修改配置
  177. appVersion.Version = version
  178. appVersion.SizeM = float32(sizeM)
  179. appVersion.Url = obsUrl
  180. data, err := json.Marshal(appVersion)
  181. if err != nil {
  182. fmt.Println("数据转换错误:", err)
  183. return
  184. }
  185. res, err := utils.PostJson(data)
  186. if err != nil {
  187. fmt.Println("更新version错误:", err)
  188. return
  189. }
  190. if res == "fail" {
  191. fmt.Println("更新version失败")
  192. return
  193. }
  194. if res == "succ" {
  195. fmt.Println("更新version成功")
  196. err = utils.WriteVersionDb(&utils.Version{
  197. Label: appVersion.Label,
  198. Name: appConf.Key,
  199. Icon: appVersion.Icon,
  200. Type: appVersion.Type,
  201. Version: version,
  202. SizeM: sizeM,
  203. Url: obsUrl,
  204. Platform: appVersion.Platform,
  205. Thumbnail: appVersion.Thumbnail,
  206. }, outputDb)
  207. if err != nil {
  208. fmt.Println("写入output/db.json错误:", err)
  209. return
  210. }
  211. fmt.Println("输出文件目录=>: output")
  212. time.Sleep(3 * time.Second)
  213. }
  214. }