rewrite

package module
v0.3.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 5 Imported by: 0

README

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

View Source
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

func Discover(git Git, fs FileSystem, override module.Name) (Identity, Identity, error)

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 DryRun

type DryRun bool

DryRun reports whether to compute changes without writing them.

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 Git

type Git interface {
	// Remote returns the origin remote URL.
	Remote() (module.Remote, error)
}

Git provides the git queries discovery needs.

type Identity

type Identity struct {
	Module module.Path
	Name   module.Name
}

Identity is a project's identity: its Go module path and short command name.

type OSFileSystem

type OSFileSystem struct {
	Lister func() ([]FilePath, error)
	Root   FilePath
}

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.

func (OSGit) Files

func (g OSGit) Files() ([]FilePath, error)

Files returns the repository's tracked files via "git ls-files".

func (OSGit) Remote

func (g OSGit) Remote() (module.Remote, error)

Remote returns the origin remote URL via "git remote get-url origin".

type Plan

type Plan struct {
	MoveFrom     FilePath
	MoveTo       FilePath
	Replacements []Replacement
}

Plan is a computed set of content replacements and one command-directory move.

func BuildPlan

func BuildPlan(current, target Identity) Plan

BuildPlan computes the replacements and directory move that turn the current identity into the target identity.

func (Plan) Apply

func (p Plan) Apply(fs FileSystem, isDry DryRun) (Changed, error)

Apply executes the plan against fs, returning the files whose contents changed. When dry is true it reports the would-be changes without writing or moving.

type Replacement

type Replacement struct {
	From Token
	To   Token
}

Replacement maps an old token to its replacement.

type Token

type Token string

Token is a literal string substituted in file contents.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL