SavePetScript writes (or removes) the JavaScript script file for a pet type.
(petType string, content string)
| 71 | |
| 72 | // SavePetScript writes (or removes) the JavaScript script file for a pet type. |
| 73 | func SavePetScript(petType string, content string) error { |
| 74 | petType = strings.ToLower(strings.TrimSpace(petType)) |
| 75 | p, ok := petTypes[petType] |
| 76 | if !ok { |
| 77 | return fmt.Errorf("pet type %q not found", petType) |
| 78 | } |
| 79 | |
| 80 | scriptPath := p.GetScriptPath() |
| 81 | |
| 82 | if content == "" { |
| 83 | if err := os.Remove(scriptPath); err != nil && !os.IsNotExist(err) { |
| 84 | return fmt.Errorf("removing pet script: %w", err) |
| 85 | } |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | if err := os.MkdirAll(filepath.Dir(scriptPath), os.ModePerm); err != nil { |
| 90 | return fmt.Errorf("creating pet scripts directory: %w", err) |
| 91 | } |
| 92 | return util.WriteFile(scriptPath, []byte(content), 0644) |
| 93 | } |
no test coverage detected