Skip to content

Commit

Permalink
code: some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlancehu committed Aug 25, 2023
1 parent bd954d8 commit b7d7876
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ import (
)

var (
version = "Dev" // 程序版本号
goos = "os" // 程序运行的操作系统
goarch = "arch" // 程序运行的操作系统架构
buildTime = "time" // 程序编译时间
action string // 程序运行的行为
notifa bool // 是否启用 windows 通知
notifyHelpMsg = "" // 帮助信息中的通知信息
ua = "qcip/" + version // 请求的 User-Agent
confPath = "config.json" // 默认配置文件路径
httpClient = &http.Client{
version string = "Dev" // 程序版本号
goos string = "os" // 程序运行的操作系统
goarch string = "arch" // 程序运行的操作系统架构
buildTime string = "time" // 程序编译时间
action string // 程序运行的行为
notifa bool // 是否启用 windows 通知
notifyHelpMsg string = "" // 帮助信息中的通知信息
ua string = "qcip/" + version // 请求的 User-Agent
confPath string = "config.json" // 默认配置文件路径
httpClient *http.Client = &http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
Expand Down
19 changes: 14 additions & 5 deletions winnotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ package main

import (
"os"
"runtime"

"gopkg.in/toast.v1"
)

var (
// static/success.svg
successicon = []byte{0x3C, 0x3F, 0x78, 0x6D, 0x6C, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x22, 0x31,
successicon []byte = []byte{0x3C, 0x3F, 0x78, 0x6D, 0x6C, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x22, 0x31,
0x2E, 0x30, 0x22, 0x20, 0x73, 0x74, 0x61, 0x6E, 0x64, 0x61, 0x6C, 0x6F, 0x6E, 0x65, 0x3D, 0x22,
0x6E, 0x6F, 0x22, 0x3F, 0x3E, 0x0A, 0x3C, 0x73, 0x76, 0x67, 0x20, 0x78, 0x6D, 0x6C, 0x6E, 0x73,
0x3D, 0x22, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x33, 0x2E,
Expand All @@ -36,7 +37,7 @@ var (
0x20, 0x2F, 0x3E, 0x0A, 0x3C, 0x2F, 0x73, 0x76, 0x67, 0x3E}

// static/failed.svg
failedicon = []byte{0x3C, 0x3F, 0x78, 0x6D, 0x6C, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x22, 0x31,
failedicon []byte = []byte{0x3C, 0x3F, 0x78, 0x6D, 0x6C, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x22, 0x31,
0x2E, 0x30, 0x22, 0x20, 0x73, 0x74, 0x61, 0x6E, 0x64, 0x61, 0x6C, 0x6F, 0x6E, 0x65, 0x3D, 0x22,
0x6E, 0x6F, 0x22, 0x3F, 0x3E, 0x0A, 0x3C, 0x73, 0x76, 0x67, 0x20, 0x78, 0x6D, 0x6C, 0x6E, 0x73,
0x3D, 0x22, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x33, 0x2E,
Expand Down Expand Up @@ -87,15 +88,24 @@ func init() {
// 系统为 windows 时,启用 winnotify 并显示对应的帮助信息

notifyHelpMsg = "\n -n, --winnotify Send notifacation cards, only available on Windows"
handleerr := func() {
if err != nil {
errhandle("Error while sending notification: " + err.Error())
err = nil
runtime.Goexit()
}
}
notify = func(title string, message string, succeed bool) {
var tmpFile *os.File
tmpFile, err = os.CreateTemp("", "qcip-*.svg")
handleerr()
if succeed {
tmpFile.Write(successicon)
} else {
tmpFile.Write(failedicon)
}
err = tmpFile.Close()
handleerr()
iconpath := tmpFile.Name()
var notification toast.Notification
if err == nil {
Expand All @@ -106,15 +116,14 @@ func init() {
Icon: iconpath,
}
} else {
errhandle("Error while showing notification icon: " + err.Error())
notification = toast.Notification{
AppID: "QCIP",
Title: title,
Message: message,
}
}
err = notification.Push()
if err != nil {
errhandle("Error while sending notification: " + err.Error())
}
handleerr()
}
}

0 comments on commit b7d7876

Please sign in to comment.