segment

package
v0.1.0-dev.20260129081852 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package segment provides segment-based directory matching for writ.

Index

Constants

View Source
const EnvVarPrefix = "WRIT_SEGMENT_"

EnvVarPrefix is the prefix for segment environment variables. Segments are set via WRIT_SEGMENT_<NAME>=<value> (e.g., WRIT_SEGMENT_ROLE=server).

Variables

This section is empty.

Functions

func GroupByProject

func GroupByProject(results []MatchResult) map[string][]MatchResult

GroupByProject groups match results by project name. Within each group, directories are sorted by specificity (most specific first).

func OSFamily

func OSFamily(os string) string

OSFamily returns the OS family for a given OS. Unix matches both Darwin and Linux.

func ParseDirName

func ParseDirName(dirname string) (project string, suffixes []string)

ParseDirName parses a directory name into project and suffixes. Example: "noblefactor.Darwin.arm64" → "noblefactor", ["Darwin", "arm64"]

func ProjectNames

func ProjectNames(results []MatchResult) []string

ProjectNames returns the unique project names from match results in sorted order.

Types

type MatchResult

type MatchResult struct {
	Path     string   // Full path to directory
	Project  string   // Project name (e.g., "noblefactor")
	Suffixes []string // Matched suffixes (e.g., ["Darwin", "arm64"])
}

MatchResult represents a matched directory.

func MatchAllProjects

func MatchAllProjects(sourceRoot string, segs Segments) ([]MatchResult, error)

MatchAllProjects finds all matching directories for all projects in sourceRoot. It returns results sorted by project name, then by specificity (most specific first).

func MatchDirectories

func MatchDirectories(sourceRoot string, projects []string, segs Segments) ([]MatchResult, error)

MatchDirectories finds all matching directories in sourceRoot for the given projects. It returns results sorted by project name, then by specificity (most specific first).

func (MatchResult) Specificity

func (m MatchResult) Specificity() int

Specificity returns the number of matched suffixes. Higher specificity means more specific match.

type Segment

type Segment struct {
	Name  string // e.g., "OS", "DISTRO", "ROLE"
	Value string // e.g., "Darwin", "debian", "desktop"
}

Segment represents a single segment value (e.g., OS="Darwin", ROLE="desktop").

type Segments

type Segments []Segment

Segments is an ordered list of segments for matching. Order matters: OS, DISTRO, ARCH, then custom segments.

func DetectSegments

func DetectSegments() Segments

DetectSegments returns segments for the current platform. Returns OS, DISTRO (Linux only), and ARCH segments.

func DetectSegmentsWithNames

func DetectSegmentsWithNames(names []string) Segments

DetectSegmentsWithNames returns segments with custom segment names from config. Custom segment names (like ROLE, SITE) are appended after OS, DISTRO, ARCH. Values are empty until set via CLI --segment flags. Unassigned segments behave like DISTRO on macOS: directories with that suffix won't match.

func (Segments) AllValues

func (s Segments) AllValues() []string

AllValues returns all matchable values including OS family. For OS=Darwin, returns ["Darwin", "Unix"].

func (Segments) Get

func (s Segments) Get(name string) string

Get returns the value for a segment by name, or empty if not found.

func (Segments) LoadFromEnv

func (s Segments) LoadFromEnv() Segments

LoadFromEnv reads segment values from environment variables. Only loads values for segments already defined in segs. Environment variable format: WRIT_SEGMENT_<NAME>=<value> Returns a new Segments with values populated from environment.

func (Segments) Match

func (s Segments) Match(dirname string) bool

Match checks if a directory name matches the given segments. Returns true if all suffixes match segment values (including OS family).

func (Segments) Set

func (s Segments) Set(name, value string) Segments

Set returns a new Segments with the named segment set to value. If the segment doesn't exist, it is appended.

func (Segments) SetValues

func (s Segments) SetValues(values map[string]string) (Segments, error)

SetValues applies CLI --segment values to existing segments. Returns an error if a value references a segment name not defined in segs. Values format: map[name]value (e.g., {"ROLE": "server", "SITE": "aws"})

func (Segments) String

func (s Segments) String() string

String returns a human-readable representation of segments.

func (Segments) Values

func (s Segments) Values() []string

Values returns all non-empty segment values in order.

type UndefinedSegmentError

type UndefinedSegmentError struct {
	Name string
}

UndefinedSegmentError indicates a CLI --segment referenced an undefined segment name.

func (*UndefinedSegmentError) Error

func (e *UndefinedSegmentError) Error() string

Jump to

Keyboard shortcuts

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