Can I use ipfs as a library?

I am extending some IPFS functionalities and I would like to ask if it’s possible to use go-ipfs as library. For example to add a file I would use the same logic like the add.go cmd does:

https://github.com/ipfs/go-ipfs/blob/master/core/coreunix/add.go#L104
func (adder *Adder) add(reader io.Reader) (ipld.Node, error) {
	chnk, err := chunker.FromString(reader, adder.Chunker)
	if err != nil {
		return nil, err
	}
...

I am having some doubts because as AFIK, running an IPFS CLI cmd converts the commands into HTTP requests. Why is this done in this way? Some exclusive locks, shared resources?

Update: run into an exclusive lock ipfs/repo.lock failed: resource temporarily unavailable but solved it by running the IPFS daemon from within my code and keeping the pointer to it. AddHandler seems to work. Let’s see about the cat.

Update: all seems to work well. Although, some copy pasting because some IPFS business logic is unfortunately placed in the main/cmd package with many private functions.

go-ipfs offers a public Core API that allows to do everything.

Check https://github.com/ipfs/go-ipfs/tree/master/docs/examples/go-ipfs-as-a-library (this is linked from the docs.ipfs.io page)

1 Like