This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
/
registry.go
85 lines (69 loc) · 1.62 KB
/
registry.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package files
import (
"context"
"crypto/rand"
"path"
"github.com/hashicorp/waypoint-plugin-sdk/docs"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
"github.com/hashicorp/waypoint/internal/pkg/copy"
"github.com/oklog/ulid"
)
// Registry represents access to a Files registry.
type Registry struct {
config Config
}
// Config implements Configurable
func (r *Registry) Config() (interface{}, error) {
return &r.config, nil
}
// PushFunc implements component.Registry
func (r *Registry) PushFunc() interface{} {
return r.Push
}
// Push pushes an image to the registry.
func (r *Registry) Push(
ctx context.Context,
files *Files,
ui terminal.UI,
) (*Files, error) {
// Generate a unique path for the destination file
dstID, err := ulid.New(ulid.Now(), rand.Reader)
if err != nil {
return nil, err
}
dst := path.Join(r.config.Path, files.Path, dstID.String())
err = copy.CopyDir(files.Path, dst)
if err != nil {
return nil, err
}
return &Files{Path: dst}, nil
}
// Config is the configuration structure for the registry.
type Config struct {
// Path is the path that files are stored in
Path string `hcl:"path,attr"`
}
func (r *Registry) Documentation() (*docs.Documentation, error) {
doc, err := docs.New(docs.FromConfig(&Config{}), docs.FromFunc(r.PushFunc()))
if err != nil {
return nil, err
}
doc.Description("Copies files to a specific directory")
doc.Input("files.Files")
doc.Output("files.Files")
doc.Example(`
build {
use "files" {}
registry {
use "files" {
path = "/path/to/file"
}
}
}
`)
doc.SetField(
"path",
"the filesystem path to store the files into",
)
return doc, nil
}