main.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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\n":
  118. fmt.Println("上传中...")
  119. case "no\n":
  120. fmt.Println("取消上传")
  121. isCover = false
  122. time.Sleep(3 * time.Second)
  123. // !这里要不要直接退出,如果不需要上传则,后面的发布流程感觉不需要。所以这里直接退出
  124. return
  125. default:
  126. fmt.Println("上传中...")
  127. return
  128. }
  129. }
  130. obsUrl := checkFile
  131. if isCover {
  132. // 上传文件
  133. huawei.InitConfig()
  134. // http://spu3dv1.obs.cn-east-3.myhuaweicloud.com/pkg/lancher1.0.2.windows-amd64.zip
  135. obsDir := "pkg"
  136. obs, err := huawei.UploadFile(zipPath, obsDir, zipFile)
  137. if err != nil {
  138. fmt.Println(err)
  139. return
  140. }
  141. if len(obs.Url) < 1 {
  142. fmt.Println("上传zipfile错误")
  143. return
  144. }
  145. fmt.Println("succ uploaded=>", obs.Url)
  146. obsUrl = fmt.Sprintf("https:%s", obs.Url)
  147. }
  148. // 获取最新版本
  149. // 输出db文件
  150. outputDb := "output/db.json"
  151. sizeM, err := strconv.Atoi(strings.Replace(appConf.Size, "MB", "", -1))
  152. if err != nil {
  153. fmt.Println("转换size string->int 错误:", err)
  154. }
  155. // obsUrl := fmt.Sprintf("https:%s", obs.Url)
  156. fmt.Println("上传成功")
  157. // 是否发布
  158. inputReader := bufio.NewReader(os.Stdin)
  159. fmt.Printf("是否发布【%s】,version【%s】?\n发布:【yes】,不发布【no】: ", appConf.Key, version)
  160. input, err := inputReader.ReadString('\n')
  161. if err != nil {
  162. fmt.Println(err)
  163. return
  164. }
  165. switch input {
  166. case "yes\n":
  167. fmt.Println("发布中...")
  168. case "no\n":
  169. fmt.Println("取消发布")
  170. time.Sleep(3 * time.Second)
  171. return
  172. default:
  173. fmt.Println("发布中...")
  174. return
  175. }
  176. // 获取对应应用配置
  177. // "platform": "windows-amd64",
  178. platform := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
  179. query := fmt.Sprintf("?name=%s&platform=%s", appConf.Key, platform)
  180. appVersion, err := utils.GetAppVersion(query)
  181. if err != nil {
  182. fmt.Println(err)
  183. fmt.Println("获取版本信息错误")
  184. }
  185. fmt.Println(appVersion)
  186. // 调用接口修改配置
  187. appVersion.Version = version
  188. appVersion.SizeM = float32(sizeM)
  189. appVersion.Url = obsUrl
  190. data, err := json.Marshal(appVersion)
  191. if err != nil {
  192. fmt.Println("数据转换错误:", err)
  193. return
  194. }
  195. res, err := utils.PostJson(data)
  196. if err != nil {
  197. fmt.Println("更新version错误:", err)
  198. return
  199. }
  200. if res == "fail" {
  201. fmt.Println("更新version失败")
  202. return
  203. }
  204. if res == "succ" {
  205. fmt.Println("更新version成功")
  206. err = utils.WriteVersionDb(&utils.Version{
  207. Label: appVersion.Label,
  208. Name: appConf.Key,
  209. Icon: appVersion.Icon,
  210. Type: appVersion.Type,
  211. Version: version,
  212. SizeM: sizeM,
  213. Url: obsUrl,
  214. Platform: appVersion.Platform,
  215. Thumbnail: appVersion.Thumbnail,
  216. Desc: appVersion.Desc,
  217. AppType: appVersion.AppType,
  218. InstCount: appConf.InstCount,
  219. }, outputDb)
  220. if err != nil {
  221. fmt.Println("写入output/db.json错误:", err)
  222. return
  223. }
  224. fmt.Println("输出文件目录=>: output")
  225. time.Sleep(3 * time.Second)
  226. }
  227. }