Documentation
¶
Overview ¶
Package ghrelease provides primitives for fetching and extracting GitHub release assets.
Index ¶
- Variables
- func Extract(data []byte, filename string, opts ExtractOptions) ([]byte, error)
- func ParseChecksumFile(r io.Reader) (map[string]string, error)
- func ParseSource(source string) (owner, repo string, err error)
- func Render(pattern string, vars TemplateVars, mode TemplateMode) (string, error)
- func ValidateHash(computed, expected string) error
- func WithTagPrefix(ctx context.Context, prefix string) context.Context
- type ArchiveFormat
- type Asset
- type Client
- func (c *Client) Download(url string, w io.Writer, opts DownloadOptions) (*DownloadResult, error)
- func (c *Client) DownloadToBytes(url string, opts DownloadOptions) ([]byte, *DownloadResult, error)
- func (c *Client) FetchChecksums(ctx context.Context, url string) (map[string]string, error)
- func (c *Client) GetRelease(ctx context.Context, owner, repo, tag string) (*Release, error)
- func (c *Client) LatestRelease(ctx context.Context, owner, repo string) (string, error)
- type DownloadOptions
- type DownloadResult
- type ExtractOptions
- type Release
- type ReleaseFilter
- type TemplateMode
- type TemplateVars
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidSource = errors.New("invalid GitHub source format") ErrReleaseNotFound = errors.New("release not found") ErrAssetNotFound = errors.New("asset not found") ErrChecksumMismatch = errors.New("checksum verification failed") ErrUnknownVariable = errors.New("unknown template variable") ErrUnknownModifier = errors.New("unknown template modifier") ErrInvalidModifier = errors.New("invalid modifier syntax") ErrMultipleFiles = errors.New("archive contains multiple files, ExtractPath required") ErrFileNotFound = errors.New("file not found in archive") ErrUnsupportedFormat = errors.New("unsupported archive format") )
Package errors
Functions ¶
func Extract ¶
func Extract(data []byte, filename string, opts ExtractOptions) ([]byte, error)
Extract extracts content from an archive. Auto-detects format from filename unless opts.Format specified. If opts.ExtractPath set, extracts specific file. If archive contains single file and no ExtractPath, auto-extracts it.
func ParseChecksumFile ¶
ParseChecksumFile parses GNU coreutils style checksum files. Format: <hash> <filename> Supports both single and double space separators. Ignores blank lines and comments (lines starting with #).
func ParseSource ¶
ParseSource extracts owner and repo from various GitHub identifiers. Supports:
- owner/repo
- https://github.com/owner/repo
- https://github.com/owner/repo.git
- git@github.com:owner/repo.git
func Render ¶
func Render(pattern string, vars TemplateVars, mode TemplateMode) (string, error)
Render applies template variables to a pattern string. Supports variables: {name}, {version}, {os}, {arch}, {ext} Supports modifiers: upper, lower, title, trimprefix:PREFIX, trimsuffix:SUFFIX, replace:FROM=TO
Modifiers are chained with pipe separators and applied left-to-right:
{variable|modifier1|modifier2}
Examples:
{name}_{version}_{os}_{arch}.{ext}
{name|upper}-{version}.tar.gz
{os|replace:darwin=macos}-binary
{version|trimprefix:v}
Mode behavior:
- TemplateStrict: returns error on unknown variables/modifiers
- TemplatePermissive: preserves token unchanged (e.g., {os|bogusmod})
- TemplateFailsafe: ignores unknown modifiers, substitutes variable value (e.g., linux)
func ValidateHash ¶
ValidateHash compares a computed hash against an expected hash. Both hashes are normalized to lowercase for comparison.
func WithTagPrefix ¶ added in v0.4.0
WithTagPrefix returns a context that instructs LatestRelease to scan all releases and return the most recently published one whose tag starts with prefix. When prefix is non-empty, LatestRelease uses the paginated /releases endpoint instead of /releases/latest, because GitHub's /releases/latest does not support prefix filtering.
Example — find the latest CLI release in a multi-prefix repo:
ctx := ghrelease.WithTagPrefix(context.Background(), "cli/") tag, err := client.LatestRelease(ctx, "rancher", "ob-charts-tool")
Types ¶
type ArchiveFormat ¶
type ArchiveFormat string
ArchiveFormat represents supported archive types.
const ( FormatTarGz ArchiveFormat = "tar.gz" FormatTgz ArchiveFormat = "tgz" FormatZip ArchiveFormat = "zip" FormatGzip ArchiveFormat = "gz" FormatPlain ArchiveFormat = "plain" // no archive )
Archive format constants
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client handles GitHub API interactions.
func NewClient ¶
NewClient creates a GitHub API client. Token is optional but recommended to avoid rate limits. If token is empty, tries GITHUB_TOKEN environment variable.
func NewClientWithHTTP ¶
NewClientWithHTTP creates a client with a custom HTTP client. Allows callers to configure timeouts, proxies, etc.
func (*Client) Download ¶
func (c *Client) Download(url string, w io.Writer, opts DownloadOptions) (*DownloadResult, error)
Download fetches an asset and writes it to w while computing SHA-256. Always computes hash (returned in result). If opts.ExpectedHash is set, validates before returning.
func (*Client) DownloadToBytes ¶
func (c *Client) DownloadToBytes(url string, opts DownloadOptions) ([]byte, *DownloadResult, error)
DownloadToBytes is a convenience wrapper that downloads to a byte slice.
func (*Client) FetchChecksums ¶
FetchChecksums downloads and parses a checksum file.
func (*Client) GetRelease ¶
GetRelease fetches a specific release by tag.
func (*Client) LatestRelease ¶
LatestRelease fetches the latest release tag for a repository.
By default it calls GitHub's /releases/latest endpoint, which returns the most recently published non-prerelease, non-draft release regardless of tag format. For repositories that maintain multiple release lines under distinct tag prefixes (e.g. "v*" for a library and "cli/v*" for a CLI), attach a ReleaseFilter to the context with WithTagPrefix so that only releases matching the desired prefix are considered.
type DownloadOptions ¶
type DownloadOptions struct {
// If set, validates downloaded content against this hash (SHA-256 hex, case-insensitive)
ExpectedHash string
// Context for cancellation (optional)
Context context.Context
}
DownloadOptions configures asset download behavior.
type DownloadResult ¶
type DownloadResult struct {
Hash string // Computed SHA-256 hex string
Size int64 // Bytes downloaded
}
DownloadResult contains the outcome of a download operation.
type ExtractOptions ¶
type ExtractOptions struct {
// Format override (auto-detect from filename if empty)
Format ArchiveFormat
// Specific file path within archive (empty = auto-select single file)
ExtractPath string
}
ExtractOptions configures archive extraction behavior.
type ReleaseFilter ¶ added in v0.4.0
type ReleaseFilter struct {
// TagPrefix restricts results to releases whose tag starts with this string.
// An empty prefix disables filtering and preserves the default behavior.
TagPrefix string
}
ReleaseFilter configures how LatestRelease selects a release in repositories that publish multiple release lines under different tag prefixes (e.g. "v*" for a library and "cli/v*" for a CLI tool, following Go multi-module conventions).
type TemplateMode ¶
type TemplateMode int
TemplateMode controls error handling for unknown variables.
const ( // TemplateStrict returns error on unknown variables/modifiers TemplateStrict TemplateMode = iota // TemplatePermissive passes through unknown vars/modifiers unchanged TemplatePermissive // TemplateFailsafe ignores unknown modifiers (returns unmodified value) // but preserves unknown variables as-is TemplateFailsafe )
type TemplateVars ¶
type TemplateVars struct {
Name string // Binary name
Version string // Release version/tag
OS string // Operating system (linux, darwin, windows)
Arch string // Architecture (amd64, arm64, etc)
Ext string // File extension (tar.gz, zip, etc)
}
TemplateVars holds variables for template rendering.