change ExecuteCommand to return an error

This commit is contained in:
Jeromy 2014-09-12 19:11:54 +00:00
parent 80dc4b3531
commit ec40a29b3d
4 changed files with 34 additions and 21 deletions

View File

@ -50,7 +50,10 @@ func addCmd(c *commander.Command, inp []string) error {
return err
}
daemon.ExecuteCommand(cmd, n, os.Stdout)
err := daemon.ExecuteCommand(cmd, n, os.Stdout)
if err != nil {
fmt.Println(err)
}
}
return nil
}

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"os"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
@ -38,7 +39,10 @@ func catCmd(c *commander.Command, inp []string) error {
return err
}
daemon.ExecuteCommand(com, n, os.Stdout)
err := daemon.ExecuteCommand(com, n, os.Stdout)
if err != nil {
fmt.Println(err)
}
}
return nil
}

View File

@ -42,7 +42,10 @@ func lsCmd(c *commander.Command, inp []string) error {
return err
}
daemon.ExecuteCommand(com, n, os.Stdout)
err := daemon.ExecuteCommand(com, n, os.Stdout)
if err != nil {
fmt.Println(err)
}
}
return nil

View File

@ -74,10 +74,13 @@ func (dl *DaemonListener) handleConnection(conn net.Conn) {
}
u.DOut("Got command: %v\n", command)
ExecuteCommand(&command, dl.node, conn)
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) {
func ExecuteCommand(com *Command, ipfsnode *core.IpfsNode, out io.Writer) error {
u.DOut("executing command: %s\n", com.Command)
switch com.Command {
case "add":
@ -86,38 +89,40 @@ func ExecuteCommand(com *Command, ipfsnode *core.IpfsNode, out io.Writer) {
depth = -1
}
for _, path := range com.Args {
_, err := commands.AddPath(ipfsnode, path, depth)
nd, err := commands.AddPath(ipfsnode, path, depth)
if err != nil {
fmt.Fprintf(out, "addFile error: %v\n", err)
continue
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())
}
case "cat":
for _, fn := range com.Args {
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
if err != nil {
fmt.Fprintf(out, "catFile error: %v\n", err)
return
return fmt.Errorf("catFile error: %v", err)
}
read, err := dag.NewDagReader(dagnode, ipfsnode.DAG)
if err != nil {
fmt.Fprintln(out, err)
continue
return fmt.Errorf("cat error: %v", err)
}
_, err = io.Copy(out, read)
if err != nil {
fmt.Fprintln(out, err)
continue
return fmt.Errorf("cat error: %v", err)
}
}
case "ls":
for _, fn := range com.Args {
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
if err != nil {
fmt.Fprintf(out, "ls error: %v\n", err)
return
return fmt.Errorf("ls error: %v", err)
}
for _, link := range dagnode.Links {
@ -128,18 +133,16 @@ func ExecuteCommand(com *Command, ipfsnode *core.IpfsNode, out io.Writer) {
for _, fn := range com.Args {
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
if err != nil {
fmt.Fprintf(out, "pin error: %v\n", err)
return
return fmt.Errorf("pin error: %v", err)
}
err = ipfsnode.PinDagNode(dagnode)
if err != nil {
fmt.Fprintf(out, "pin: %v\n", err)
return
return fmt.Errorf("pin: %v", err)
}
}
default:
fmt.Fprintf(out, "Invalid Command: '%s'\n", com.Command)
return fmt.Errord("Invalid Command: '%s'", com.Command)
}
}