turbo-editors/turbo-corepublic Fork 0
main
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.

🛟 Updated. 28d5985 · on main · k33g · 4h ago
create.go · 58 lines · 1.8 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
51
52
53
54
55
56
57
58
package snippets

import (
	"fmt"
	"os"

	"codeberg.org/turbo-editors/turbo-core/profile"
	"codeberg.org/turbo-editors/turbo-core/projectfile"
)

// template returns the starter file: the profile's Templates.Snippets with the
// name of the ungrouped group and the user's own snippets path filled into its
// comments, in that order.
func template(p profile.Profile) string {
	return fmt.Sprintf(p.Templates.Snippets, ungroupedName, userPathComment(p))
}

// userPathComment returns the user's snippets 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 snippets 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.
//
//	path, err := snippets.Create(p, ".")
//	if errors.Is(err, snippets.ErrExists) {
//		// already there; open it instead
//	}
func Create(p profile.Profile, projectDir string) (string, error) {
	path := ProjectPath(p, projectDir)
	if exists(path) {
		return path, fmt.Errorf("%w: %s", ErrExists, path)
	}

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

// Exists reports whether a project has a snippets file that can be read.
func Exists(p profile.Profile, projectDir string) bool {
	return exists(ProjectPath(p, projectDir))
}

// exists reports whether a path is a regular file. A directory in its place
// counts as absent: it is not something Load could have read.
func exists(path string) bool {
	info, err := os.Stat(path)
	return err == nil && info.Mode().IsRegular()
}