| 🛟 Updated. 28d5985 k33g 6h ago | 1 | package acp |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "codeberg.org/turbo-editors/turbo-core/profile" |
| 7 | "codeberg.org/turbo-editors/turbo-core/projectfile" |
| 8 | ) |
| 9 | |
| 10 | // template returns the starter file: the profile's Templates.Agents with the |
| 11 | // editor's own project directory and the user's own agents path filled into |
| 12 | // it, in that order. |
| 13 | func template(p profile.Profile) string { |
| 14 | return fmt.Sprintf(p.Templates.Agents, p.ProjectDir(), userPathComment(p)) |
| 15 | } |
| 16 | |
| 17 | // userPathComment returns the user's agents path for the template's comment, |
| 18 | // or a plain description when there is nowhere to put one. |
| 19 | func userPathComment(p profile.Profile) string { |
| 20 | if path := UserPath(p); path != "" { |
| 21 | return path |
| 22 | } |
| 23 | return "(no configuration directory on this system)" |
| 24 | } |
| 25 | |
| 26 | // Create writes a starter agents file for a project and returns its path. |
| 27 | // |
| 28 | // A project that already has one gives ErrExists and is left untouched: a file |
| 29 | // somebody has been editing is never overwritten by a menu item. |
| 30 | // |
| 31 | // The file written is the profile's Templates.Agents, which is where the |
| 32 | // example belongs — the agent a Go project reaches for is not necessarily the |
| 33 | // one a Rust project does, and the path in its arguments is the editor's own |
| 34 | // project directory. |
| 35 | // |
| 36 | // path, err := acp.Create(p, ".") |
| 37 | // if errors.Is(err, acp.ErrExists) { |
| 38 | // // already there; open it instead |
| 39 | // } |
| 40 | func Create(p profile.Profile, projectDir string) (string, error) { |
| 41 | path := ProjectPath(p, projectDir) |
| 42 | if Exists(p, projectDir) { |
| 43 | return path, fmt.Errorf("%w: %s", ErrExists, path) |
| 44 | } |
| 45 | |
| 46 | if err := projectfile.Write(path, []byte(template(p))); err != nil { |
| 47 | return path, err |
| 48 | } |
| 49 | return path, nil |
| 50 | } |