forked from photon-storage/go-ipfs-car
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
179 lines (153 loc) · 4.14 KB
/
options.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package car
import (
"errors"
"github.com/ipfs/kubo/core/coreiface/options"
mh "github.com/multiformats/go-multihash"
)
var (
ErrInvalidMhType = errors.New("invalid multihash type")
ErrIncompactibleCidVersion = errors.New("incompactible CID version")
)
type importOptions struct {
cidVersion int
mhType uint64
rawLeaves bool
rawLeavesSet bool
inline bool
inlineLimit int
chunker string
layout options.Layout
out chan *ImportEvent
includeHiddenFiles bool
ignoreFile string
ignoreRules []string
}
func buildImportOptions(opts ...ImportOption) (*importOptions, error) {
ioptions := &importOptions{
cidVersion: 1,
mhType: mh.SHA2_256,
rawLeaves: false,
rawLeavesSet: false,
inline: false,
inlineLimit: 32,
chunker: "size-262144",
layout: options.BalancedLayout,
includeHiddenFiles: false,
ignoreFile: "",
ignoreRules: nil,
}
for _, opt := range opts {
if err := opt(ioptions); err != nil {
return nil, err
}
}
if ioptions.mhType != mh.SHA2_256 && ioptions.cidVersion != 1 {
return nil, ErrIncompactibleCidVersion
}
if ioptions.cidVersion == 1 && !ioptions.rawLeavesSet {
ioptions.rawLeaves = true
}
return ioptions, nil
}
type ImportOption func(*importOptions) error
type importScope struct{}
var ImportOpts importScope
// CIDv0 uses CID v0.
func (importScope) CIDv0() ImportOption {
return func(opts *importOptions) error {
opts.cidVersion = 0
return nil
}
}
// CIDv1 uses CID v1 (default).
func (importScope) CIDv1() ImportOption {
return func(opts *importOptions) error {
opts.cidVersion = 1
return nil
}
}
// MhType sets multihash type to use (default: mh.SHA2_256).
func (importScope) MhType(code uint64) ImportOption {
return func(opts *importOptions) error {
_, found := mh.Codes[code]
if !found {
return ErrInvalidMhType
}
opts.mhType = code
return nil
}
}
// RawLeaves enables raw leaves in the DAG tree (default to false for CIDv0,
// true for CIDv1).
func (importScope) RawLeaves(enabled bool) ImportOption {
return func(opts *importOptions) error {
opts.rawLeaves = enabled
opts.rawLeavesSet = true
return nil
}
}
// InlineBlock enables inline small blocks into CID (default false).
func (importScope) InlineBlock() ImportOption {
return func(opts *importOptions) error {
opts.inline = true
return nil
}
}
// InlineBlockLimit sets the threshold for triggering inline (default 32).
func (importScope) InlineBlockLimit(limit int) ImportOption {
return func(opts *importOptions) error {
opts.inlineLimit = limit
return nil
}
}
// Chunker sets chunker configuration (default size-262144).
func (importScope) Chunker(chunker string) ImportOption {
return func(opts *importOptions) error {
opts.chunker = chunker
return nil
}
}
// BalancedLayout uses balanced DAG layout.
func (importScope) BalancedLayout() ImportOption {
return func(opts *importOptions) error {
opts.layout = options.BalancedLayout
return nil
}
}
// TrickleLayout uses trickle DAG layout.
func (importScope) TrickleLayout() ImportOption {
return func(opts *importOptions) error {
opts.layout = options.TrickleLayout
return nil
}
}
// Events sets event channel to receive import progress.
func (importScope) Events(ch chan *ImportEvent) ImportOption {
return func(opts *importOptions) error {
opts.out = ch
return nil
}
}
// IncludeHiddenFiles includes hidden files when scan filesystems.
func (importScope) IncludeHiddenFiles() ImportOption {
return func(opts *importOptions) error {
opts.includeHiddenFiles = true
return nil
}
}
// IgnoreFile specifies the gitignore style file to exclude files from
// filesystem scan.
func (importScope) IgnoreFile(path string) ImportOption {
return func(opts *importOptions) error {
opts.ignoreFile = path
return nil
}
}
// Ignores sets a set of gitignore style rules to exclude files from
// filesystem scan.
func (importScope) Ignores(rules ...string) ImportOption {
return func(opts *importOptions) error {
opts.ignoreRules = rules
return nil
}
}