source

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package source provides abstractions for reading files from various data sources. It defines a unified interface that supports local filesystem, Git repositories, and potentially other sources like S3 or GitLab in the future.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPath indicates the provided path is invalid or inaccessible.
	ErrInvalidPath = errors.New("source: invalid path")

	// ErrGitCloneFailed indicates a git clone operation failed.
	ErrGitCloneFailed = errors.New("source: git clone failed")

	// ErrRepositoryNotFound indicates the repository does not exist or is inaccessible.
	ErrRepositoryNotFound = errors.New("source: repository not found")
)

Sentinel errors for source operations.

Functions

func RepoPathFromURL

func RepoPathFromURL(repoURL string) (string, error)

RepoPathFromURL extracts a filesystem-safe path from a repository URL. Example: "https://github.com/owner/repo.git" -> "github.com/owner/repo" Credentials in the URL are stripped and not included in error messages.

func VerifyGitInstalled

func VerifyGitInstalled() error

VerifyGitInstalled checks if git is available in the system PATH.

func VerifyRepository

func VerifyRepository(ctx context.Context, repoURL string, creds *GitCredentials) error

VerifyRepository checks if a repository exists and is accessible. It uses git ls-remote which doesn't count against API rate limits.

Types

type GitCredentials

type GitCredentials struct {
	Username string
	Password string
}

GitCredentials contains authentication information for Git operations.

type GitOptions

type GitOptions struct {
	Branch      string
	Depth       int
	Credentials *GitCredentials
}

GitOptions configures how a Git repository is cloned.

type GitSource

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

GitSource implements Source for Git repository access. It clones the repository to a temporary directory and provides filesystem-like access to its contents.

func NewGitSource

func NewGitSource(ctx context.Context, repoURL string, opts *GitOptions) (*GitSource, error)

NewGitSource clones a Git repository and returns a Source for accessing its files. The repository is cloned to a temporary directory with shallow clone (depth 1) by default. The caller must call Close() to clean up the cloned repository.

func (*GitSource) Branch

func (s *GitSource) Branch() string

Branch returns the branch name of the cloned repository.

func (*GitSource) Close

func (s *GitSource) Close() error

Close removes the cloned repository and releases resources. Close is idempotent; calling it multiple times has no additional effect.

func (*GitSource) CommitSHA

func (s *GitSource) CommitSHA() string

CommitSHA returns the HEAD commit SHA of the cloned repository.

func (*GitSource) Open

func (s *GitSource) Open(ctx context.Context, path string) (io.ReadCloser, error)

Open opens the file at the given path for reading.

func (*GitSource) Root

func (s *GitSource) Root() string

Root returns the path to the cloned repository.

func (*GitSource) Stat

func (s *GitSource) Stat(ctx context.Context, path string) (fs.FileInfo, error)

Stat returns file info for the given path.

type LocalSource

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

LocalSource implements Source for local filesystem access.

func NewLocalSource

func NewLocalSource(rootPath string) (*LocalSource, error)

NewLocalSource creates a new LocalSource for the given root path. The path must be an existing directory. Relative paths are converted to absolute paths.

func (*LocalSource) Close

func (s *LocalSource) Close() error

Close is a no-op for LocalSource as there are no resources to release.

func (*LocalSource) Open

func (s *LocalSource) Open(_ context.Context, path string) (io.ReadCloser, error)

Open opens the file at the given path for reading. The path must be relative to the source root. Paths attempting to escape the root directory will return ErrInvalidPath.

func (*LocalSource) Root

func (s *LocalSource) Root() string

Root returns the absolute path to the source root directory.

func (*LocalSource) Stat

func (s *LocalSource) Stat(_ context.Context, path string) (fs.FileInfo, error)

Stat returns file info for the given path. The path must be relative to the source root. Paths attempting to escape the root directory will return ErrInvalidPath.

type Source

type Source interface {
	// Root returns the root path of the source.
	// For LocalSource, this is the absolute path to the directory.
	// For GitSource, this is the path to the cloned repository.
	Root() string

	// Open opens the file at the given path for reading.
	// The path should be relative to the source root.
	// Callers must close the returned ReadCloser when done.
	Open(ctx context.Context, path string) (io.ReadCloser, error)

	// Stat returns file info for the given path.
	// The path should be relative to the source root.
	Stat(ctx context.Context, path string) (fs.FileInfo, error)

	// Close releases any resources held by the source.
	// For GitSource, this removes the cloned repository.
	// Close is idempotent; calling it multiple times has no effect.
	Close() error
}

Source defines the interface for reading files from a data source. Implementations must be safe for concurrent use.

Jump to

Keyboard shortcuts

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