-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconfig.go
More file actions
158 lines (135 loc) · 3.57 KB
/
config.go
File metadata and controls
158 lines (135 loc) · 3.57 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package gtree
import "context"
// Definition of optional functions for Node.
type NodeOption func(*Node)
// WithDuplicationAllowed is an optional function that ensures Add method call creates and returns a new node even if a node with the same text already exists at the same hierarchy. It can be specified in the NewRoot function.
func WithDuplicationAllowed() NodeOption {
return func(n *Node) {
n.allowDuplicates = true
}
}
type config struct {
lastBranch string
midBranch string
hLine string
vLine string
// todo: refactor
lastNodeFormat *branchFormat
intermedialNodeFormat *branchFormat
massive bool
ctx context.Context
encode encode
dryrun bool
fileExtensions []string
targetDir string
strictVerify bool
noUseIterOfSimpleOutput bool
}
func newConfig(options []Option) *config {
c := &config{
lastBranch: "└",
midBranch: "├",
hLine: "──",
vLine: "│",
lastNodeFormat: &branchFormat{},
intermedialNodeFormat: &branchFormat{},
massive: false,
encode: encodeDefault,
targetDir: ".",
}
for _, opt := range options {
if opt == nil {
continue
}
opt(c)
}
c.lastNodeFormat.directly = c.lastBranch + c.hLine
c.lastNodeFormat.indirectly = " "
c.intermedialNodeFormat.directly = c.midBranch + c.hLine
c.intermedialNodeFormat.indirectly = c.vLine + " "
return c
}
// Option is functional options pattern
type Option func(*config)
// WithLastBranch allows you to change the branch parts.
func WithLastBranch(s string) Option {
return func(c *config) {
c.lastBranch = s
}
}
// WithMidBranch allows you to change the branch parts.
func WithMidBranch(s string) Option {
return func(c *config) {
c.midBranch = s
}
}
// WithHLine allows you to change the branch parts.
func WithHLine(s string) Option {
return func(c *config) {
c.hLine = s
}
}
// WithVLine allows you to change the branch parts.
func WithVLine(s string) Option {
return func(c *config) {
c.vLine = s
}
}
// 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
}
}
// Deprecated: This is for benchmark testing.
func WithNoUseIterOfSimpleOutput() Option {
return func(c *config) {
c.noUseIterOfSimpleOutput = true
}
}