workspace

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package workspace provides types and operations for managing multiple modules in a monorepo or multi-module context.

Package workspace provides types and operations for managing multiple modules in a monorepo or multi-module context.

Index

Constants

View Source
const DefaultCacheTTL = 5 * time.Second

DefaultCacheTTL is the default time-to-live for cached discovery results.

Variables

This section is empty.

Functions

func ErrorCount

func ErrorCount(results []ExecutionResult) int

ErrorCount returns the number of failed results.

func HasErrors

func HasErrors(results []ExecutionResult) bool

HasErrors returns true if any of the results contain errors.

func SuccessCount

func SuccessCount(results []ExecutionResult) int

SuccessCount returns the number of successful results.

func TotalDuration

func TotalDuration(results []ExecutionResult) time.Duration

TotalDuration returns the sum of all result durations.

Types

type CacheInfo

type CacheInfo struct {
	HasCache  bool
	Root      string
	Age       time.Duration
	ExpiresIn time.Duration
}

CacheInfo returns information about the current cache state. Useful for debugging and testing.

type CachingDetector

type CachingDetector struct {
	// contains filtered or unexported fields
}

CachingDetector wraps a Detector and caches discovery results. This improves performance for large monorepos where multiple operations may need to detect modules within a short time window.

func NewCachingDetector

func NewCachingDetector(detector *Detector, ttl time.Duration) *CachingDetector

NewCachingDetector creates a new CachingDetector wrapping the given Detector. Uses DefaultCacheTTL if ttl is 0.

func (*CachingDetector) DetectContext

func (c *CachingDetector) DetectContext(ctx context.Context, root string) (*Context, error)

DetectContext returns cached results if valid, otherwise performs detection.

Cache is invalidated when:

  • TTL has expired
  • Root directory has changed
  • InvalidateCache() was called

func (*CachingDetector) DiscoverModules

func (c *CachingDetector) DiscoverModules(ctx context.Context, root string) ([]*Module, error)

DiscoverModules delegates to the underlying detector (not cached). Use DetectContext for cached discovery.

func (*CachingDetector) GetCacheInfo

func (c *CachingDetector) GetCacheInfo() CacheInfo

GetCacheInfo returns the current cache state.

func (*CachingDetector) InvalidateCache

func (c *CachingDetector) InvalidateCache()

InvalidateCache clears the cached results. Call this after operations that modify .version files.

type Context

type Context struct {
	// Mode indicates the execution mode based on discovery.
	Mode DetectionMode

	// Path is the .version file path for SingleModule mode.
	Path string

	// Modules contains discovered modules for MultiModule mode.
	Modules []*Module
}

Context represents the execution context determined by the detector. It encapsulates the mode and any discovered modules.

type DetectionMode

type DetectionMode int

DetectionMode indicates how the CLI should operate based on discovered .version files.

const (
	// SingleModule indicates a single .version file was found (in cwd or exactly one in subdirs).
	SingleModule DetectionMode = iota

	// MultiModule indicates multiple .version files were found in subdirectories.
	MultiModule

	// NoModules indicates no .version files were found.
	NoModules
)

func (DetectionMode) String

func (m DetectionMode) String() string

String returns a human-readable representation of the detection mode.

type Detector

type Detector struct {
	// contains filtered or unexported fields
}

Detector discovers .version files and determines execution context.

func NewDetector

func NewDetector(fs core.FileSystem, cfg *config.Config) *Detector

NewDetector creates a new Detector with the given filesystem and configuration.

func (*Detector) DetectContext

func (d *Detector) DetectContext(ctx context.Context, cwd string) (*Context, error)

DetectContext determines the execution context from the given directory. It follows this algorithm:

  1. Check if .version exists in cwd -> SingleModule mode
  2. If not, scan subdirectories for .version files
  3. If 0 found -> NoModules
  4. If 1 found -> SingleModule (use that file)
  5. If 2+ found -> MultiModule

If the configuration has explicit modules defined, they are used instead of discovery.

func (*Detector) DiscoverModules

func (d *Detector) DiscoverModules(ctx context.Context, root string) ([]*Module, error)

DiscoverModules finds all .version files in subdirectories according to configuration. It respects exclude patterns, max depth, and recursive settings.

type ExecutionResult

type ExecutionResult struct {
	// Module is the module that was operated on.
	Module *Module

	// OldVersion is the version before the operation (if applicable).
	OldVersion string

	// NewVersion is the version after the operation (if applicable).
	NewVersion string

	// Success indicates whether the operation succeeded.
	Success bool

	// Error contains the error if the operation failed.
	Error error

	// Duration is how long the operation took.
	Duration time.Duration
}

ExecutionResult represents the result of executing an operation on a module.

type Executor

type Executor struct {
	// contains filtered or unexported fields
}

Executor executes operations on multiple modules. It supports both sequential and parallel execution with error handling strategies.

func NewExecutor

func NewExecutor(opts ...ExecutorOption) *Executor

NewExecutor creates a new Executor with the given options.

func (*Executor) Run

func (e *Executor) Run(ctx context.Context, modules []*Module, op Operation) ([]ExecutionResult, error)

Run executes the given operation on all modules. It returns a slice of ExecutionResults, one for each module. The context can be used for cancellation.

type ExecutorOption

type ExecutorOption func(*Executor)

