Documentation
¶
Overview ¶
Package rewrite rewrites a project's identity tokens across its files and moves its command directory, turning a clone of the template into a distinct project.
It is the reusable implementation behind the rename command's domain: it discovers the current and target identities (the module path from go.mod, the command name from the tracked files, the target module from the git origin remote), computes a Plan of token replacements and one directory move, and applies it through an injected FileSystem. The Git and FileSystem seams keep the engine pure and testable; OSGit and OSFileSystem back them with the real repository. The package holds no CLI or orchestration logic.
Index ¶
Constants ¶
const ( // ErrGitCommand indicates a git invocation failed. ErrGitCommand errs.Const = "git command failed" // ErrNotFound indicates a required token (module directive, cmd dir) was absent. ErrNotFound errs.Const = "not found" // ErrOpenFile indicates a file could not be read. ErrOpenFile errs.Const = "failed to open file" // ErrWriteFile indicates a file could not be written. ErrWriteFile errs.Const = "failed to write file" // ErrMoveFile indicates a path could not be moved. ErrMoveFile errs.Const = "failed to move file" )
Variables ¶
This section is empty.
Functions ¶
func Discover ¶
Discover reads the current and target identities. The current module comes from go.mod and the current name from the cmd/<name> directory among the listed files; the target module comes from the origin remote and the target name from override when non-empty, else the remote's repository name.
Types ¶
type Changed ¶
type Changed []FilePath
Changed is the set of files whose contents the engine rewrote.
type FilePath ¶
type FilePath string
FilePath is a path within the project tree, relative to its root.
type FileSystem ¶
type FileSystem interface {
// List returns the project files eligible for rewriting.
List() ([]FilePath, error)
// Read returns the contents of path.
Read(path FilePath) ([]byte, error)
// Write replaces the contents of path.
Write(path FilePath, data []byte) error
// Move renames a directory from one path to another.
Move(from, to FilePath) error
}
FileSystem is the minimal file access the engine needs. OSFileSystem backs it with the repository; tests back it with an in-memory map.
type OSFileSystem ¶
OSFileSystem implements FileSystem over the repository rooted at Root, enumerating its files through Lister (typically OSGit.Files).
func (OSFileSystem) List ¶
func (o OSFileSystem) List() ([]FilePath, error)
List enumerates the project's files via Lister.
func (OSFileSystem) Move ¶
func (o OSFileSystem) Move(from, to FilePath) error
Move renames a directory from one path to another, both resolved beneath Root.
func (OSFileSystem) Read ¶
func (o OSFileSystem) Read(path FilePath) ([]byte, error)
Read returns the contents of path resolved beneath Root.
func (OSFileSystem) Write ¶
func (o OSFileSystem) Write(path FilePath, data []byte) error
Write replaces the contents of path resolved beneath Root, preserving the file's existing permission bits. A file that does not yet exist is created with the restrictive newFilePerm rather than a hardcoded permissive mode.
type OSGit ¶
type OSGit struct {
Dir FilePath
}
OSGit implements Git by shelling out to git in a working directory.
type Plan ¶
type Plan struct {
MoveFrom FilePath
MoveTo FilePath
Replacements []Replacement
}
Plan is a computed set of content replacements and one command-directory move.
type Replacement ¶
Replacement maps an old token to its replacement.