Skip to content

Commit

Permalink
Clean-up add CLI.
Browse files Browse the repository at this point in the history
Basic params are a struct and addFile/addDir are now methods on
the struct

[ reworded to conform to commit msg guidelines ]
  • Loading branch information
gatesvp committed May 14, 2015
1 parent 5cb34ee commit 441740a
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ remains to be implemented.
localIgnorePatterns = checkForLocalIgnorePatterns(parentPath, ignoreFilePatterns)
}

rootnd, err := addFile(n, addParams{file, outChan, progress, wrap, hidden, localIgnorePatterns})
addParams := adder{n, outChan, progress, wrap, hidden}
rootnd, err := addParams.addFile(file, localIgnorePatterns)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -235,13 +236,12 @@ remains to be implemented.
Type: AddedObject{},
}

type addParams struct {
file files.File
out chan interface{}
progress bool
wrap bool
hidden bool
ignoreFilePatterns []ignore.GitIgnore
type adder struct {
node *core.IpfsNode
out chan interface{}
progress bool
wrap bool
hidden bool
}

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

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

// Check for ignore files matches
for i := range params.ignoreFilePatterns {
if params.ignoreFilePatterns[i].MatchesPath(params.file.FileName()) {
log.Debugf("%s is ignored file, skipping", params.file.FileName())
return nil, &ignoreFileError{params.file.FileName()}
for i := range ignoreFilePatterns {
if ignoreFilePatterns[i].MatchesPath(file.FileName()) {
log.Debugf("%s is ignored file, skipping", file.FileName())
return nil, &ignoreFileError{file.FileName()}
}
}

// Check if "file" is actually a directory
if params.file.IsDirectory() {
return addDir(n, params)
if file.IsDirectory() {
return params.addDir(file, ignoreFilePatterns)
}

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

if params.wrap {
p, dagnode, err := coreunix.AddWrapped(n, reader, path.Base(params.file.FileName()))
p, dagnode, err := coreunix.AddWrapped(params.node, reader, path.Base(file.FileName()))
if err != nil {
return nil, err
}
params.out <- &AddedObject{
Hash: p,
Name: params.file.FileName(),
Name: file.FileName(),
}
return dagnode, nil
}

dagnode, err := add(n, reader)
dagnode, err := add(params.node, reader)
if err != nil {
return nil, err
}

log.Infof("adding file: %s", params.file.FileName())
if err := outputDagnode(params.out, params.file.FileName(), dagnode); err != nil {
log.Infof("adding file: %s", file.FileName())
if err := outputDagnode(params.out, file.FileName(), dagnode); err != nil {
return nil, err
}
return dagnode, nil
}

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

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

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

for {
file, err := params.file.NextFile()
file, err := file.NextFile()
if err != nil && err != io.EOF {
return nil, err
}
if file == nil {
break
}

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

err := outputDagnode(params.out, params.file.FileName(), tree)
err := outputDagnode(params.out, file.FileName(), tree)
if err != nil {
return nil, err
}

_, err = n.DAG.Add(tree)
_, err = params.node.DAG.Add(tree)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 441740a

Please sign in to comment.