Commit b55a5078 authored by Jeromy's avatar Jeromy

refactor daemon code, moved commands to core/commands

parent ec40a29b
......@@ -6,6 +6,7 @@ import (
"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"
)
......@@ -50,7 +51,7 @@ func addCmd(c *commander.Command, inp []string) error {
return err
}
err := daemon.ExecuteCommand(cmd, n, os.Stdout)
err = commands.Add(n, cmd.Args, cmd.Opts, os.Stdout)
if err != nil {
fmt.Println(err)
}
......
......@@ -6,6 +6,7 @@ import (
"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"
)
......@@ -39,7 +40,7 @@ func catCmd(c *commander.Command, inp []string) error {
return err
}
err := daemon.ExecuteCommand(com, n, os.Stdout)
err = commands.Cat(n, com.Args, com.Opts, os.Stdout)
if err != nil {
fmt.Println(err)
}
......
......@@ -6,6 +6,7 @@ import (
"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"
)
......@@ -42,7 +43,7 @@ func lsCmd(c *commander.Command, inp []string) error {
return err
}
err := daemon.ExecuteCommand(com, n, os.Stdout)
err = commands.Ls(n, com.Args, com.Opts, os.Stdout)
if err != nil {
fmt.Println(err)
}
......
......@@ -2,6 +2,7 @@ package commands
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
......@@ -15,6 +16,27 @@ import (
// Error indicating the max depth has been exceded.
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
func Add(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
depth := 1
if r, ok := opts["r"].(bool); r && ok {
depth = -1
}
for _, path := range args {
nd, err := AddPath(n, path, depth)
if err != nil {
return fmt.Errorf("addFile error: %v", err)
}
k, err := nd.Key()
if err != nil {
return fmt.Errorf("addFile error: %v", err)
}
fmt.Fprintf(out, "Added node: %s = %s\n", path, k.Pretty())
}
return nil
}
func AddPath(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
if depth == 0 {
return nil, ErrDepthLimitExceeded
......
package commands
import (
"fmt"
"io"
"github.com/jbenet/go-ipfs/core"
mdag "github.com/jbenet/go-ipfs/merkledag"
)
func Cat(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
for _, fn := range args {
dagnode, err := n.Resolver.ResolvePath(fn)
if err != nil {
return fmt.Errorf("catFile error: %v", err)
}
read, err := mdag.NewDagReader(dagnode, n.DAG)
if err != nil {
return fmt.Errorf("cat error: %v", err)
}
_, err = io.Copy(out, read)
if err != nil {
return fmt.Errorf("cat error: %v", err)
}
}
return nil
}
package commands
import (
"fmt"
"io"
"github.com/jbenet/go-ipfs/core"
)
func Ls(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
for _, fn := range args {
dagnode, err := n.Resolver.ResolvePath(fn)
if err != nil {
return fmt.Errorf("ls error: %v", err)
}
for _, link := range dagnode.Links {
fmt.Fprintf(out, "%s %d %s\n", link.Hash.B58String(), link.Size, link.Name)
}
}
return nil
}
package commands
import (
"fmt"
"io"
"github.com/jbenet/go-ipfs/core"
)
func Pin(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
for _, fn := range args {
dagnode, err := n.Resolver.ResolvePath(fn)
if err != nil {
return fmt.Errorf("pin error: %v", err)
}
err = n.PinDagNode(dagnode)
if err != nil {
return fmt.Errorf("pin: %v", err)
}
}
return nil
}
......@@ -3,12 +3,10 @@ package daemon
import (
"encoding/json"
"fmt"
"io"
"net"
core "github.com/jbenet/go-ipfs/core"
commands "github.com/jbenet/go-ipfs/core/commands"
dag "github.com/jbenet/go-ipfs/merkledag"
"github.com/jbenet/go-ipfs/core/commands"
u "github.com/jbenet/go-ipfs/util"
)
......@@ -74,75 +72,20 @@ func (dl *DaemonListener) handleConnection(conn net.Conn) {
}
u.DOut("Got command: %v\n", command)
err := ExecuteCommand(&command, dl.node, conn)
if err != nil {
fmt.Fprintln(conn, "%v\n", err)
}
}
func ExecuteCommand(com *Command, ipfsnode *core.IpfsNode, out io.Writer) error {
u.DOut("executing command: %s\n", com.Command)
switch com.Command {
switch command.Command {
case "add":
depth := 1
if r, ok := com.Opts["r"].(bool); r && ok {
depth = -1
}
for _, path := range com.Args {
nd, err := commands.AddPath(ipfsnode, path, depth)
if err != nil {
return fmt.Errorf("addFile error: %v", err)
}
k, err := nd.Key()
if err != nil {
return fmt.Errorf("addFile error: %v", err)
}
fmt.Fprintf(out, "Added node: %s = %s\n", path, k.Pretty())
}
err = commands.Add(dl.node, command.Args, command.Opts, conn)
case "cat":
for _, fn := range com.Args {
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
if err != nil {
return fmt.Errorf("catFile error: %v", err)
}
read, err := dag.NewDagReader(dagnode, ipfsnode.DAG)
if err != nil {
return fmt.Errorf("cat error: %v", err)
}
_, err = io.Copy(out, read)
if err != nil {
return fmt.Errorf("cat error: %v", err)
}
}
err = commands.Cat(dl.node, command.Args, command.Opts, conn)
case "ls":
for _, fn := range com.Args {
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
if err != nil {
return fmt.Errorf("ls error: %v", err)
}
for _, link := range dagnode.Links {
fmt.Fprintf(out, "%s %d %s\n", link.Hash.B58String(), link.Size, link.Name)
}
}
err = commands.Ls(dl.node, command.Args, command.Opts, conn)
case "pin":
for _, fn := range com.Args {
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
if err != nil {
return fmt.Errorf("pin error: %v", err)
err = commands.Pin(dl.node, command.Args, command.Opts, conn)
default:
err = fmt.Errorf("Invalid Command: '%s'", command.Command)
}
err = ipfsnode.PinDagNode(dagnode)
if err != nil {
return fmt.Errorf("pin: %v", err)
}
}
default:
return fmt.Errord("Invalid Command: '%s'", com.Command)
fmt.Fprintln(conn, err)
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment