123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package main
- import (
- "app-uploader/huawei"
- "app-uploader/utils"
- "bufio"
- "encoding/json"
- "fmt"
- "os"
- "path/filepath"
- "runtime"
- "strconv"
- "strings"
- "time"
- "github.com/jessevdk/go-flags"
- )
- var GAppOption = &AppOption{}
- // short只能是一个字符
- type AppOption struct {
- Unzip string `short:"d" long:"unzip" description:"unzip file"`
- }
- func (o *AppOption) Parse() {
- _, err := flags.NewParser(o, flags.Default|flags.IgnoreUnknown).Parse()
- fmt.Println(err)
- }
- func main() {
- // 读取package.json版本数据
- // 复制app.json文件和logo文件
- // 读取配置
- // 解压工具 app-uploader.exe -d output/queenter-v1.1.2-amd64-windows.zip
- // !压缩后的文件在windows上用软件打开有问题,但在linux上解压或者unzip正常
- // utils.Unzip(zipPath,filepath.Base(zipFile),nil)
- GAppOption.Parse()
- if len(GAppOption.Unzip) > 0 {
- fileName := filepath.Base(GAppOption.Unzip)
- ext := filepath.Ext(fileName)
- fileDir := strings.TrimSuffix(fileName, ext)
- fmt.Println(GAppOption.Unzip)
- dest := fmt.Sprintf("%s/%s", filepath.Dir(GAppOption.Unzip), fileDir)
- fmt.Println("unzip dir:", dest)
- err := utils.Unzip(GAppOption.Unzip, dest, nil)
- if err != nil {
- fmt.Println("unzip err:", err)
- return
- }
- return
- }
- appConf, err := utils.ReadAppConfig()
- if err != nil {
- fmt.Println("读取配置文件错误")
- return
- }
- uploadConf, err := utils.ReadUpConf()
- if err != nil {
- fmt.Println("读取配置文件错误")
- return
- }
- if _, ok := uploadConf[appConf.Key]; !ok {
- fmt.Println("未找到该配置项")
- return
- }
- // 复制文件到对应目录
- dest := uploadConf[appConf.Key].Dest
- copyFiles := uploadConf[appConf.Key].Files
- modify := uploadConf[appConf.Key].Modify
- fmt.Println(uploadConf[appConf.Key])
- // 复制文件到目标目录中
- for _, file := range copyFiles {
- err := utils.Copy(file, fmt.Sprintf("%s/%s", dest, file))
- if err != nil {
- fmt.Println(err)
- return
- }
- }
- // 获取版本信息
- version, err := utils.GetVersion()
- if err != nil {
- fmt.Println(err)
- return
- }
- appConf.Version = version
- appConfDest := fmt.Sprintf("%s/%s", dest, modify)
- err = utils.WriteAppConfig(appConf, appConfDest)
- if err != nil {
- fmt.Println(err)
- return
- }
- // 压缩文件
- // remoteswift-v1.0.0-x64-windows.zip
- zipFile := fmt.Sprintf("%s-v%s-%s-%s.zip", appConf.Key, version, runtime.GOARCH, runtime.GOOS)
- zipPath := fmt.Sprintf("output/%s", zipFile)
- fmt.Println("压缩文件中...")
- err = utils.Zip(zipPath, dest)
- if err != nil {
- fmt.Println("压缩文件出错")
- return
- }
- // 检测zip文件是否存在
- ok, err := utils.CheckZipExist("http://spu3dv1.obs.cn-east-3.myhuaweicloud.com/pkg/" + zipFile)
- if err != nil {
- fmt.Println(err)
- fmt.Println("检测打包文件失败")
- }
- if ok {
- inputReader := bufio.NewReader(os.Stdin)
- fmt.Printf("obs已存在该zip文件,是否覆盖【%s】?\n发布:【yes】,不发布【no】:", zipFile)
- input, err := inputReader.ReadString('\n')
- if err != nil {
- fmt.Println(err)
- return
- }
- switch input {
- case "yes\n":
- fmt.Println("上传中...")
- case "no\n":
- fmt.Println("取消上传")
- time.Sleep(3 * time.Second)
- return
- default:
- fmt.Println("上传中...")
- return
- }
- }
- // 上传文件
- huawei.InitConfig()
- // http://spu3dv1.obs.cn-east-3.myhuaweicloud.com/pkg/lancher1.0.2.windows-amd64.zip
- obsDir := "pkg"
- obs, err := huawei.UploadFile(zipPath, obsDir, zipFile)
- if err != nil {
- fmt.Println(err)
- return
- }
- if len(obs.Url) < 1 {
- fmt.Println("上传zipfile错误")
- return
- }
- fmt.Println("succ uploaded=>", obs.Url)
- // 获取最新版本
- // 输出db文件
- outputDb := "output/db.json"
- sizeM, err := strconv.Atoi(strings.Replace(appConf.Size, "MB", "", -1))
- if err != nil {
- fmt.Println("转换size string->int 错误:", err)
- }
- obsUrl := fmt.Sprintf("https:%s", obs.Url)
- fmt.Println("上传成功")
- // 是否发布
- inputReader := bufio.NewReader(os.Stdin)
- fmt.Printf("是否发布【%s】,version【%s】?\n发布:【yes】,不发布【no】:", appConf.Key, version)
- input, err := inputReader.ReadString('\n')
- if err != nil {
- fmt.Println(err)
- return
- }
- switch input {
- case "yes\n":
- fmt.Println("发布中...")
- case "no\n":
- fmt.Println("取消发布")
- time.Sleep(3 * time.Second)
- return
- default:
- fmt.Println("发布中...")
- return
- }
- // 获取对应应用配置
- // "platform": "windows-amd64",
- // platform := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
- platform := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
- query := fmt.Sprintf("?name=%s&platform=%s", appConf.Key, platform)
- appVersion, err := utils.GetAppVersion(query)
- if err != nil {
- fmt.Println(err)
- fmt.Println("获取版本信息错误")
- }
- fmt.Println(appVersion)
- // 调用接口修改配置
- appVersion.Version = version
- appVersion.SizeM = float32(sizeM)
- appVersion.Url = obsUrl
- data, err := json.Marshal(appVersion)
- if err != nil {
- fmt.Println("数据转换错误:", err)
- return
- }
- res, err := utils.PostJson(data)
- if err != nil {
- fmt.Println("更新version错误:", err)
- return
- }
- if res == "fail" {
- fmt.Println("更新version失败")
- return
- }
- if res == "succ" {
- fmt.Println("更新version成功")
- err = utils.WriteVersionDb(&utils.Version{
- Label: appVersion.Label,
- Name: appConf.Key,
- Icon: appVersion.Icon,
- Type: appVersion.Type,
- Version: version,
- SizeM: sizeM,
- Url: obsUrl,
- Platform: appVersion.Platform,
- Thumbnail: appVersion.Thumbnail,
- }, outputDb)
- if err != nil {
- fmt.Println("写入output/db.json错误:", err)
- return
- }
- fmt.Println("输出文件目录=>: output")
- time.Sleep(3 * time.Second)
- }
- }
|