SavePanelLayout writes yamlData to the panel layout file for the given name. name is relative to panel-layouts/ without the .yaml extension. The file must already exist; this function does not create new layout files.
(name, yamlData string)
| 620 | // name is relative to panel-layouts/ without the .yaml extension. |
| 621 | // The file must already exist; this function does not create new layout files. |
| 622 | func SavePanelLayout(name, yamlData string) error { |
| 623 | dataFiles := string(configs.GetFilePathsConfig().DataFiles) |
| 624 | path := dataFiles + "/panel-layouts/" + name + ".yaml" |
| 625 | |
| 626 | // Verify the file exists before overwriting. |
| 627 | if _, err := os.Stat(path); err != nil { |
| 628 | return fmt.Errorf("panel layout %q: %w", name, err) |
| 629 | } |
| 630 | |
| 631 | // Validate the YAML parses correctly before writing. |
| 632 | var def panelLayoutDef |
| 633 | if err := yaml.Unmarshal([]byte(yamlData), &def); err != nil { |
| 634 | return fmt.Errorf("panel layout %q: invalid YAML: %w", name, err) |
| 635 | } |
| 636 | |
| 637 | if err := os.WriteFile(path, []byte(yamlData), 0644); err != nil { |
| 638 | return fmt.Errorf("panel layout %q: %w", name, err) |
| 639 | } |
| 640 | return nil |
| 641 | } |
| 642 | |
| 643 | // LoadPanelLayout loads a panel layout definition from the datafiles directory. |
| 644 | // name is relative to the panel-layouts/ subdirectory and has no extension. |
no test coverage detected