parser

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: 9 Imported by: 0

Documentation

Overview

Package parser provides a unified interface for reading version information from various file formats including JSON, YAML, TOML, raw text, and regex patterns. It is used by both the dependency-check plugin and the discovery service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldForFormat

func FieldForFormat(filename string) string

FieldForFormat returns the typical field path for common file types.

Types

type FileConfig

type FileConfig struct {
	// Path is the file path (absolute or relative).
	Path string

	// Format specifies the file format.
	Format Format

	// Field is the dot-notation path to the version field (for JSON/YAML/TOML).
	// Example: "version", "package.version", "tool.poetry.version"
	Field string

	// Pattern is the regex pattern for regex format.
	// Must contain a capturing group for the version.
	Pattern string
}

FileConfig describes how to read a version from a specific file.

type Format

type Format string

Format represents the supported file formats for version parsing.

const (
	// FormatJSON is for JSON files (package.json, etc.).
	FormatJSON Format = "json"

	// FormatYAML is for YAML files (Chart.yaml, etc.).
	FormatYAML Format = "yaml"

	// FormatTOML is for TOML files (Cargo.toml, pyproject.toml, etc.).
	FormatTOML Format = "toml"

	// FormatRaw is for plain text files where the entire content is the version.
	FormatRaw Format = "raw"

	// FormatRegex is for files requiring regex extraction.
	FormatRegex Format = "regex"
)

func FormatForFile

func FormatForFile(filename string) Format

FormatForFile detects the format based on file extension or name.

func ParseFormat

func ParseFormat(s string) Format

ParseFormat converts a string to a Format, returning FormatRaw as fallback.

func (Format) IsValid

func (f Format) IsValid() bool

IsValid returns true if the format is a known valid format.

func (Format) String

func (f Format) String() string

String returns the string representation of the format.

type ReadWriter

type ReadWriter struct {
	*Reader
	*Writer
}

ReadWriter combines Reader and Writer functionality.

func NewReadWriter

func NewReadWriter(fs core.FileSystem) *ReadWriter

NewReadWriter creates a new ReadWriter with the given filesystem.

type Reader

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

Reader provides version reading capabilities for multiple file formats.

func NewReader

func NewReader(fs core.FileSystem) *Reader

NewReader creates a new Reader with the given filesystem.

func (*Reader) Read

func (r *Reader) Read(ctx context.Context, cfg FileConfig) (*Result, error)

Read reads a version from a file based on the provided configuration.

func (*Reader) ReadVersion

func (r *Reader) ReadVersion(ctx context.Context, cfg FileConfig) (string, error)

ReadVersion is a convenience method that reads and returns just the version string.

type Result

type Result struct {
	// Version is the extracted version string.
	Version string

	// Path is the file path that was read.
	Path string

	// Format is the format that was used.
	Format Format

	// Field is the field path that was used (for structured formats).
	Field string
}

Result represents the result of reading a version from a file.

type Writer

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

Writer provides version writing capabilities for multiple file formats.

func NewWriter

func NewWriter(fs core.FileSystem) *Writer

NewWriter creates a new Writer with the given filesystem.

func (*Writer) Exists

func (w *Writer) Exists(ctx context.Context, path string) bool

Exists checks if a file exists at the given path.

func (*Writer) Write

func (w *Writer) Write(ctx context.Context, cfg FileConfig, version string) error

Write writes a version to a file based on the provided configuration.

Jump to

Keyboard shortcuts

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