From 610572d0e6a6edfe3c2ffd1bfcca1396cad6818f Mon Sep 17 00:00:00 2001 From: Ophestra Date: Mon, 12 Jan 2026 04:34:50 +0900 Subject: internal/pkg: optionally named static file These are generally for generating configuration files or build scripts, naming them is quite useful. Signed-off-by: Ophestra --- internal/pkg/file.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'internal/pkg/file.go') diff --git a/internal/pkg/file.go b/internal/pkg/file.go index fdff7351..98b8eee6 100644 --- a/internal/pkg/file.go +++ b/internal/pkg/file.go @@ -3,6 +3,7 @@ package pkg import ( "context" "crypto/sha512" + "fmt" ) // A fileArtifact is an [Artifact] that cures into data known ahead of time. @@ -10,10 +11,28 @@ type fileArtifact []byte var _ KnownChecksum = fileArtifact{} +// fileArtifactNamed embeds fileArtifact alongside a caller-supplied name. +type fileArtifactNamed struct { + fileArtifact + // Caller-supplied user-facing reporting name. + name string +} + +var _ fmt.Stringer = fileArtifactNamed{} + +// String returns the caller-supplied reporting name. +func (a fileArtifactNamed) String() string { return a.name } + // NewFile returns a [File] that cures into a caller-supplied byte slice. // -// Caller must not modify data after NewBin returns. -func NewFile(data []byte) File { return fileArtifact(data) } +// Caller must not modify data after NewFile returns. +func NewFile(name string, data []byte) File { + f := fileArtifact(data) + if name != "" { + return fileArtifactNamed{f, name} + } + return f +} // Kind returns the hardcoded [Kind] constant. func (a fileArtifact) Kind() Kind { return KindFile } -- cgit v1.3.1