go检测状态 template

发布时间 2023-05-23 21:42:52作者: 技术颜良
type systemStatus struct {
Name string
Status string
}

type InitSystemStatus struct {
Error string
ServiceList []systemStatus
}


func (n *InitSystemChecker) Output(status *InitSystemStatus) error {
tpl, isOk, err := template.TryParse(`
Systemd Service Status
Logger: journalctl -xeu SERVICE-NAME
Error: {{ .Error }}
{{ if .ServiceList -}}
Init System List:
{{- range .ServiceList }}
Name: {{.Name}} Status: {{.Status}}
{{- end }}
{{ end }}`)
if err != nil || !isOk {
if err != nil {
logger.Error("failed to render system service checkers template. error: %s", err.Error())
return err
}
return errors.New("convert system service template failed")
}
if err = tpl.Execute(os.Stdout, status); err != nil {
return err
}
return nil
}