spec

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConditionalSpansSections = errors.New("conditional block spans across section boundaries")

ErrConditionalSpansSections is returned when a conditional block (%if/%endif) spans across section boundaries, making it impossible to safely remove the section without breaking the conditional nesting structure.

View Source
var ErrNoSuchTag = errors.New("no such tag")

ErrNoSuchTag is returned when a requested tag does not exist in the spec.

View Source
var ErrPatternNotFound = errors.New("pattern not found")

ErrPatternNotFound is returned when a search pattern does not match any content in the spec.

View Source
var ErrSectionNotFound = errors.New("section not found")

ErrSectionNotFound is returned when a requested section does not exist in the spec.

Functions

func GetPackageNameFromSectionHeader

func GetPackageNameFromSectionHeader(tokens []string) string

GetPackageNameFromSectionHeader extracts the package name from the tokens of a section header line. For example, for a line like "%package -n foo", it would return "foo". For a line like "%package foo", it would return "foo" as well. Because this function does not know the base name of the spec, it cannot take a suffix-only name and resolve it to a full name.

func ParsePatchTagNumber

func ParsePatchTagNumber(tag string) (int, bool)

ParsePatchTagNumber checks if the given tag name is a PatchN tag (case-insensitive) and returns the numeric suffix N. Returns -1, false if the tag is not a PatchN tag or the suffix is not a valid integer.

func StructuralParsePatchTagNumber added in v1.1.0

func StructuralParsePatchTagNumber(tag string) (int, bool)

StructuralParsePatchTagNumber checks if the given tag name is a PatchN tag (case-insensitive) and returns the numeric suffix N. Returns -1, false if the tag is not a PatchN tag or the suffix is not a valid integer.

Types

type Context

type Context struct {
	// Target is the current visit target.
	Target VisitTarget
	// RawLine is the raw text of the current line, if applicable (nil otherwise).
	RawLine *string
	// CurrentSection is the current section being visited.
	CurrentSection SectionTarget
	// CurrentLineNum is the current (0-indexed) line number being visited.
	CurrentLineNum int
	// contains filtered or unexported fields
}

Context provides context information to a visitor function when visiting a spec.

func (*Context) InsertLinesAfter

func (ctx *Context) InsertLinesAfter(lines []string)

InsertLinesAfter inserts the provided lines just after the line currently being visited, updating the context accordingly. The next line to be visited will be the line following the newly inserted lines.

func (*Context) InsertLinesBefore

func (ctx *Context) InsertLinesBefore(lines []string)

InsertLinesBefore inserts the provided lines just before the line currently being visited, updating the context accordingly. The next line to be visited will be the line following the current one being visited.

func (*Context) RemoveLine

func (ctx *Context) RemoveLine()

RemoveLine removes the line currently being visited, updating the context accordingly. The next line to be visited will be the line that followed the removed line.

func (*Context) ReplaceLine

func (ctx *Context) ReplaceLine(replacement string)

ReplaceLine replaces the line currently being visited with the provided replacement line, updating the context accordingly.

type EditorMode added in v1.1.0

type EditorMode string

EditorMode identifies the implementation that edits an RPM spec.

const (

	// EditorLegacy uses the established line-oriented editor.
	EditorLegacy EditorMode = "legacy"
	// EditorStructural uses the lossless structural editor.
	EditorStructural EditorMode = "structural"
)

type Line

type Line struct {
	// Text is the original physical text of the line.
	Text string
	// Parsed is the parsed representation of the line's contents.
	Parsed ParsedLine
}

Line represents a single line in an RPM spec file.

type OpenOption added in v1.1.0

type OpenOption func(*editorOptions)

OpenOption configures OpenSpec.

func WithEditor added in v1.1.0

func WithEditor(mode EditorMode) OpenOption

WithEditor selects the editor implementation used by OpenSpec.

type ParsedLine

type ParsedLine interface {
	// GetType returns the type of the parsed line.
	GetType() ParsedLineType
}

ParsedLine is the interface that all parsed line types implement.

type ParsedLineType

type ParsedLineType string

ParsedLineType represents the type of a parsed line.

