main.go 6.0 KB

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