generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
builder_file.go
104 lines (87 loc) · 2.9 KB
/
builder_file.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package ferrite
import (
"errors"
"io"
"os"
"github.com/dogmatiq/ferrite/internal/variable"
)
// File configures an environment variable as a filename.
//
// name is the name of the environment variable to read. desc is a
// human-readable description of the environment variable.
func File(name, desc string) *FileBuilder {
b := &FileBuilder{}
b.builder.Name(name)
b.builder.Description(desc)
b.builder.NonNormativeExample("/path/to/file", "an absolute file path")
b.builder.NonNormativeExample("./path/to/file", "a relative file path")
return b
}
// FileBuilder builds a specification for a string value that is the name of a
// file.
type FileBuilder struct {
schema variable.TypedString[FileName]
builder variable.TypedSpecBuilder[FileName]
}
var _ isBuilderOf[FileName, *FileBuilder]
// WithDefault sets the default value of the variable.
//
// It is used when the environment variable is undefined or empty.
func (b *FileBuilder) WithDefault(v string) *FileBuilder {
b.builder.Default(FileName(v))
return b
}
// WithMustExist adds a constraint that requires the value to refer to an
// existing file.
func (b *FileBuilder) WithMustExist() *FileBuilder {
b.builder.BuiltInConstraint(
"**MUST** refer to a file that already exists",
func(ctx variable.ConstraintContext, v FileName) variable.ConstraintError {
if ctx != variable.ConstraintContextFinal {
return nil
}
info, err := os.Stat(string(v))
if err != nil {
if os.IsNotExist(err) {
return errors.New("expected the file to exist")
}
return err
}
if info.IsDir() {
return errors.New("the path refers to a directory, expected a file")
}
return nil
},
)
return b
}
// Required completes the build process and registers a required variable with
// Ferrite's validation system.
func (b *FileBuilder) Required(options ...RequiredOption) Required[FileName] {
return required(b.schema, &b.builder, options...)
}
// Optional completes the build process and registers an optional variable with
// Ferrite's validation system.
func (b *FileBuilder) Optional(options ...OptionalOption) Optional[FileName] {
return optional(b.schema, &b.builder, options...)
}
// Deprecated completes the build process and registers a deprecated variable
// with Ferrite's validation system.
func (b *FileBuilder) Deprecated(options ...DeprecatedOption) Deprecated[FileName] {
return deprecated(b.schema, &b.builder, options...)
}
// FileName is the name of a file.
type FileName string
// Reader returns a reader that produces the contents of the file.
func (n FileName) Reader() (io.ReadCloser, error) {
return os.Open(string(n))
}
// ReadBytes returns the contents of the file as a byte slice.
func (n FileName) ReadBytes() ([]byte, error) {
return os.ReadFile(string(n))
}
// ReadString returns the contents of the file as a string.
func (n FileName) ReadString() (string, error) {
data, err := n.ReadBytes()
return string(data), err
}