@cryptotaxi247 / kubo / commits / 727b6bf9a

udpated commands and RPC dialing to work with new configuration changes

Jeromy committed Sep 27, 2014 at 16:02 UTC 727b6bf9a3f58f8874cd725b2a0b5325548c8a09
11 files changed +104 -101
README.md
+7
@@ -45,6 +45,13 @@ Advanced Commands:
45 Use "ipfs help <command>" for more information about a command.
46 ```
47
48 +## Getting Started
49 +To start using ipfs, you must first initialize ipfs's config files on your system, this is done with `ipfs init`. See `ipfs help init` for information on arguments it takes. After initialization is complete, you can use `ipfs mount`, `ipfs add` and any of the other commands to explore!
50 +
51 +
52 +NOTE: if you have previously installed ipfs before and you are running into problems getting it to work, try deleting (or backing up somewhere else) your config directory (~/.go-ipfs/config by default) and rerunning `ipfs init`.
53 +
54 +
55 ## Contributing
56
57 go-ipfs is MIT licensed open source software. We welcome contributions big and small! Please make sure to check the [issues](https://github.com/jbenet/go-ipfs/issues). Search the closed ones before reporting things, and help us with the open ones.
cmd/ipfs/add.go
+1 -29
@@ -2,13 +2,10 @@ package main
2
3 import (
4 "fmt"
5 - "os"
5
6 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
7 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
8 "github.com/jbenet/go-ipfs/core/commands"
10 - "github.com/jbenet/go-ipfs/daemon"
11 - u "github.com/jbenet/go-ipfs/util"
9 )
10
11 // Error indicating the max depth has been exceded.
@@ -32,29 +29,4 @@ func init() {
29 cmdIpfsAdd.Flag.Bool("r", false, "add objects recursively")
30 }
31
35 -func addCmd(c *commander.Command, inp []string) error {
36 - if len(inp) < 1 {
37 - u.POut(c.Long)
38 - return nil
39 - }
40 -
41 - cmd := daemon.NewCommand()
42 - cmd.Command = "add"
43 - cmd.Args = inp
44 - cmd.Opts["r"] = c.Flag.Lookup("r").Value.Get()
45 - err := daemon.SendCommand(cmd, "localhost:12345")
46 - if err != nil {
47 - // Do locally
48 - conf, err := getConfigDir(c.Parent)
49 - if err != nil {
50 - return err
51 - }
52 - n, err := localNode(conf, false)
53 - if err != nil {
54 - return err
55 - }
56 -
57 - return commands.Add(n, cmd.Args, cmd.Opts, os.Stdout)
58 - }
59 - return nil
60 -}
32 +var addCmd = MakeCommand("add", []string{"r"}, commands.Add)
cmd/ipfs/cat.go
+4 -4
@@ -1,13 +1,9 @@
1 package main
2
3 import (
4 - "os"
5 -
4 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
5 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
6 "github.com/jbenet/go-ipfs/core/commands"
9 - "github.com/jbenet/go-ipfs/daemon"
10 - u "github.com/jbenet/go-ipfs/util"
7 )
8
9 var cmdIpfsCat = &commander.Command{
@@ -22,6 +18,9 @@ var cmdIpfsCat = &commander.Command{
18 Flag: *flag.NewFlagSet("ipfs-cat", flag.ExitOnError),
19 }
20
21 +var catCmd = MakeCommand("cat", nil, commands.Cat)
22 +
23 +/*
24 func catCmd(c *commander.Command, inp []string) error {
25 if len(inp) < 1 {
26 u.POut(c.Long)
@@ -47,3 +46,4 @@ func catCmd(c *commander.Command, inp []string) error {
46 }
47 return nil
48 }
49 +*/
cmd/ipfs/gen.go new
+60
@@ -0,0 +1,60 @@
1 +package main
2 +
3 +import (
4 + "errors"
5 + "fmt"
6 + "os"
7 +
8 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
9 + "github.com/jbenet/go-ipfs/config"
10 + "github.com/jbenet/go-ipfs/core/commands"
11 + "github.com/jbenet/go-ipfs/daemon"
12 + u "github.com/jbenet/go-ipfs/util"
13 +)
14 +
15 +type CommanderFunc func(*commander.Command, []string) error
16 +
17 +// Wraps a commands.CmdFunc so that it may be safely run by the commander library
18 +func MakeCommand(cmdName string, expargs []string, cmdFn commands.CmdFunc) CommanderFunc {
19 + return func(c *commander.Command, inp []string) error {
20 + if len(inp) < 1 {
21 + u.POut(c.Long)
22 + return nil
23 + }
24 + confdir, err := getConfigDir(c.Parent)
25 + if err != nil {
26 + return err
27 + }
28 +
29 + confapi, err := config.ReadConfigKey(confdir+"/config", "Addresses.API")
30 + if err != nil {
31 + return err
32 + }
33 +
34 + apiaddr, ok := confapi.(string)
35 + if !ok {
36 + return errors.New("ApiAddress in config file was not a string")
37 + }
38 +
39 + cmd := daemon.NewCommand()
40 + cmd.Command = cmdName
41 + cmd.Args = inp
42 +
43 + for _, a := range expargs {
44 + cmd.Opts[a] = c.Flag.Lookup(a).Value.Get()
45 + }
46 + err = daemon.SendCommand(cmd, apiaddr)
47 + if err != nil {
48 + fmt.Printf("Executing command locally: %s", err)
49 + // Do locally
50 + n, err := localNode(confdir, false)
51 + if err != nil {
52 + fmt.Println("Local node creation failed.")
53 + return err
54 + }
55 +
56 + return cmdFn(n, cmd.Args, cmd.Opts, os.Stdout)
57 + }
58 + return nil
59 + }
60 +}
cmd/ipfs/ipfs.go
+2 -1
@@ -93,5 +93,6 @@ func getConfigDir(c *commander.Command) (string, error) {
93 if !ok {
94 return "", errors.New("failed to retrieve config flag value.")
95 }
96 - return confStr, nil
96 +
97 + return u.TildeExpansion(confStr)
98 }
cmd/ipfs/ls.go
+1 -29
@@ -1,13 +1,9 @@
1 package main
2
3 import (
4 - "os"
5 -
4 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
5 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
6 "github.com/jbenet/go-ipfs/core/commands"
9 - "github.com/jbenet/go-ipfs/daemon"
10 - u "github.com/jbenet/go-ipfs/util"
7 )
8
9 var cmdIpfsLs = &commander.Command{
@@ -25,28 +21,4 @@ var cmdIpfsLs = &commander.Command{
21 Flag: *flag.NewFlagSet("ipfs-ls", flag.ExitOnError),
22 }
23
28 -func lsCmd(c *commander.Command, inp []string) error {
29 - if len(inp) < 1 {
30 - u.POut(c.Long)
31 - return nil
32 - }
33 -
34 - com := daemon.NewCommand()
35 - com.Command = "ls"
36 - com.Args = inp
37 - err := daemon.SendCommand(com, "localhost:12345")
38 - if err != nil {
39 - conf, err := getConfigDir(c.Parent)
40 - if err != nil {
41 - return err
42 - }
43 - n, err := localNode(conf, false)
44 - if err != nil {
45 - return err
46 - }
47 -
48 - return commands.Ls(n, com.Args, com.Opts, os.Stdout)
49 - }
50 -
51 - return nil
52 -}
24 +var lsCmd = MakeCommand("ls", nil, commands.Ls)
cmd/ipfs/mount_unix.go
+3 -1
@@ -37,10 +37,12 @@ func mountCmd(c *commander.Command, inp []string) error {
37
38 conf, err := getConfigDir(c.Parent)
39 if err != nil {
40 + fmt.Println("Couldnt get config dir")
41 return err
42 }
43 n, err := localNode(conf, true)
44 if err != nil {
45 + fmt.Println("Local node creation failed.")
46 return err
47 }
48
@@ -56,6 +58,7 @@ func mountCmd(c *commander.Command, inp []string) error {
58
59 dl, err := daemon.NewDaemonListener(n, maddr)
60 if err != nil {
61 + fmt.Println("Failed to create daemon listener.")
62 return err
63 }
64 go dl.Listen()
@@ -63,6 +66,5 @@ func mountCmd(c *commander.Command, inp []string) error {
66
67 mp := inp[0]
68 fmt.Printf("Mounting at %s\n", mp)
66 -
69 return rofs.Mount(n, mp)
70 }
cmd/ipfs/refs.go
+1 -31
@@ -1,13 +1,9 @@
1 package main
2
3 import (
4 - "os"
5 -
4 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
5 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
6 commands "github.com/jbenet/go-ipfs/core/commands"
9 - "github.com/jbenet/go-ipfs/daemon"
10 - u "github.com/jbenet/go-ipfs/util"
7 )
8
9 var cmdIpfsRefs = &commander.Command{
@@ -32,30 +28,4 @@ func init() {
28 cmdIpfsRefs.Flag.Bool("u", false, "unique: list each ref only once")
29 }
30
35 -func refCmd(c *commander.Command, inp []string) error {
36 - if len(inp) < 1 {
37 - u.POut(c.Long)
38 - return nil
39 - }
40 -
41 - cmd := daemon.NewCommand()
42 - cmd.Command = "refs"
43 - cmd.Args = inp
44 - cmd.Opts["r"] = c.Flag.Lookup("r").Value.Get()
45 - cmd.Opts["u"] = c.Flag.Lookup("u").Value.Get()
46 - err := daemon.SendCommand(cmd, "localhost:12345")
47 - if err != nil {
48 - // Do locally
49 - conf, err := getConfigDir(c.Parent)
50 - if err != nil {
51 - return err
52 - }
53 - n, err := localNode(conf, false)
54 - if err != nil {
55 - return err
56 - }
57 -
58 - return commands.Refs(n, cmd.Args, cmd.Opts, os.Stdout)
59 - }
60 - return nil
61 -}
31 +var refCmd = MakeCommand("refs", []string{"r", "u"}, commands.Refs)
core/commands/commands.go new
+9
@@ -0,0 +1,9 @@
1 +package commands
2 +
3 +import (
4 + "io"
5 +
6 + "github.com/jbenet/go-ipfs/core"
7 +)
8 +
9 +type CmdFunc func(*core.IpfsNode, []string, map[string]interface{}, io.Writer) error
daemon/daemon_client.go
+12 -2
@@ -5,14 +5,24 @@ import (
5 "io"
6 "net"
7 "os"
8 - "time"
8 +
9 + ma "github.com/jbenet/go-multiaddr"
10 )
11
12 //SendCommand connects to the address on the network with a timeout and encodes the connection into JSON
13 func SendCommand(command *Command, server string) error {
14
14 - conn, err := net.DialTimeout("tcp", server, time.Millisecond*300)
15 + maddr, err := ma.NewMultiaddr(server)
16 + if err != nil {
17 + return err
18 + }
19 +
20 + network, host, err := maddr.DialArgs()
21 + if err != nil {
22 + return err
23 + }
24
25 + conn, err := net.Dial(network, host)
26 if err != nil {
27 return err
28 }
routing/dht/routing.go
+4 -4
@@ -59,7 +59,7 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
59 routeLevel := 0
60 closest := dht.routingTables[routeLevel].NearestPeers(kb.ConvertKey(key), PoolSize)
61 if closest == nil || len(closest) == 0 {
62 - return nil, kb.ErrLookupFailure
62 + return nil, nil
63 }
64
65 // setup the Query
@@ -101,7 +101,7 @@ func (dht *IpfsDHT) Provide(ctx context.Context, key u.Key) error {
101 dht.providers.AddProvider(key, dht.self)
102 peers := dht.routingTables[0].NearestPeers(kb.ConvertKey(key), PoolSize)
103 if len(peers) == 0 {
104 - return kb.ErrLookupFailure
104 + return nil
105 }
106
107 //TODO FIX: this doesn't work! it needs to be sent to the actual nearest peers.
@@ -245,7 +245,7 @@ func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (*peer.Peer, error
245 routeLevel := 0
246 p = dht.routingTables[routeLevel].NearestPeer(kb.ConvertPeerID(id))
247 if p == nil {
248 - return nil, kb.ErrLookupFailure
248 + return nil, nil
249 }
250 if p.ID.Equal(id) {
251 return p, nil
@@ -287,7 +287,7 @@ func (dht *IpfsDHT) findPeerMultiple(ctx context.Context, id peer.ID) (*peer.Pee
287 routeLevel := 0
288 peers := dht.routingTables[routeLevel].NearestPeers(kb.ConvertPeerID(id), AlphaValue)
289 if len(peers) == 0 {
290 - return nil, kb.ErrLookupFailure
290 + return nil, nil
291 }
292
293 // setup query function