Skip to content

Instantly share code, notes, and snippets.

@kalmi
Created April 4, 2016 08:19
Show Gist options
  • Save kalmi/f5c54c8261c327f6b8ed4187e2f53a02 to your computer and use it in GitHub Desktop.
Save kalmi/f5c54c8261c327f6b8ed4187e2f53a02 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/ipfs/go-ipfs-api"
"fmt"
"os"
"io/ioutil"
"log"
"path/filepath"
)
func main(){
sh := shell.NewShell("127.0.0.1:5001")
tmpDir, err := ioutil.TempDir("", "ipfs-alt-sync-test")
if err != nil {
log.Fatal("failed to get temp dir: %s", err)
}
defer os.RemoveAll(tmpDir)
/*
Will create the following hierarchy, and add it to the ipfs daemon:
- 1_emptydir
- 2_somefile.txt
- 3_nonemptydir
- somefile.txt
*/
os.Mkdir(filepath.Join(tmpDir, "1_emptydir"), 777)
ioutil.WriteFile(filepath.Join(tmpDir, "2_somefile.txt"), []byte("somecontent"), 777)
os.Mkdir(filepath.Join(tmpDir, "3_nonemptydir"), 777)
ioutil.WriteFile(filepath.Join(tmpDir, "3_nonemptydir", "somefile.txt"), []byte("somecontent"), 777)
hash, err := sh.AddDir(tmpDir)
if err != nil {
log.Fatal("Couldn't add nonempty dir")
}
fmt.Print(hash)
/*
B:\>bug.exe
QmcyqXpSYFRc65eKjW1T4X52opvvmG4DHSeeqUtLVJY6P4
B:\>ipfs ls QmcyqXpSYFRc65eKjW1T4X52opvvmG4DHSeeqUtLVJY6P4
B:\>ipfs object get QmcyqXpSYFRc65eKjW1T4X52opvvmG4DHSeeqUtLVJY6P4
{"Links":[],"Data":"\u0008\u0002\u0012?\u0007--95a955e5bfb13034cd6b0f1d6fece94172647b39db9122b368ae146fc248\r\nContent-Disposition: file; filename=\"1_emptydir\"\r\nContent-Type: multipart/mixed; boundary=d14e21c5df2dd537910f82193f4cc2b0b38e04281591c30a54c0795aa862\r\n\r\n\r\n--d14e21c5df2dd537910f82193f4cc2b0b38e04281591c30a54c0795aa862--\r\n\r\n--95a955e5bfb13034cd6b0f1d6fece94172647b39db9122b368ae146fc248\r\nContent-Disposition: file; filename=\"2_somefile.txt\"\r\nContent-Type: application/octet-stream\r\n\r\nsomecontent\r\n--95a955e5bfb13034cd6b0f1d6fece94172647b39db9122b368ae146fc248\r\nContent-Disposition: file; filename=\"3_nonemptydir\"\r\nContent-Type: multipart/mixed; boundary=7dfe609d25341aa945f3ac33bab53b648492a185be8d86cd60fc9201ca91\r\n\r\n--7dfe609d25341aa945f3ac33bab53b648492a185be8d86cd60fc9201ca91\r\nContent-Disposition: file; filename=\"3_nonemptydir%5Csomefile.txt\"\r\nContent-Type: application/octet-stream\r\n\r\nsomecontent\r\n--7dfe609d25341aa945f3ac33bab53b648492a185be8d86cd60fc9201ca91--\r\n\r\n--95a955e5bfb13034cd6b0f1d6fece94172647b39db9122b368ae146fc248--\r\n\u0018?\u0007"}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment