core

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package core defines interfaces and types for dependency injection. These interfaces enable proper testing without global mutable state.

Index

Constants

View Source
const (
	// PermOwnerRW is read/write for owner only (0o600).
	// Use for sensitive files: config, version files, audit logs.
	PermOwnerRW fs.FileMode = 0o600

	// PermOwnerRWGroupR is read/write for owner, read for group (0o640).
	// Use for files that need to be readable by group members.
	PermOwnerRWGroupR fs.FileMode = 0o640

	// PermPublicRead is read/write for owner, read for all (0o644).
	// Use for public files: changelogs, documentation.
	PermPublicRead fs.FileMode = 0o644

	// PermDirDefault is read/write/execute for owner, read/execute for others (0o755).
	// Use for directories that need to be traversable.
	PermDirDefault fs.FileMode = 0o755

	// PermDirPrivate is read/write/execute for owner only (0o700).
	// Use for directories containing sensitive data.
	PermDirPrivate fs.FileMode = 0o700

	// PermExecutable is the executable bit mask (0o111).
	// Use to check if a file has any executable permission.
	PermExecutable fs.FileMode = 0o111
)

File permission constants for consistent security across the codebase. These follow the principle of least privilege.

View Source
const (
	// TimeoutDefault is the default timeout for external commands (30 seconds).
	TimeoutDefault = 30 * time.Second

	// TimeoutShort is a shorter timeout for quick operations (5 seconds).
	TimeoutShort = 5 * time.Second

	// TimeoutLong is a longer timeout for potentially slow operations (2 minutes).
	TimeoutLong = 2 * time.Minute

	// TimeoutGit is the default timeout for git operations (60 seconds).
	// Git operations like clone and pull can take longer than regular commands.
	TimeoutGit = 60 * time.Second

	// TimeoutExtension is the default timeout for extension script execution (30 seconds).
	// This matches TimeoutDefault for consistency with other external operations.
	TimeoutExtension = TimeoutDefault
)

Timeout constants for external operations.

View Source
const (
	// MaxDiscoveryDepth is the default maximum directory depth for module discovery.
	MaxDiscoveryDepth = 10
)

Discovery constants for workspace module discovery.

Variables

This section is empty.

Functions

func EnsureParentDir

func EnsureParentDir(ctx context.Context, fs FileSystem, path string, perm fs.FileMode) error

EnsureParentDir creates the parent directory for a file path if it doesn't exist.

Types

type CommandCall

type CommandCall struct {
	Dir     string
	Command string
	Args    []string
}

CommandCall records a command invocation.

type CommandExecutor

type CommandExecutor interface {
	// Run executes a command and returns an error if it fails.
	Run(ctx context.Context, dir string, command string, args ...string) error

	// Output executes a command and returns its combined output.
	Output(ctx context.Context, dir string, command string, args ...string) (string, error)
}

CommandExecutor abstracts command execution for testability.

type FileCopier

type FileCopier interface {
	// CopyDir recursively copies a directory from src to dst.
	CopyDir(src, dst string) error

	// CopyFile copies a single file from src to dst with given permissions.
	CopyFile(src, dst string, perm FileMode) error
}

FileCopier abstracts file and directory copy operations.

type FileMode

type FileMode = fs.FileMode

FileMode is an alias for fs.FileMode to avoid import cycles.

type FileSystem

type FileSystem interface {
	// ReadFile reads the entire file at path and returns its contents.
	ReadFile(ctx context.Context, path string) ([]byte, error)

	// WriteFile writes data to the file at path with the given permissions.
	WriteFile(ctx context.Context, path string, data []byte, perm fs.FileMode) error

	// Stat returns file info for the path.
	Stat(ctx context.Context, path string) (fs.FileInfo, error)

	// MkdirAll creates a directory path, along with any necessary parents.
	MkdirAll(ctx context.Context, path string, perm fs.FileMode) error

	// Remove removes the file or empty directory at path.
	Remove(ctx context.Context, path string) error

	// RemoveAll removes path and any children it contains.
	RemoveAll(ctx context.Context, path string) error

	// ReadDir reads the directory named by path and returns a list of directory entries.
	ReadDir(ctx context.Context, path string) ([]fs.DirEntry, error)
}

FileSystem abstracts file system operations for testability. All methods accept context.Context to support cancellation and timeouts.

type GitBranchReader

type GitBranchReader interface {
	// GetCurrentBranch returns the current git branch name.
	GetCurrentBranch(ctx context.Context) (string, error)
}

GitBranchReader reads git branch information.

type GitClient

type GitClient interface {
	// DescribeTags returns the most recent tag reachable from HEAD.
	DescribeTags(ctx context.Context) (string, error)

	// Clone clones a repository to the given path.
	Clone(ctx context.Context, url string, path string) error

	// Pull updates the repository at path.
	Pull(ctx context.Context, path string) error

	// IsValidRepo checks if path is a valid git repository.
	IsValidRepo(path string) bool
}

GitClient abstracts git operations for testability.

type GitCommitReader

type GitCommitReader interface {
	// GetCommits returns commits between two references.
	GetCommits(since, until string) ([]string, error)
}

GitCommitReader reads git commit information.

type GitTagOperations

type GitTagOperations interface {
	// CreateAnnotatedTag creates an annotated git tag with the given name and message.
	CreateAnnotatedTag(name, message string) error

	// CreateLightweightTag creates a lightweight git tag with the given name.
	CreateLightweightTag(name string) error

	// CreateSignedTag creates a GPG-signed git tag with the given name, message, and optional key ID.
	// If keyID is empty, git uses the default signing key from user.signingkey config.
	CreateSignedTag(name, message, keyID string) error

	// TagExists checks if a git tag with the given name exists.
	TagExists(name string) (bool, error)

	// GetLatestTag returns the most recent semver tag from git.
	GetLatestTag() (string, error)

	// PushTag pushes a specific tag to the remote.
	PushTag(name string) error

	// ListTags returns all git tags matching a pattern.
	ListTags(pattern string) ([]string, error)

	// DeleteTag deletes a local git tag.
	DeleteTag(name string) error

	// DeleteRemoteTag deletes a tag from the remote repository.
	DeleteRemoteTag(name string) error
}

GitTagOperations abstracts git tag operations for testability.

type HookRunner

type HookRunner interface {
	// Run executes a hook and returns an error if it fails.
	Run(ctx context.Context) error

	// Name returns the hook's name for logging/error reporting.
	Name() string
}

HookRunner abstracts hook execution.

type MarshalUnmarshaler

type MarshalUnmarshaler interface {
	Marshaler
	Unmarshaler
}

MarshalUnmarshaler combines marshaling and unmarshaling.

type Marshaler

type Marshaler interface {
	Marshal(v any) ([]byte, error)
}

Marshaler abstracts data marshaling operations (YAML, JSON, etc.).

type MockCommandExecutor

type MockCommandExecutor struct {

	// Responses maps "command args..." to output
	Responses map[string]string

	// Errors maps "command args..." to error
	Errors map[string]error

	// Calls records all command invocations
	Calls []CommandCall

	// DefaultOutput is returned if no specific response is set
	DefaultOutput string

	// DefaultError is returned if no specific error is set
	DefaultError error
	// contains filtered or unexported fields
}

MockCommandExecutor is a mock command executor for testing.

func NewMockCommandExecutor

func NewMockCommandExecutor() *MockCommandExecutor

NewMockCommandExecutor creates a new MockCommandExecutor.

func (*MockCommandExecutor) Output

func (m *MockCommandExecutor) Output(ctx context.Context, dir string, command string, args ...string) (string, error)

func (*MockCommandExecutor) Run

func (m *MockCommandExecutor) Run(ctx context.Context, dir string, command string, args ...string) error

func (*MockCommandExecutor) SetError

func (m *MockCommandExecutor) SetError(command string, err error)

SetError sets an error for a command.

func (*MockCommandExecutor) SetResponse

func (m *MockCommandExecutor) SetResponse(command string, output string)

SetResponse sets the response for a command.

type MockFileCopier

type MockFileCopier struct {

	// CopyDirErr is the error to return from CopyDir.
	CopyDirErr error

	// CopyFileErr is the error to return from CopyFile.
	CopyFileErr error

	// CopyDirCalls tracks CopyDir calls.
	CopyDirCalls []struct{ Src, Dst string }

	// CopyFileCalls tracks CopyFile calls.
	CopyFileCalls []struct {
		Src, Dst string
		Perm     FileMode
	}
	// contains filtered or unexported fields
}

MockFileCopier is a mock file copier for testing.

func NewMockFileCopier

func NewMockFileCopier() *MockFileCopier

NewMockFileCopier creates a new MockFileCopier.

func (*MockFileCopier) CopyDir

func (m *MockFileCopier) CopyDir(src, dst string) error

func (*MockFileCopier) CopyFile

func (m *MockFileCopier) CopyFile(src, dst string, perm FileMode) error

type MockFileSystem

type MockFileSystem struct {

	// Error injection
	ReadErr   error
	WriteErr  error
	StatErr   error
	MkdirErr  error
	RemoveErr error
	// contains filtered or unexported fields
}

MockFileSystem is a thread-safe in-memory file system for testing.

func NewMockFileSystem

func NewMockFileSystem() *MockFileSystem

NewMockFileSystem creates a new MockFileSystem.

func (*MockFileSystem) GetFile

func (m *MockFileSystem) GetFile(path string) ([]byte, bool)

GetFile returns a file's content (for test assertions).

func (*MockFileSystem) MkdirAll

func (m *MockFileSystem) MkdirAll(ctx context.Context, path string, perm fs.FileMode) error

func (*MockFileSystem) ReadDir

func (m *MockFileSystem) ReadDir(ctx context.Context, path string) ([]fs.DirEntry, error)

func (*MockFileSystem) ReadFile

func (m *MockFileSystem) ReadFile(ctx context.Context, path string) ([]byte, error)

func (*MockFileSystem) Remove

func (m *MockFileSystem) Remove(ctx context.Context, path string) error

func (*MockFileSystem) RemoveAll

func (m *MockFileSystem) RemoveAll(ctx context.Context, path string) error

func (*MockFileSystem) SetDir added in v0.9.0

func (m *MockFileSystem) SetDir(path string)

SetDir marks a directory as existing (for test setup).

func (*MockFileSystem) SetFile

func (m *MockFileSystem) SetFile(path string, content []byte)

SetFile sets a file's content (for test setup).

func (*MockFileSystem) Stat

func (m *MockFileSystem) Stat(ctx context.Context, path string) (fs.FileInfo, error)

func (*MockFileSystem) WriteFile

func (m *MockFileSystem) WriteFile(ctx context.Context, path string, data []byte, perm fs.FileMode) error

type MockGitBranchReader

type MockGitBranchReader struct {

	// GetCurrentBranchErr is the error to return.
	GetCurrentBranchErr error

	// BranchName is the branch name to return.
	BranchName string
	// contains filtered or unexported fields
}

MockGitBranchReader is a mock git branch reader for testing.

func NewMockGitBranchReader

func NewMockGitBranchReader() *MockGitBranchReader

NewMockGitBranchReader creates a new MockGitBranchReader.

func (*MockGitBranchReader) GetCurrentBranch

func (m *MockGitBranchReader) GetCurrentBranch(_ context.Context) (string, error)

type MockGitClient

type MockGitClient struct {
	TagOutput    string
	TagError     error
	CloneError   error
	PullError    error
	IsValidRepos map[string]bool
	// contains filtered or unexported fields
}

MockGitClient is a mock git client for testing.

func NewMockGitClient

func NewMockGitClient() *MockGitClient

NewMockGitClient creates a new MockGitClient.

func (*MockGitClient) Clone

func (m *MockGitClient) Clone(ctx context.Context, url string, path string) error

func (*MockGitClient) DescribeTags

func (m *MockGitClient) DescribeTags(ctx context.Context) (string, error)

func (*MockGitClient) IsValidRepo

func (m *MockGitClient) IsValidRepo(path string) bool

func (*MockGitClient) Pull

func (m *MockGitClient) Pull(ctx context.Context, path string) error

type MockGitCommitReader

type MockGitCommitReader struct {

	// GetCommitsErr is the error to return from GetCommits.
	GetCommitsErr error

	// Commits is the list of commits to return.
	Commits []string

	// Calls tracks calls to GetCommits.
	Calls []struct{ Since, Until string }
	// contains filtered or unexported fields
}

MockGitCommitReader is a mock git commit reader for testing.

func NewMockGitCommitReader

func NewMockGitCommitReader() *MockGitCommitReader

NewMockGitCommitReader creates a new MockGitCommitReader.

func (*MockGitCommitReader) GetCommits

func (m *MockGitCommitReader) GetCommits(since, until string) ([]string, error)

type MockGitTagOperations

type MockGitTagOperations struct {

	// Error responses for each operation
	CreateAnnotatedTagErr   error
	CreateLightweightTagErr error
	TagExistsErr            error
	GetLatestTagErr         error
	PushTagErr              error

	// Response values
	TagExistsResult  bool
	GetLatestTagName string

	// Call tracking
	CreatedTags []string
	PushedTags  []string
	// contains filtered or unexported fields
}

