-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
119 lines (103 loc) · 2.6 KB
/
config.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package gtree
import "context"
type config struct {
lastNodeFormat branchFormat
intermedialNodeFormat branchFormat
massive bool
ctx context.Context
encode encode
dryrun bool
fileExtensions []string
targetDir string
strictVerify bool
}
func newConfig(options []Option) *config {
c := &config{
lastNodeFormat: branchFormat{
directly: "└──",
indirectly: " ",
},
intermedialNodeFormat: branchFormat{
directly: "├──",
indirectly: "│ ",
},
massive: false,
encode: encodeDefault,
targetDir: ".",
}
for _, opt := range options {
if opt == nil {
continue
}
opt(c)
}
return c
}
// Option is functional options pattern
type Option func(*config)
// WithBranchFormatIntermedialNode returns function for branch format.
func WithBranchFormatIntermedialNode(directly, indirectly string) Option {
return func(c *config) {
c.intermedialNodeFormat.directly = directly
c.intermedialNodeFormat.indirectly = indirectly
}
}
// WithBranchFormatLastNode returns function for branch format.
func WithBranchFormatLastNode(directly, indirectly string) Option {
return func(c *config) {
c.lastNodeFormat.directly = directly
c.lastNodeFormat.indirectly = indirectly
}
}
// WithMassive returns function for large amount roots.
func WithMassive(ctx context.Context) Option {
return func(c *config) {
c.massive = true
if ctx == nil {
ctx = context.Background()
}
c.ctx = ctx
}
}
// WithEncodeJSON returns function for output json format.
func WithEncodeJSON() Option {
return func(c *config) {
c.encode = encodeJSON
}
}
// WithEncodeYAML returns function for output yaml format.
func WithEncodeYAML() Option {
return func(c *config) {
c.encode = encodeYAML
}
}
// WithEncodeTOML returns function for output toml format.
func WithEncodeTOML() Option {
return func(c *config) {
c.encode = encodeTOML
}
}
// WithDryRun returns function for dry run. Detects node that is invalid for directory generation.
func WithDryRun() Option {
return func(c *config) {
c.dryrun = true
}
}
// WithFileExtensions returns function for creating as a file instead of a directory.
func WithFileExtensions(extensions []string) Option {
return func(c *config) {
c.fileExtensions = extensions
}
}
// WithTargetDir returns function for specifying directory. Default is current directory.
func WithTargetDir(dir string) Option {
return func(c *config) {
c.targetDir = dir
}
}
// WithStrictVerify returns function for verifing directory strictly.
func WithStrictVerify() Option {
return func(c *config) {
c.strictVerify = true
}
}