package main import ( "fmt" "io/fs" "os" "path/filepath" "strings" ) // skipped are the directories a copy for a gate never needs. var skipped = map[string]bool{".git": true, "node_modules": true, "releases": true} // copyRepository copies the repository's files into dir. func copyRepository(from, dir string) error { return filepath.WalkDir(from, func(path string, entry fs.DirEntry, err error) error { if err != nil { return err } if entry.IsDir() && skipped[entry.Name()] { return filepath.SkipDir } target := filepath.Join(dir, path) if entry.IsDir() { return os.MkdirAll(target, 0o700) } data, err := os.ReadFile(path) if err != nil { return err } return os.WriteFile(target, data, 0o600) }) } // mutateFile applies one edit to a copied file, in place. func mutateFile(path string, change edit) error { source, err := os.ReadFile(path) if err != nil { return err } if count := strings.Count(string(source), change.from); count != 1 { return fmt.Errorf("%s: the text to break occurs %d times, not once", change.file, count) } mutated := strings.Replace(string(source), change.from, change.to, 1) return os.WriteFile(path, []byte(mutated), 0o600) }