MockGitTagOperations is a mock git tag operations for testing.

func NewMockGitTagOperations

func NewMockGitTagOperations() *MockGitTagOperations

NewMockGitTagOperations creates a new MockGitTagOperations.

func (*MockGitTagOperations) CreateAnnotatedTag

func (m *MockGitTagOperations) CreateAnnotatedTag(name, message string) error

func (*MockGitTagOperations) CreateLightweightTag

func (m *MockGitTagOperations) CreateLightweightTag(name string) error

func (*MockGitTagOperations) GetLatestTag

func (m *MockGitTagOperations) GetLatestTag() (string, error)

func (*MockGitTagOperations) PushTag

func (m *MockGitTagOperations) PushTag(name string) error

func (*MockGitTagOperations) TagExists

func (m *MockGitTagOperations) TagExists(name string) (bool, error)

type MockMarshaler

type MockMarshaler struct {

	// MarshalErr is the error to return from Marshal.
	MarshalErr error

	// MarshalOutput is the output to return from Marshal.
	MarshalOutput []byte

	// UnmarshalErr is the error to return from Unmarshal.
	UnmarshalErr error

	// Calls tracks marshal calls.
	MarshalCalls []any
	// contains filtered or unexported fields
}

MockMarshaler is a mock marshaler for testing.

func NewMockMarshaler

func NewMockMarshaler() *MockMarshaler

NewMockMarshaler creates a new MockMarshaler.

func (*MockMarshaler) Marshal

func (m *MockMarshaler) Marshal(v any) ([]byte, error)

func (*MockMarshaler) Unmarshal

func (m *MockMarshaler) Unmarshal(data []byte, v any) error

type MockUserDirProvider

type MockUserDirProvider struct {

	// HomeDirErr is the error to return from HomeDir.
	HomeDirErr error

	// HomeDirPath is the path to return from HomeDir.
	HomeDirPath string
	// contains filtered or unexported fields
}

MockUserDirProvider is a mock user directory provider for testing.

func NewMockUserDirProvider

func NewMockUserDirProvider() *MockUserDirProvider

NewMockUserDirProvider creates a new MockUserDirProvider.

func (*MockUserDirProvider) HomeDir

func (m *MockUserDirProvider) HomeDir() (string, error)

type OSFileSystem

type OSFileSystem struct{}

OSFileSystem implements FileSystem using the standard os package.

func NewOSFileSystem

func NewOSFileSystem() *OSFileSystem

NewOSFileSystem returns a new OSFileSystem.

func (*OSFileSystem) MkdirAll

func (f *OSFileSystem) MkdirAll(ctx context.Context, path string, perm fs.FileMode) error

func (*OSFileSystem) ReadDir

func (f *OSFileSystem) ReadDir(ctx context.Context, path string) ([]fs.DirEntry, error)

func (*OSFileSystem) ReadFile

func (f *OSFileSystem) ReadFile(ctx context.Context, path string) ([]byte, error)

func (*OSFileSystem) Remove

func (f *OSFileSystem) Remove(ctx context.Context, path string) error

func (*OSFileSystem) RemoveAll

func (f *OSFileSystem) RemoveAll(ctx context.Context, path string) error

func (*OSFileSystem) Stat

func (f *OSFileSystem) Stat(ctx context.Context, path string) (fs.FileInfo, error)

func (*OSFileSystem) WriteFile

func (f *OSFileSystem) WriteFile(ctx context.Context, path string, data []byte, perm fs.FileMode) error

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(data []byte, v any) error
}

Unmarshaler abstracts data unmarshaling operations.

type UserDirProvider

type UserDirProvider interface {
	// HomeDir returns the current user's home directory.
	HomeDir() (string, error)
}

UserDirProvider provides user directory information.

type VersionManager

type VersionManager interface {
	VersionReader
	VersionWriter

	// Initialize creates a version file if it doesn't exist.
	Initialize(ctx context.Context, path string) (created bool, err error)
}

VersionManager combines reading and writing operations.

type VersionReader

type VersionReader interface {
	// Read reads a version from the given path.
	Read(ctx context.Context, path string) (string, error)
}

VersionReader abstracts version file reading operations.

type VersionWriter

type VersionWriter interface {
	// Write writes a version string to the given path.
	Write(ctx context.Context, path string, version string) error
}

VersionWriter abstracts version file writing operations.

Jump to

Keyboard shortcuts

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