turbo-editors/turbo-corepublic Fork 0
v1.0.2
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

📦 Turbo Core f3ade8d · on v1.0.2 · k33g · 6h ago
create.go · 50 lines · 1.6 KBGo Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package acp

import (
	"fmt"

	"rickub.com/turbo-editors/turbo-core/profile"
	"rickub.com/turbo-editors/turbo-core/projectfile"
)

// template returns the starter file: the profile's Templates.Agents with the
// editor's own project directory and the user's own agents path filled into
// it, in that order.
func template(p profile.Profile) string {
	return fmt.Sprintf(p.Templates.Agents, p.ProjectDir(), userPathComment(p))
}

// userPathComment returns the user's agents path for the template's comment,
// or a plain description when there is nowhere to put one.
func userPathComment(p profile.Profile) string {
	if path := UserPath(p); path != "" {
		return path
	}
	return "(no configuration directory on this system)"
}

// Create writes a starter agents file for a project and returns its path.
//
// A project that already has one gives ErrExists and is left untouched: a file
// somebody has been editing is never overwritten by a menu item.
//
// The file written is the profile's Templates.Agents, which is where the
// example belongs — the agent a Go project reaches for is not necessarily the
// one a Rust project does, and the path in its arguments is the editor's own
// project directory.
//
//	path, err := acp.Create(p, ".")
//	if errors.Is(err, acp.ErrExists) {
//		// already there; open it instead
//	}
func Create(p profile.Profile, projectDir string) (string, error) {
	path := ProjectPath(p, projectDir)
	if Exists(p, projectDir) {
		return path, fmt.Errorf("%w: %s", ErrExists, path)
	}

	if err := projectfile.Write(path, []byte(template(p))); err != nil {
		return path, err
	}
	return path, nil
}