storeTemplateFile stores template file at the given templateFilepath. Returns true, if file content has changed (new or updated file), false if file with the same name and content was already stored locally.
(templateFilepath, content string)
| 1434 | // Returns true, if file content has changed (new or updated file), false if file with the same name |
| 1435 | // and content was already stored locally. |
| 1436 | func storeTemplateFile(templateFilepath, content string) (bool, error) { |
| 1437 | // Make sure the directory exists. |
| 1438 | dir := filepath.Dir(templateFilepath) |
| 1439 | err := os.MkdirAll(dir, 0755) |
| 1440 | if err != nil { |
| 1441 | return false, fmt.Errorf("unable to create Alertmanager templates directory %q: %s", dir, err) |
| 1442 | } |
| 1443 | |
| 1444 | // Check if the template file already exists and if it has changed |
| 1445 | if tmpl, err := os.ReadFile(templateFilepath); err == nil && string(tmpl) == content { |
| 1446 | return false, nil |
| 1447 | } else if err != nil && !os.IsNotExist(err) { |
| 1448 | return false, err |
| 1449 | } |
| 1450 | |
| 1451 | if err := os.WriteFile(templateFilepath, []byte(content), 0644); err != nil { |
| 1452 | return false, fmt.Errorf("unable to create Alertmanager template file %q: %s", templateFilepath, err) |
| 1453 | } |
| 1454 | |
| 1455 | return true, nil |
| 1456 | } |
no outgoing calls