| 📦 Turbo Core — files rewritten outside reload into their windows; the agent window wraps what you type and keeps a long paste aside as a token (#28); configrepo fetches a shared .turbo-<slug> from a forge URL (#25) fe23288 k33g 11h ago | 1 | // Copying the fetched directory into the project. |
| 2 | |
| 3 | package configrepo |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "io/fs" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | |
| 11 | "rickub.com/turbo-editors/turbo-core/projectfile" |
| 12 | ) |
| 13 | |
| 14 | // copyTree copies every regular file under from into to, keeping the |
| 15 | // layout, and returns their names relative to to. |
| 16 | // |
| 17 | // Each file goes through projectfile.Write, so it is created the way the |
| 18 | // editor creates a project file itself — same mode, same atomic rename, the |
| 19 | // directory made on the way. Anything that is not a regular file — a |
| 20 | // symbolic link, a socket — is left behind: a configuration is files. |
| 21 | func copyTree(from, to string) ([]string, error) { |
| 22 | var files []string |
| 23 | err := filepath.WalkDir(from, func(path string, entry fs.DirEntry, err error) error { |
| 24 | if err != nil || !entry.Type().IsRegular() { |
| 25 | return err |
| 26 | } |
| 27 | relative, err := copyFile(from, to, path) |
| 28 | if err == nil { |
| 29 | files = append(files, relative) |
| 30 | } |
| 31 | return err |
| 32 | }) |
| 33 | if err != nil { |
| 34 | return nil, fmt.Errorf("configrepo: copying into %s: %w", to, err) |
| 35 | } |
| 36 | return files, nil |
| 37 | } |
| 38 | |
| 39 | // copyFile copies one file from under from to the same place under to, and |
| 40 | // returns its name relative to both. |
| 41 | func copyFile(from, to, path string) (string, error) { |
| 42 | relative, err := filepath.Rel(from, path) |
| 43 | if err != nil { |
| 44 | return "", err |
| 45 | } |
| 46 | data, err := os.ReadFile(path) |
| 47 | if err != nil { |
| 48 | return "", err |
| 49 | } |
| 50 | if err := projectfile.Write(filepath.Join(to, relative), data); err != nil { |
| 51 | return "", err |
| 52 | } |
| 53 | return filepath.ToSlash(relative), nil |
| 54 | } |