// 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 }