Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Simplify IPFS interface #217

Closed
JustinDrake opened this issue Nov 11, 2016 · 4 comments
Closed

Simplify IPFS interface #217

JustinDrake opened this issue Nov 11, 2016 · 4 comments

Comments

@JustinDrake
Copy link
Contributor

JustinDrake commented Nov 11, 2016

As I understand from talking to @cpacia, the IPFS interface code in the ipfs folder was written "in the dark" (no documentation) from the IPFS command line tool and ended up being a dirty hack. For example, inside add.go the function AddFile calls the functions PreRun, Run and PostRun which are not relevant to adding files. As explained in this issue, there is a much neater way of doing things (see whyrusleeping's answer):

func AddFile(ipfs *core.IpfsNode, file string) (string, error) {
    fi, err := os.Open(file)
    if err != nil {
        return "", err
    }

    return coreunix.Add(ipfs, fi)
}

instead of

func AddFile(ctx commands.Context, fpath string) (string, error) {
	args := []string{"add", fpath}
	req, cmd, err := NewRequest(ctx, args)
	if err != nil {
		return "", err
	}
	res := commands.NewResponse(req)
	cmd.PreRun(req)
	cmd.Run(req, res)
	var fileHash string
	for r := range res.Output().(<-chan interface{}) {
		fileHash = r.(*coreunix.AddedObject).Hash
	}
	cmd.PostRun(req, res)
	if res.Error() != nil {
		return "", res.Error()
	}
	if fileHash == "" {
		return "", addErr
	}
	return fileHash, nil
}
@cpacia
Copy link
Member

cpacia commented Nov 11, 2016

If someone wants to do that it's fine, but I think this is the only command that could be simplified that way. And doing so would make this function inconsistent with the rest of the IPFS package.

@JustinDrake
Copy link
Contributor Author

JustinDrake commented Nov 14, 2016

Why is Add is the only command that could be simplified? It seems to me that PreRun, Run and PostRun are just helper functions for the IPFS command line, which we are not using. Looking into coreunix, I see cat.go.

@cpacia
Copy link
Member

cpacia commented Nov 14, 2016

Because as far as I know add is the only one where you have a function like coreunix.add. If you can find others in the IPFS code to do the same thing for the other functions then that works.

@JustinDrake
Copy link
Contributor Author

Well for one I think there's cat.go

https://github.com/ipfs/go-ipfs/blob/master/core/coreunix/cat.go

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants