add interface and fix

This commit is contained in:
“xHuPo” 2024-08-16 17:48:07 +08:00
parent bb46f5adc4
commit c16ee9ca2c
13 changed files with 573 additions and 89 deletions

View file

@ -113,6 +113,10 @@ var rootCmd = &cobra.Command{
if action == "update" {
msg = genUpdateMsg(region, ak, sk, id, key, cert)
}
postUrl = generateUrl(webhook, "", map[string]interface{}{
"action": "update",
"id": id,
})
}
}
@ -120,54 +124,23 @@ var rootCmd = &cobra.Command{
if notice {
// 如果有通知信息,则发送通知
if msg != "" {
var data interface{}
// 如果post为true即发送飞书富文本信息
if post {
// 构造富文本信息
postData := FeishuPostMessage{
MsgType: "post",
Content: PostContent{
Post: &Post{
ZhCn: &Message{
Title: "华为云TLS",
Content: [][]ContentItem{
{
{
Tag: "text",
TextContent: &TextContent{
Text: msg,
},
},
{
Tag: "a",
AContent: &AContent{
Text: "点击续签TLS证书",
Href: postUrl,
},
},
},
},
},
},
data = genPostAMsg("华为云TLS", msg, "点击续签TLS证书", postUrl)
} else {
// 发送text文本信息
// 构造文本信息
data = FeishuTextMessage{
MsgType: "text",
Content: TextContent{
Text: msg,
},
}
// 发送通知富文本信息
if err := postRequest(notify, postData); err != nil {
fmt.Println("Error sending message:", err)
os.Exit(1)
}
}
// 发送text文本信息
// 构造文本信息
textData := FeishuTextMessage{
MsgType: "text",
Content: TextContent{
Text: msg,
},
}
// 发送通知
if err := postRequest(notify, textData); err != nil {
if err := postRequest(notify, data); err != nil {
fmt.Println("Error sending message:", err)
os.Exit(1)
}
@ -198,15 +171,15 @@ func initConfig() {
fmt.Println(err)
os.Exit(1)
}
bin, err := os.Executable()
exec, err := os.Executable()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
binPath := filepath.Dir(bin)
exPath := filepath.Dir(exec)
viper.AddConfigPath(".")
viper.AddConfigPath(home)
viper.AddConfigPath(binPath)
viper.AddConfigPath(exPath)
viper.SetConfigName("config")
}
@ -314,7 +287,7 @@ func timeDiff(expire string) (bool, error) {
duration := now.Sub(t)
// 检查时间差是否小于1天
if duration > -24*time.Hour {
if duration > -7*24*time.Hour {
return true, nil
}
return false, nil
@ -358,7 +331,7 @@ func genStatusMsg(region, ak, sk, id string) (bool, string) {
}
if diff {
message = fmt.Sprintf("域名:%v\n\n证书有效期不足一,请及时更新:", domain)
message = fmt.Sprintf("域名:%v\n\n证书有效期不足一,请及时更新:", domain)
return true, message
}
return false, message
@ -375,3 +348,29 @@ func genUpdateMsg(region, ak, sk, id, key, cert string) string {
message = fmt.Sprintf("域名:%v\nhttps证书更新成功", domain)
return message
}
func genPostAMsg(title, message, text, url string) *FeishuPostMessage {
return &FeishuPostMessage{
MsgType: "post",
Content: PostContent{
Post: Post{
ZhCn: Message{
Title: title,
Content: [][]ContentItem{
{
{
Tag: "text",
Text: message,
},
{
Tag: "a",
Text: text,
Href: url,
},
},
},
},
},
},
}
}