ExecutorOption configures an Executor.

func WithFailFast

func WithFailFast(failFast bool) ExecutorOption

WithFailFast causes execution to stop on the first error.

func WithParallel

func WithParallel(parallel bool) ExecutorOption

WithParallel enables parallel execution of operations.

type IgnoreFile

type IgnoreFile struct {
	// contains filtered or unexported fields
}

IgnoreFile represents a parsed .sleyignore file.

func NewIgnoreFile

func NewIgnoreFile(content string) *IgnoreFile

NewIgnoreFile creates an IgnoreFile from the given content.

func (*IgnoreFile) Matches

func (i *IgnoreFile) Matches(path string) bool

Matches checks if the given path should be ignored. It supports:

  • Exact matching: "node_modules" matches "node_modules"
  • Glob patterns: "*.tmp" matches "foo.tmp", "test/*.log" matches "test/app.log"
  • Directory patterns: "build/" matches "build" directory

func (*IgnoreFile) Patterns

func (i *IgnoreFile) Patterns() []string

Patterns returns all patterns in the ignore file.

type JSONFormatter

type JSONFormatter struct{}

JSONFormatter formats output as JSON.

func NewJSONFormatter

func NewJSONFormatter() *JSONFormatter

NewJSONFormatter creates a new JSON formatter.

func (*JSONFormatter) FormatModuleList

func (f *JSONFormatter) FormatModuleList(modules []*Module) string

FormatModuleList formats a list of modules as JSON.

func (*JSONFormatter) FormatResults

func (f *JSONFormatter) FormatResults(results []ExecutionResult) string

FormatResults formats execution results as JSON.

type Module

type Module struct {
	// Name is the module identifier, typically derived from directory name.
	Name string

	// Path is the absolute path to the module's .version file.
	Path string

	// RelPath is the relative path from workspace root to the .version file.
	RelPath string

	// CurrentVersion is the current version string for display purposes.
	// This is loaded lazily to improve discovery performance.
	CurrentVersion string

	// Dir is the directory containing the .version file.
	Dir string
}

Module represents a single versioned module within a workspace. Each module has its own .version file and can be operated on independently.

func (*Module) DisplayName

func (m *Module) DisplayName() string

DisplayName returns a formatted name suitable for TUI display. Format: "module-name (version)"

func (*Module) DisplayNameWithPath

func (m *Module) DisplayNameWithPath() string

DisplayNameWithPath returns a formatted name with path for disambiguation. Format: "module-name (version) - path/to/module" Useful in TUI when multiple modules have the same name.

func (*Module) String

func (m *Module) String() string

String returns a string representation of the module.

type Operation

type Operation interface {
	// Execute runs the operation on the given module.
	// It should return an error if the operation fails.
	Execute(ctx context.Context, mod *Module) error

	// Name returns a human-readable name for this operation.
	// Used for logging and display purposes.
	Name() string
}

Operation represents a single operation that can be executed on a module. Operations are the unit of work in the executor system.

type OutputFormatter

type OutputFormatter interface {
	// FormatResults formats a slice of execution results into a string.
	FormatResults(results []ExecutionResult) string

	// FormatModuleList formats a list of modules for display.
	FormatModuleList(modules []*Module) string
}

OutputFormatter formats execution results for display.

func GetFormatter

func GetFormatter(format string, operation string) OutputFormatter

GetFormatter returns the appropriate formatter based on the format string. Uses "updated" as the default action verb.

func GetFormatterWithVerb

func GetFormatterWithVerb(format, operation, actionVerb string) OutputFormatter

GetFormatterWithVerb returns the appropriate formatter with a custom action verb. Use this for read-only operations like "show" (checked) or "doctor" (validated).

type TableFormatter

type TableFormatter struct {
	// contains filtered or unexported fields
}

TableFormatter formats output as an ASCII table.

func NewTableFormatter

func NewTableFormatter(operation string) *TableFormatter

NewTableFormatter creates a new table formatter with default "updated" action verb.

func NewTableFormatterWithVerb

func NewTableFormatterWithVerb(operation, actionVerb string) *TableFormatter

NewTableFormatterWithVerb creates a new table formatter with a custom action verb.

func (*TableFormatter) FormatModuleList

func (f *TableFormatter) FormatModuleList(modules []*Module) string

FormatModuleList formats a list of modules as a table.

func (*TableFormatter) FormatResults

func (f *TableFormatter) FormatResults(results []ExecutionResult) string

FormatResults formats execution results as a table.

type TextFormatter

type TextFormatter struct {
	// contains filtered or unexported fields
}

TextFormatter formats output as human-readable text.

func NewTextFormatter

func NewTextFormatter(operation string) *TextFormatter

NewTextFormatter creates a new text formatter with default "updated" action verb.

func NewTextFormatterWithVerb

func NewTextFormatterWithVerb(operation, actionVerb string) *TextFormatter

NewTextFormatterWithVerb creates a new text formatter with a custom action verb.

func (*TextFormatter) FormatModuleList

func (f *TextFormatter) FormatModuleList(modules []*Module) string

FormatModuleList formats a list of modules as text.

func (*TextFormatter) FormatResults

func (f *TextFormatter) FormatResults(results []ExecutionResult) string

FormatResults formats execution results as text.

Jump to

Keyboard shortcuts

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