aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/file.go
blob: 98b8eee69b0e8b41a14ff33e81fd79b299e37225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 }