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

copy.go · 54 lines · 1.5 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
// Copying the fetched directory into the project.

package configrepo

import (
	"fmt"
	"io/fs"
	"os"
	"path/filepath"

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

// copyTree copies every regular file under from into to, keeping the
// layout, and returns their names relative to to.
//
// Each file goes through projectfile.Write, so it is created the way the
// editor creates a project file itself — same mode, same atomic rename, the
// directory made on the way. Anything that is not a regular file — a
// symbolic link, a socket — is left behind: a configuration is files.
func copyTree(from, to string) ([]string, error) {
	var files []string
	err := filepath.WalkDir(from, func(path string, entry fs.DirEntry, err error) error {
		if err != nil || !entry.Type().IsRegular() {
			return err
		}
		relative, err := copyFile(from, to, path)
		if err == nil {
			files = append(files, relative)
		}
		return err
	})
	if err != nil {
		return nil, fmt.Errorf("configrepo: copying into %s: %w", to, err)
	}
	return files, nil
}

// copyFile copies one file from under from to the same place under to, and
// returns its name relative to both.
func copyFile(from, to, path string) (string, error) {
	relative, err := filepath.Rel(from, path)
	if err != nil {
		return "", err
	}
	data, err := os.ReadFile(path)
	if err != nil {
		return "", err
	}
	if err := projectfile.Write(filepath.Join(to, relative), data); err != nil {
		return "", err
	}
	return filepath.ToSlash(relative), nil
}