grafana_gen/template.go

58 lines
1.4 KiB
Go
Executable file

package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"os"
"path/filepath"
)
func loadTemplate(templatesPath string) (*DashboardTemplate, error) {
templateFile := filepath.Join(templatesPath, "dashboard_template.json")
data, err := os.ReadFile(templateFile)
if err != nil {
return nil, fmt.Errorf("failed to read template file: %w", err)
}
var template DashboardTemplate
if err := json.Unmarshal(data, &template); err != nil {
return nil, fmt.Errorf("failed to parse template JSON: %w", err)
}
return &template, nil
}
func loadAlertTemplate(templatesPath string) (*AlertRulesTemplate, error) {
templateFile := filepath.Join(templatesPath, "alert_rules_template.json")
data, err := os.ReadFile(templateFile)
if err != nil {
return nil, fmt.Errorf("failed to read alert template file: %w", err)
}
var tmpl AlertRulesTemplate
if err := json.Unmarshal(data, &tmpl); err != nil {
return nil, fmt.Errorf("failed to parse alert template JSON: %w", err)
}
return &tmpl, nil
}
// hashAlertTemplate считает SHA256 от содержимого alert_rules_template.json
func hashAlertTemplate(templatesPath string) string {
data, err := os.ReadFile(filepath.Join(templatesPath, "alert_rules_template.json"))
if err != nil {
return ""
}
hash := sha256.Sum256(data)
return fmt.Sprintf("%x", hash)
}
func deepCopy(src interface{}) interface{} {
data, _ := json.Marshal(src)
var dst interface{}
json.Unmarshal(data, &dst)
return dst
}