From ee80bee87a973f40de4b701ff57c3b66494e6ea4 Mon Sep 17 00:00:00 2001 From: atpking Date: Mon, 19 Dec 2022 09:52:51 +0800 Subject: [PATCH] add qrcode export function --- .gitignore | 1 + README.md | 6 +++++- api/rio_api.go | 8 ++++--- go.mod | 1 + go.sum | 2 ++ main.go | 58 +++++++++++++++++++++++++++++++++++++++++++++----- 6 files changed, 67 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 0199918..fd6267a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ icon.png +qr.png go_fir_cli go_fir_cli.exe *.apk diff --git a/README.md b/README.md index 7da1ff7..50087b0 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,16 @@ ## 说明 -这是一个 fir-cli [https://github.com/PGYER/fir-cli](https://github.com/PGYER/fir-cli) 的 go 版本, 用于上传文件到 betaqr.com (原fir.im) +这是一个 fir-cli [https://github.com/PGYER/fir-cli](https://github.com/PGYER/fir-cli) 的 go 版本, 用于上传文件到 [betaqr.com](https://www.betaqr.com) (原fir.im) ## 安装 下载自己对应的系统的文件, 赋值可执行. 若想在全局使用,放到 path 里即可. +- macOS 使用 darwin-amd64 +- Linux 使用 amd64 +- Windows 使用 + diff --git a/api/rio_api.go b/api/rio_api.go index e163d4f..e8c2985 100644 --- a/api/rio_api.go +++ b/api/rio_api.go @@ -22,6 +22,8 @@ type FirApi struct { ApiToken string AppChangelog string Email string + QrCodePngNeed bool + QrCodeAsciiNeed bool ApiAppInfo *ApiAppInfo uploadAppService *analysis.UploadAppService appFileInfo *analysis.AppFileInfo @@ -236,7 +238,7 @@ func (f *FirApi) Upload(file string) error { fmt.Println(resp) // 进行回调 - resp, e = manualCallback(file, f.appFileInfo, uploadingInfo) + resp, e = f.manualCallback(file, f.appFileInfo, uploadingInfo) if e != nil { fmt.Println("上传成功, 但是回调失败", e.Error()) os.Exit(1) @@ -335,7 +337,7 @@ func (f *FirApi) uploadAppFile(uploadingInfo AppPrepareUploadData, file string) return resp, e } -func manualCallback(file string, appInfo *analysis.AppFileInfo, uploadingInfo AppPrepareUploadData) (*resty.Response, error) { +func (f *FirApi) manualCallback(file string, appInfo *analysis.AppFileInfo, uploadingInfo AppPrepareUploadData) (*resty.Response, error) { // manual callback client := resty.New() @@ -355,7 +357,7 @@ func manualCallback(file string, appInfo *analysis.AppFileInfo, uploadingInfo Ap ReleaseType: appInfo.ReleaseType, Token: uploadingInfo.Cert.Binary.Token, Version: appInfo.Version, - Changelog: appInfo.Changelog, + Changelog: f.AppChangelog, UserId: uploadingInfo.AppUserId, } diff --git a/go.mod b/go.mod index 2d88cfa..06ad483 100644 --- a/go.mod +++ b/go.mod @@ -13,5 +13,6 @@ require ( require ( github.com/klauspost/compress v1.15.12 // indirect + github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect ) diff --git a/go.sum b/go.sum index 967957d..e7e4598 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrD github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/shogo82148/androidbinary v1.0.3 h1:0E9bBCcDVwb2xJP3NwDkdfkxtOrys3DK/NjxnUq+bNI= github.com/shogo82148/androidbinary v1.0.3/go.mod h1:TZXT25KBG2LxEC+kh7v2qvmwXfMwC/FNxKt7uTf8tJc= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM= golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= diff --git a/main.go b/main.go index 5991971..37ebbd3 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,12 @@ package main import ( "fmt" + "io/ioutil" "os" "betaqr.com/go_fir_cli/api" "betaqr.com/go_fir_cli/constants" + "github.com/skip2/go-qrcode" "gopkg.in/urfave/cli.v1" ) @@ -33,7 +35,7 @@ func initCli() { cli.Command{ Name: "version", ShortName: "v", - Usage: "查看版本", + Usage: "查看 go_fir_cli 版本", Action: func(c *cli.Context) error { fmt.Println(constants.VERSION) return nil @@ -77,7 +79,19 @@ func uploadFile() cli.Command { }, cli.StringFlag{ Name: "changelog, c", - Usage: "app 的更新日志", + Usage: "app 的更新日志, 可以是文件路径, 也可以是字符串", + }, + cli.BoolFlag{ + Name: "specific_release, s", + Usage: "生成的下载地址是否精确指定到 release, 默认为 false", + }, + cli.BoolFlag{ + Name: "qrcode, q", + Usage: "输出二维码文件 qrcode.png, 用于下载, 默认为 false", + }, + cli.BoolFlag{ + Name: "qrcodeascii, Q", + Usage: "输出二维码到终端, 默认为 false", }, }, Action: func(c *cli.Context) error { @@ -97,16 +111,50 @@ func uploadFile() cli.Command { changelog := c.String("changelog") + // 检测 changelog 文件path是否存在 + if changelog != "" { + _, err := os.Stat(changelog) + if err != nil { + // 文件不存在, 说明changlog 就是 changlog 字符串 + } else { + // + str, e := ioutil.ReadFile(changelog) + if e != nil { + + } else { + changelog = string(str) + } + } + } + api := api.FirApi{ - ApiToken: token, - AppChangelog: changelog, + ApiToken: token, + AppChangelog: changelog, + QrCodePngNeed: c.Bool("qrcode"), + QrCodeAsciiNeed: c.Bool("qrcodeascii"), } api.Upload(file) fmt.Println("上传成功") - fmt.Printf("下载页面: http://%s/%s\nReleaseID: %s\n", api.ApiAppInfo.DownloadDomain, api.ApiAppInfo.Short, api.ApiAppInfo.MasterReleaseId) + url := buildDownloadUrl(api.ApiAppInfo, c.Bool("specific_release")) + fmt.Printf("下载页面: %s\nReleaseID: %s\n", url, api.ApiAppInfo.MasterReleaseId) + + if api.QrCodePngNeed { + fmt.Println("二维码文件: qrcode.png") + qrcode.WriteFile(url, qrcode.Medium, 256, "qr.png") + } + + if api.QrCodeAsciiNeed { + } return nil }, } } + +func buildDownloadUrl(apiAppInfo *api.ApiAppInfo, includeRelease bool) string { + if includeRelease { + return fmt.Sprintf("http://%s/%s?release_id=%s", apiAppInfo.DownloadDomain, apiAppInfo.Short, apiAppInfo.MasterReleaseId) + } + return fmt.Sprintf("http://%s/%s", apiAppInfo.DownloadDomain, apiAppInfo.Short) +}