Files
hakurei/internal/pkg/file.go
Ophestra 610572d0e6
All checks were successful
Test / Create distribution (push) Successful in 47s
Test / Sandbox (race detector) (push) Successful in 8m9s
Test / ShareFS (push) Successful in 8m30s
Test / Hakurei (race detector) (push) Successful in 10m35s
Test / Hakurei (push) Successful in 3m58s
Test / Sandbox (push) Successful in 1m42s
Test / Hpkg (push) Successful in 3m57s
Test / Flake checks (push) Successful in 1m46s
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 <cat@gensokyo.uk>
2026-01-12 04:34:50 +09:00

55 lines
1.4 KiB
Go

package pkg
import (
"context"
"crypto/sha512"
"fmt"
)
// A fileArtifact is an [Artifact] that cures into data known ahead of time.
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 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 }
// Params returns the result of Data.
func (a fileArtifact) Params() []byte { return a }
// Dependencies returns a nil slice.
func (a fileArtifact) Dependencies() []Artifact { return nil }
// Checksum computes and returns the checksum of caller-supplied data.
func (a fileArtifact) Checksum() Checksum {
h := sha512.New384()
h.Write(a)
return Checksum(h.Sum(nil))
}
// Cure returns the caller-supplied data.
func (a fileArtifact) Cure(context.Context) ([]byte, error) { return a, nil }