Files
ProjectOctopus/message_pusher/cmii/CmiiMessage.go
2024-06-14 10:37:40 +08:00

70 lines
1.4 KiB
Go
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cmii
import (
"bytes"
"io.wdd.message_pusher/pusher"
"text/template"
"wdd.io/agent-common/logger"
)
var log = logger.Log
type MessageBody struct {
Namespace string
AppName string
FromTag string
ToTag string
Replicas string
DeployStatus bool
}
const cmiiUpdateTemplate = `
{{if .DeployStatus}}
部署状态: 成功😍
{{- else }}
部署状态: 失败👿👿👿
{{- end}}
命名空间: {{.Namespace}}
应用名称: {{.AppName}}
副本数量: {{.Replicas}}
原始版本: {{.FromTag}}
目标版本: {{.ToTag}}
`
func (d *MessageBody) ParseCmiiUpdateTemplate() bytes.Buffer {
// 解析模板
tmpl, err := template.New("cmiiUpdateTemplate").Parse(cmiiUpdateTemplate)
if err != nil {
panic(err)
}
// 应用数据并打印结果
var result bytes.Buffer
err = tmpl.Execute(&result, d)
if err != nil {
panic(err)
}
return result
}
var UpdatePushOptions = []pusher.PublishOption{
pusher.WithTitle("更新应用"),
pusher.WithPriority("3"),
}
// DefaultPushUpgradeMessage 使用默认的Client 发布更新的消息
func (d *MessageBody) DefaultPushUpgradeMessage() *pusher.Message {
upgradeTemplate := d.ParseCmiiUpdateTemplate()
result, err := pusher.DefaultPusherClient.PublishDefault(upgradeTemplate, UpdatePushOptions)
if err != nil {
log.ErrorF("[PushCmiiUpgradeMessage] - message push error %s", err.Error())
return result
}
return result
}