const (
	// SectionStart applies to lines that start a new section, e.g. "%description".
	SectionStart ParsedLineType = "SectionStart"
	// Tag applies to lines that define a tag, e.g. "Name: foo".
	Tag ParsedLineType = "Tag"
	// Raw applies to lines that are raw text, e.g. a line in a script section.
	Raw ParsedLineType = "Raw"
)

type RawLine

type RawLine struct {
	// Content is the raw line text.
	Content string
}

RawLine represents a line that is raw text.

func (*RawLine) GetType

func (*RawLine) GetType() ParsedLineType

GetType returns the type of the parsed line.

type SectionStartLine

type SectionStartLine struct {
	SectType SectionType
	SectName string
	Tokens   []string
}

SectionStartLine represents a line that starts a new section in the spec, e.g.: "%build".

func (*SectionStartLine) GetType

func (*SectionStartLine) GetType() ParsedLineType

GetType returns the type of the parsed line.

type SectionTarget

type SectionTarget struct {
	// SectName is the name of the section, e.g. "%description".
	SectName string
	// SectType is the type of the section.
	SectType SectionType
	// Package is the package this section applies to, if any. Left empty for
	// the default package or sections that aren't package-specific.
	Package string
}

SectionTarget encapsulates information about the current section context.

type SectionType

type SectionType string

SectionType represents the type of a section in an RPM spec file.

const (
	// PackageSection applies to sections that define a package, e.g. "%package -n foo".
	PackageSection SectionType = "Package"
	// ScriptSection applies to sections that contain scripts, e.g. "%build".
	ScriptSection SectionType = "Script"
	// RawSection applies to sections that contain raw content, e.g.: "%description".
	RawSection SectionType = "Raw"
	// ChangelogSection applies to the "%changelog" section.
	ChangelogSection SectionType = "Changelog"
	// FilesSection applies to a "%files" section.
	FilesSection SectionType = "Files"
	// SourceFileListSection applies to a section that lists source files, e.g.: "%sourcelist".
	SourceFileListSection SectionType = "SourceFileList"
)

type Spec

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

Spec is the public facade for a configuration-selected RPM spec editor.

func OpenSpec

func OpenSpec(reader io.Reader, options ...OpenOption) (*Spec, error)

OpenSpec reads a spec and selects its editor once. With no option, it preserves the established legacy behavior.

func (*Spec) AddChangelogEntry

func (s *Spec) AddChangelogEntry(user, email, version, release string, at time.Time, details []string) error

AddChangelogEntry adds an entry to the changelog section.

func (*Spec) AddPatchEntry

func (s *Spec) AddPatchEntry(pkg, filename string) error

AddPatchEntry registers a patch in the spec.

func (*Spec) AddTag

func (s *Spec) AddTag(pkg, tag, value string) error

AddTag adds a tag to the specified package.

func (*Spec) AppendLines added in v0.2.0

func (s *Spec) AppendLines(lines []string)

AppendLines adds lines at the end of the spec.

func (*Spec) AppendLinesToSection

func (s *Spec) AppendLinesToSection(section, pkg string, lines []string) error

AppendLinesToSection adds lines at the end of a section.

func (*Spec) GetHighestPatchTagNumber

func (s *Spec) GetHighestPatchTagNumber() (int, error)

GetHighestPatchTagNumber returns the highest PatchN tag number.

func (*Spec) GetLastTag added in v1.1.0

func (s *Spec) GetLastTag(pkg, tag string) (string, error)

GetLastTag returns the final matching tag in the specified package.

func (*Spec) GetTag added in v1.1.0

func (s *Spec) GetTag(pkg, tag string) (string, error)

GetTag returns the first matching tag in the specified package.

func (*Spec) HasSection

func (s *Spec) HasSection(section string) (bool, error)

HasSection reports whether a section exists.

func (*Spec) InsertLinesAt

func (s *Spec) InsertLinesAt(lines []string, lineNumber int)

InsertLinesAt inserts lines before the specified 0-indexed line number.

func (*Spec) InsertTag

func (s *Spec) InsertTag(pkg, tag, value string) error

InsertTag inserts a tag in the specified package.

func (*Spec) PrependLines added in v0.2.0

func (s *Spec) PrependLines(lines []string)

PrependLines adds lines at the start of the spec.

func (*Spec) PrependLinesToSection

func (s *Spec) PrependLinesToSection(section, pkg string, lines []string) error

PrependLinesToSection adds lines at the start of a section.

func (*Spec) RemoveLine

func (s *Spec) RemoveLine(lineNumber int)

RemoveLine removes the line at the specified 0-indexed line number.

func (*Spec) RemoveLines added in v0.2.0

func (s *Spec) RemoveLines(start, end int)

RemoveLines removes the lines in the specified 0-indexed range.

func (*Spec) RemovePatchEntry

func (s *Spec) RemovePatchEntry(pattern string) error

RemovePatchEntry removes patch references matching pattern.

func (*Spec) RemoveSection added in v0.2.0

func (s *Spec) RemoveSection(section, pkg string) error

RemoveSection removes a section from the specified package.

func (*Spec) RemoveSubpackage added in v0.2.0

func (s *Spec) RemoveSubpackage(pkg string) error

RemoveSubpackage removes all sections for a sub-package.

func (*Spec) RemoveTag

func (s *Spec) RemoveTag(pkg, tag, value string) error

RemoveTag removes matching tags from the specified package.

func (*Spec) RemoveTagsMatching

func (s *Spec) RemoveTagsMatching(pkg string, matcher func(string, string) bool) (int, error)

RemoveTagsMatching removes tags matched by matcher from the specified package.

func (*Spec) ReplaceLine

func (s *Spec) ReplaceLine(lineNumber int, replacement string)

ReplaceLine replaces the line at the specified 0-indexed line number.

func (*Spec) SearchAndReplace

func (s *Spec) SearchAndReplace(section, pkg, regex, replacement string) error

SearchAndReplace replaces matching content in a section.

func (*Spec) Serialize

func (s *Spec) Serialize(writer io.Writer) error

Serialize writes the spec contents to writer.

func (*Spec) SetTag

func (s *Spec) SetTag(pkg, tag, value string) error

SetTag sets the value of a tag in the specified package.

func (*Spec) UpdateExistingTag

func (s *Spec) UpdateExistingTag(pkg, tag, value string) error

UpdateExistingTag updates every matching tag in the specified package.

func (*Spec) Visit

func (s *Spec) Visit(visitor Visitor) error

func (*Spec) VisitTags

func (s *Spec) VisitTags(visitor func(tagLine *TagLine, ctx *Context) error) error

VisitTags iterates over all tag lines across all packages, calling the visitor function for each one. The visitor receives the parsed TagLine and the mutation Context.

func (*Spec) VisitTagsPackage added in v0.2.0

func (s *Spec) VisitTagsPackage(packageName string, visitor func(tagLine *TagLine, ctx *Context) error) error

VisitTagsPackage iterates over all tag lines in the given package, calling the visitor function for each one. The visitor receives the parsed TagLine and the mutation Context.

type TagLine

type TagLine struct {
	// Tag is the name of the tag being defined.
	Tag string
	// Value is the value assigned to the tag.
	Value string
}

TagLine encapsulates the definition of a tag.

func (*TagLine) GetType

func (*TagLine) GetType() ParsedLineType

GetType returns the type of the parsed line.

type VisitTarget

type VisitTarget struct {
	// TargetType is the type of the current visit target.
	TargetType VisitTargetType
	// Optionally, provides more detail about the target's section.
	// Left nil when the target is not part of a section.
	Section *SectionTarget
	// Optionally, provides more detail about the target's line. Left nil
	// when the target is not a line.
	Line *Line
}

VisitTarget encapsulates the current target of a visit operation.

type VisitTargetType

type VisitTargetType string

VisitTargetType indicates the type of a visit target.

const (
	// SpecStartTarget indicates the start of the spec.
	SpecStartTarget VisitTargetType = "SpecStart"
	// SectionStartTarget indicates the start of a section.
	SectionStartTarget VisitTargetType = "SectionStart"
	// SectionLineTarget indicates a line within a section.
	SectionLineTarget VisitTargetType = "SectionLine"
	// SectionEndTarget indicates the end of a section.
	SectionEndTarget VisitTargetType = "SectionEnd"
	// SpecEndTarget indicates the end of the spec.
	SpecEndTarget VisitTargetType = "SpecEnd"
)

type Visitor

type Visitor = func(ctx *Context) error

Visitor is the type of a visitor function that can be passed to Spec.Visit.

Jump to

Keyboard shortcuts

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