-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuilder_dir.go
More file actions
97 lines (82 loc) · 2.69 KB
/
Copy pathbuilder_dir.go
File metadata and controls
97 lines (82 loc) · 2.69 KB
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
package ferrite
import (
"errors"
"os"
"github.com/dogmatiq/ferrite/internal/variable"
)
// Dir configures an environment variable as a filesystem directory name.
//
// name is the name of the environment variable to read. desc is a
// human-readable description of the environment variable.
func Dir(name, desc string) *DirBuilder {
b := &DirBuilder{}
b.builder.Name(name)
b.builder.Description(desc)
b.builder.NonNormativeExample("/path/to/dir", "an absolute directory path")
b.builder.NonNormativeExample("./path/to/dir", "a relative directory path")
return b
}
// DirBuilder builds a specification for a string value that is the name of a
// filesystem directory.
type DirBuilder struct {
schema variable.TypedString[DirName]
builder variable.TypedSpecBuilder[DirName]
}
var _ isBuilderOf[
DirName,
string,
*DirBuilder,
]
// WithDefault sets the default value of the variable.
//
// It is used when the environment variable is undefined or empty.
func (b *DirBuilder) WithDefault(v string) *DirBuilder {
b.builder.Default(DirName(v))
return b
}
// WithExample adds an example value to the variable's documentation.
func (b *DirBuilder) WithExample(v string, desc string) *DirBuilder {
b.builder.NormativeExample(DirName(v), desc)
return b
}
// WithMustExist adds a constraint that requires the value to refer to an
// existing directory.
func (b *DirBuilder) WithMustExist() *DirBuilder {
b.builder.BuiltInConstraint(
"**MUST** refer to a directory that already exists",
func(ctx variable.ConstraintContext, v DirName) 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 directory to exist")
}
return err
}
if !info.IsDir() {
return errors.New("the path refers to a file, expected a directory")
}
return nil
},
)
return b
}
// Required completes the build process and registers a required variable with
// Ferrite's validation system.
func (b *DirBuilder) Required(options ...RequiredOption) Required[DirName] {
return required(b.schema, &b.builder, options...)
}
// Optional completes the build process and registers an optional variable with
// Ferrite's validation system.
func (b *DirBuilder) Optional(options ...OptionalOption) Optional[DirName] {
return optional(b.schema, &b.builder, options...)
}
// Deprecated completes the build process and registers a deprecated variable
// with Ferrite's validation system.
func (b *DirBuilder) Deprecated(options ...DeprecatedOption) Deprecated[DirName] {
return deprecated(b.schema, &b.builder, options...)
}
// DirName is the name of a directory.
type DirName string