Skip to content

Commit 441740a

Browse files
committed
Clean-up add CLI.
Basic params are a struct and addFile/addDir are now methods on the struct [ reworded to conform to commit msg guidelines ]
1 parent 5cb34ee commit 441740a

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

core/commands/add.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ remains to be implemented.
129129
localIgnorePatterns = checkForLocalIgnorePatterns(parentPath, ignoreFilePatterns)
130130
}
131131

132-
rootnd, err := addFile(n, addParams{file, outChan, progress, wrap, hidden, localIgnorePatterns})
132+
addParams := adder{n, outChan, progress, wrap, hidden}
133+
rootnd, err := addParams.addFile(file, localIgnorePatterns)
133134
if err != nil {
134135
res.SetError(err, cmds.ErrNormal)
135136
return
@@ -235,13 +236,12 @@ remains to be implemented.
235236
Type: AddedObject{},
236237
}
237238

238-
type addParams struct {
239-
file files.File
240-
out chan interface{}
241-
progress bool
242-
wrap bool
243-
hidden bool
244-
ignoreFilePatterns []ignore.GitIgnore
239+
type adder struct {
240+
node *core.IpfsNode
241+
out chan interface{}
242+
progress bool
243+
wrap bool
244+
hidden bool
245245
}
246246

247247
func add(n *core.IpfsNode, reader io.Reader) (*dag.Node, error) {
@@ -258,75 +258,75 @@ func add(n *core.IpfsNode, reader io.Reader) (*dag.Node, error) {
258258
return node, nil
259259
}
260260

261-
func addFile(n *core.IpfsNode, params addParams) (*dag.Node, error) {
261+
func (params *adder) addFile(file files.File, ignoreFilePatterns []ignore.GitIgnore) (*dag.Node, error) {
262262
// Check if file is hidden
263-
if fileIsHidden := files.IsHidden(params.file); fileIsHidden && !params.hidden {
264-
log.Debugf("%s is hidden, skipping", params.file.FileName())
265-
return nil, &hiddenFileError{params.file.FileName()}
263+
if fileIsHidden := files.IsHidden(file); fileIsHidden && !params.hidden {
264+
log.Debugf("%s is hidden, skipping", file.FileName())
265+
return nil, &hiddenFileError{file.FileName()}
266266
}
267267

268268
// Check for ignore files matches
269-
for i := range params.ignoreFilePatterns {
270-
if params.ignoreFilePatterns[i].MatchesPath(params.file.FileName()) {
271-
log.Debugf("%s is ignored file, skipping", params.file.FileName())
272-
return nil, &ignoreFileError{params.file.FileName()}
269+
for i := range ignoreFilePatterns {
270+
if ignoreFilePatterns[i].MatchesPath(file.FileName()) {
271+
log.Debugf("%s is ignored file, skipping", file.FileName())
272+
return nil, &ignoreFileError{file.FileName()}
273273
}
274274
}
275275

276276
// Check if "file" is actually a directory
277-
if params.file.IsDirectory() {
278-
return addDir(n, params)
277+
if file.IsDirectory() {
278+
return params.addDir(file, ignoreFilePatterns)
279279
}
280280

281281
// if the progress flag was specified, wrap the file so that we can send
282282
// progress updates to the client (over the output channel)
283-
var reader io.Reader = params.file
283+
var reader io.Reader = file
284284
if params.progress {
285-
reader = &progressReader{file: params.file, out: params.out}
285+
reader = &progressReader{file: file, out: params.out}
286286
}
287287

288288
if params.wrap {
289-
p, dagnode, err := coreunix.AddWrapped(n, reader, path.Base(params.file.FileName()))
289+
p, dagnode, err := coreunix.AddWrapped(params.node, reader, path.Base(file.FileName()))
290290
if err != nil {
291291
return nil, err
292292
}
293293
params.out <- &AddedObject{
294294
Hash: p,
295-
Name: params.file.FileName(),
295+
Name: file.FileName(),
296296
}
297297
return dagnode, nil
298298
}
299299

300-
dagnode, err := add(n, reader)
300+
dagnode, err := add(params.node, reader)
301301
if err != nil {
302302
return nil, err
303303
}
304304

305-
log.Infof("adding file: %s", params.file.FileName())
306-
if err := outputDagnode(params.out, params.file.FileName(), dagnode); err != nil {
305+
log.Infof("adding file: %s", file.FileName())
306+
if err := outputDagnode(params.out, file.FileName(), dagnode); err != nil {
307307
return nil, err
308308
}
309309
return dagnode, nil
310310
}
311311

312-
func addDir(n *core.IpfsNode, params addParams) (*dag.Node, error) {
312+
func (params *adder) addDir(file files.File, ignoreFilePatterns []ignore.GitIgnore) (*dag.Node, error) {
313313

314314
tree := &dag.Node{Data: ft.FolderPBData()}
315-
log.Infof("adding directory: %s", params.file.FileName())
315+
log.Infof("adding directory: %s", file.FileName())
316316

317317
// Check for an .ipfsignore file that is local to this Dir and append to the incoming
318-
localIgnorePatterns := checkForLocalIgnorePatterns(params.file.FileName(), params.ignoreFilePatterns)
318+
localIgnorePatterns := checkForLocalIgnorePatterns(file.FileName(), ignoreFilePatterns)
319319

320320
for {
321-
file, err := params.file.NextFile()
321+
file, err := file.NextFile()
322322
if err != nil && err != io.EOF {
323323
return nil, err
324324
}
325325
if file == nil {
326326
break
327327
}
328328

329-
node, err := addFile(n, addParams{file, params.out, params.progress, false, params.hidden, localIgnorePatterns})
329+
node, err := params.addFile(file, localIgnorePatterns)
330330
if _, ok := err.(*hiddenFileError); ok {
331331
// hidden file error, set the node to nil for below
332332
node = nil
@@ -347,12 +347,12 @@ func addDir(n *core.IpfsNode, params addParams) (*dag.Node, error) {
347347
}
348348
}
349349

350-
err := outputDagnode(params.out, params.file.FileName(), tree)
350+
err := outputDagnode(params.out, file.FileName(), tree)
351351
if err != nil {
352352
return nil, err
353353
}
354354

355-
_, err = n.DAG.Add(tree)
355+
_, err = params.node.DAG.Add(tree)
356356
if err != nil {
357357
return nil, err
358358
}

0 commit comments

Comments
 (0)