From 727b6bf9a3f58f8874cd725b2a0b5325548c8a09 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Sat, 27 Sep 2014 16:02:50 -0700 Subject: [PATCH] udpated commands and RPC dialing to work with new configuration changes --- README.md | 7 +++++ cmd/ipfs/add.go | 30 +------------------- cmd/ipfs/cat.go | 8 +++--- cmd/ipfs/gen.go | 60 +++++++++++++++++++++++++++++++++++++++ cmd/ipfs/ipfs.go | 3 +- cmd/ipfs/ls.go | 30 +------------------- cmd/ipfs/mount_unix.go | 4 ++- cmd/ipfs/refs.go | 32 +-------------------- core/commands/commands.go | 9 ++++++ daemon/daemon_client.go | 14 +++++++-- routing/dht/routing.go | 8 +++--- 11 files changed, 104 insertions(+), 101 deletions(-) create mode 100644 cmd/ipfs/gen.go create mode 100644 core/commands/commands.go diff --git a/README.md b/README.md index df14bd227..81a231ac4 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,13 @@ Advanced Commands: Use "ipfs help " for more information about a command. ``` +## Getting Started +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! + + +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`. + + ## Contributing 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. diff --git a/cmd/ipfs/add.go b/cmd/ipfs/add.go index a8798c8e3..91eb1e6e4 100644 --- a/cmd/ipfs/add.go +++ b/cmd/ipfs/add.go @@ -2,13 +2,10 @@ package main import ( "fmt" - "os" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" "github.com/jbenet/go-ipfs/core/commands" - "github.com/jbenet/go-ipfs/daemon" - u "github.com/jbenet/go-ipfs/util" ) // Error indicating the max depth has been exceded. @@ -32,29 +29,4 @@ func init() { cmdIpfsAdd.Flag.Bool("r", false, "add objects recursively") } -func addCmd(c *commander.Command, inp []string) error { - if len(inp) < 1 { - u.POut(c.Long) - return nil - } - - cmd := daemon.NewCommand() - cmd.Command = "add" - cmd.Args = inp - cmd.Opts["r"] = c.Flag.Lookup("r").Value.Get() - err := daemon.SendCommand(cmd, "localhost:12345") - if err != nil { - // Do locally - conf, err := getConfigDir(c.Parent) - if err != nil { - return err - } - n, err := localNode(conf, false) - if err != nil { - return err - } - - return commands.Add(n, cmd.Args, cmd.Opts, os.Stdout) - } - return nil -} +var addCmd = MakeCommand("add", []string{"r"}, commands.Add) diff --git a/cmd/ipfs/cat.go b/cmd/ipfs/cat.go index 68e7daeaf..cc4598e86 100644 --- a/cmd/ipfs/cat.go +++ b/cmd/ipfs/cat.go @@ -1,13 +1,9 @@ package main import ( - "os" - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" "github.com/jbenet/go-ipfs/core/commands" - "github.com/jbenet/go-ipfs/daemon" - u "github.com/jbenet/go-ipfs/util" ) var cmdIpfsCat = &commander.Command{ @@ -22,6 +18,9 @@ var cmdIpfsCat = &commander.Command{ Flag: *flag.NewFlagSet("ipfs-cat", flag.ExitOnError), } +var catCmd = MakeCommand("cat", nil, commands.Cat) + +/* func catCmd(c *commander.Command, inp []string) error { if len(inp) < 1 { u.POut(c.Long) @@ -47,3 +46,4 @@ func catCmd(c *commander.Command, inp []string) error { } return nil } +*/ diff --git a/cmd/ipfs/gen.go b/cmd/ipfs/gen.go new file mode 100644 index 000000000..e54b09247 --- /dev/null +++ b/cmd/ipfs/gen.go @@ -0,0 +1,60 @@ +package main + +import ( + "errors" + "fmt" + "os" + + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" + "github.com/jbenet/go-ipfs/config" + "github.com/jbenet/go-ipfs/core/commands" + "github.com/jbenet/go-ipfs/daemon" + u "github.com/jbenet/go-ipfs/util" +) + +type CommanderFunc func(*commander.Command, []string) error + +// Wraps a commands.CmdFunc so that it may be safely run by the commander library +func MakeCommand(cmdName string, expargs []string, cmdFn commands.CmdFunc) CommanderFunc { + return func(c *commander.Command, inp []string) error { + if len(inp) < 1 { + u.POut(c.Long) + return nil + } + confdir, err := getConfigDir(c.Parent) + if err != nil { + return err + } + + confapi, err := config.ReadConfigKey(confdir+"/config", "Addresses.API") + if err != nil { + return err + } + + apiaddr, ok := confapi.(string) + if !ok { + return errors.New("ApiAddress in config file was not a string") + } + + cmd := daemon.NewCommand() + cmd.Command = cmdName + cmd.Args = inp + + for _, a := range expargs { + cmd.Opts[a] = c.Flag.Lookup(a).Value.Get() + } + err = daemon.SendCommand(cmd, apiaddr) + if err != nil { + fmt.Printf("Executing command locally: %s", err) + // Do locally + n, err := localNode(confdir, false) + if err != nil { + fmt.Println("Local node creation failed.") + return err + } + + return cmdFn(n, cmd.Args, cmd.Opts, os.Stdout) + } + return nil + } +} diff --git a/cmd/ipfs/ipfs.go b/cmd/ipfs/ipfs.go index 5e895175b..c160a8738 100644 --- a/cmd/ipfs/ipfs.go +++ b/cmd/ipfs/ipfs.go @@ -93,5 +93,6 @@ func getConfigDir(c *commander.Command) (string, error) { if !ok { return "", errors.New("failed to retrieve config flag value.") } - return confStr, nil + + return u.TildeExpansion(confStr) } diff --git a/cmd/ipfs/ls.go b/cmd/ipfs/ls.go index 59c7ee10c..aed286dea 100644 --- a/cmd/ipfs/ls.go +++ b/cmd/ipfs/ls.go @@ -1,13 +1,9 @@ package main import ( - "os" - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" "github.com/jbenet/go-ipfs/core/commands" - "github.com/jbenet/go-ipfs/daemon" - u "github.com/jbenet/go-ipfs/util" ) var cmdIpfsLs = &commander.Command{ @@ -25,28 +21,4 @@ var cmdIpfsLs = &commander.Command{ Flag: *flag.NewFlagSet("ipfs-ls", flag.ExitOnError), } -func lsCmd(c *commander.Command, inp []string) error { - if len(inp) < 1 { - u.POut(c.Long) - return nil - } - - com := daemon.NewCommand() - com.Command = "ls" - com.Args = inp - err := daemon.SendCommand(com, "localhost:12345") - if err != nil { - conf, err := getConfigDir(c.Parent) - if err != nil { - return err - } - n, err := localNode(conf, false) - if err != nil { - return err - } - - return commands.Ls(n, com.Args, com.Opts, os.Stdout) - } - - return nil -} +var lsCmd = MakeCommand("ls", nil, commands.Ls) diff --git a/cmd/ipfs/mount_unix.go b/cmd/ipfs/mount_unix.go index 274632f37..bb243afae 100644 --- a/cmd/ipfs/mount_unix.go +++ b/cmd/ipfs/mount_unix.go @@ -37,10 +37,12 @@ func mountCmd(c *commander.Command, inp []string) error { conf, err := getConfigDir(c.Parent) if err != nil { + fmt.Println("Couldnt get config dir") return err } n, err := localNode(conf, true) if err != nil { + fmt.Println("Local node creation failed.") return err } @@ -56,6 +58,7 @@ func mountCmd(c *commander.Command, inp []string) error { dl, err := daemon.NewDaemonListener(n, maddr) if err != nil { + fmt.Println("Failed to create daemon listener.") return err } go dl.Listen() @@ -63,6 +66,5 @@ func mountCmd(c *commander.Command, inp []string) error { mp := inp[0] fmt.Printf("Mounting at %s\n", mp) - return rofs.Mount(n, mp) } diff --git a/cmd/ipfs/refs.go b/cmd/ipfs/refs.go index ce45b29f0..f86e51dbf 100644 --- a/cmd/ipfs/refs.go +++ b/cmd/ipfs/refs.go @@ -1,13 +1,9 @@ package main import ( - "os" - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" commands "github.com/jbenet/go-ipfs/core/commands" - "github.com/jbenet/go-ipfs/daemon" - u "github.com/jbenet/go-ipfs/util" ) var cmdIpfsRefs = &commander.Command{ @@ -32,30 +28,4 @@ func init() { cmdIpfsRefs.Flag.Bool("u", false, "unique: list each ref only once") } -func refCmd(c *commander.Command, inp []string) error { - if len(inp) < 1 { - u.POut(c.Long) - return nil - } - - cmd := daemon.NewCommand() - cmd.Command = "refs" - cmd.Args = inp - cmd.Opts["r"] = c.Flag.Lookup("r").Value.Get() - cmd.Opts["u"] = c.Flag.Lookup("u").Value.Get() - err := daemon.SendCommand(cmd, "localhost:12345") - if err != nil { - // Do locally - conf, err := getConfigDir(c.Parent) - if err != nil { - return err - } - n, err := localNode(conf, false) - if err != nil { - return err - } - - return commands.Refs(n, cmd.Args, cmd.Opts, os.Stdout) - } - return nil -} +var refCmd = MakeCommand("refs", []string{"r", "u"}, commands.Refs) diff --git a/core/commands/commands.go b/core/commands/commands.go new file mode 100644 index 000000000..5bef54a7b --- /dev/null +++ b/core/commands/commands.go @@ -0,0 +1,9 @@ +package commands + +import ( + "io" + + "github.com/jbenet/go-ipfs/core" +) + +type CmdFunc func(*core.IpfsNode, []string, map[string]interface{}, io.Writer) error diff --git a/daemon/daemon_client.go b/daemon/daemon_client.go index a63f4a398..8cd126964 100644 --- a/daemon/daemon_client.go +++ b/daemon/daemon_client.go @@ -5,14 +5,24 @@ import ( "io" "net" "os" - "time" + + ma "github.com/jbenet/go-multiaddr" ) //SendCommand connects to the address on the network with a timeout and encodes the connection into JSON func SendCommand(command *Command, server string) error { - conn, err := net.DialTimeout("tcp", server, time.Millisecond*300) + maddr, err := ma.NewMultiaddr(server) + if err != nil { + return err + } + network, host, err := maddr.DialArgs() + if err != nil { + return err + } + + conn, err := net.Dial(network, host) if err != nil { return err } diff --git a/routing/dht/routing.go b/routing/dht/routing.go index a057ca828..66ae09848 100644 --- a/routing/dht/routing.go +++ b/routing/dht/routing.go @@ -59,7 +59,7 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) { routeLevel := 0 closest := dht.routingTables[routeLevel].NearestPeers(kb.ConvertKey(key), PoolSize) if closest == nil || len(closest) == 0 { - return nil, kb.ErrLookupFailure + return nil, nil } // setup the Query @@ -101,7 +101,7 @@ func (dht *IpfsDHT) Provide(ctx context.Context, key u.Key) error { dht.providers.AddProvider(key, dht.self) peers := dht.routingTables[0].NearestPeers(kb.ConvertKey(key), PoolSize) if len(peers) == 0 { - return kb.ErrLookupFailure + return nil } //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 routeLevel := 0 p = dht.routingTables[routeLevel].NearestPeer(kb.ConvertPeerID(id)) if p == nil { - return nil, kb.ErrLookupFailure + return nil, nil } if p.ID.Equal(id) { return p, nil @@ -287,7 +287,7 @@ func (dht *IpfsDHT) findPeerMultiple(ctx context.Context, id peer.ID) (*peer.Pee routeLevel := 0 peers := dht.routingTables[routeLevel].NearestPeers(kb.ConvertPeerID(id), AlphaValue) if len(peers) == 0 { - return nil, kb.ErrLookupFailure + return nil, nil } // setup query function