Skip to content

Commit b55a507

Browse files
committed
refactor daemon code, moved commands to core/commands
1 parent ec40a29 commit b55a507

File tree

8 files changed

+112
-70
lines changed

8 files changed

+112
-70
lines changed

cmd/ipfs/add.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
88
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
9+
"github.com/jbenet/go-ipfs/core/commands"
910
"github.com/jbenet/go-ipfs/daemon"
1011
u "github.com/jbenet/go-ipfs/util"
1112
)
@@ -50,7 +51,7 @@ func addCmd(c *commander.Command, inp []string) error {
5051
return err
5152
}
5253

53-
err := daemon.ExecuteCommand(cmd, n, os.Stdout)
54+
err = commands.Add(n, cmd.Args, cmd.Opts, os.Stdout)
5455
if err != nil {
5556
fmt.Println(err)
5657
}

cmd/ipfs/cat.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
88
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
9+
"github.com/jbenet/go-ipfs/core/commands"
910
"github.com/jbenet/go-ipfs/daemon"
1011
u "github.com/jbenet/go-ipfs/util"
1112
)
@@ -39,7 +40,7 @@ func catCmd(c *commander.Command, inp []string) error {
3940
return err
4041
}
4142

42-
err := daemon.ExecuteCommand(com, n, os.Stdout)
43+
err = commands.Cat(n, com.Args, com.Opts, os.Stdout)
4344
if err != nil {
4445
fmt.Println(err)
4546
}

cmd/ipfs/ls.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
88
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
9+
"github.com/jbenet/go-ipfs/core/commands"
910
"github.com/jbenet/go-ipfs/daemon"
1011
u "github.com/jbenet/go-ipfs/util"
1112
)
@@ -42,7 +43,7 @@ func lsCmd(c *commander.Command, inp []string) error {
4243
return err
4344
}
4445

45-
err := daemon.ExecuteCommand(com, n, os.Stdout)
46+
err = commands.Ls(n, com.Args, com.Opts, os.Stdout)
4647
if err != nil {
4748
fmt.Println(err)
4849
}

core/commands/add.go

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"fmt"
5+
"io"
56
"io/ioutil"
67
"os"
78
"path/filepath"
@@ -15,6 +16,27 @@ import (
1516
// Error indicating the max depth has been exceded.
1617
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")
1718

19+
func Add(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
20+
depth := 1
21+
if r, ok := opts["r"].(bool); r && ok {
22+
depth = -1
23+
}
24+
for _, path := range args {
25+
nd, err := AddPath(n, path, depth)
26+
if err != nil {
27+
return fmt.Errorf("addFile error: %v", err)
28+
}
29+
30+
k, err := nd.Key()
31+
if err != nil {
32+
return fmt.Errorf("addFile error: %v", err)
33+
}
34+
35+
fmt.Fprintf(out, "Added node: %s = %s\n", path, k.Pretty())
36+
}
37+
return nil
38+
}
39+
1840
func AddPath(n *core.IpfsNode, fpath string, depth int) (*dag.Node, error) {
1941
if depth == 0 {
2042
return nil, ErrDepthLimitExceeded

core/commands/cat.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/jbenet/go-ipfs/core"
8+
mdag "github.com/jbenet/go-ipfs/merkledag"
9+
)
10+
11+
func Cat(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
12+
for _, fn := range args {
13+
dagnode, err := n.Resolver.ResolvePath(fn)
14+
if err != nil {
15+
return fmt.Errorf("catFile error: %v", err)
16+
}
17+
18+
read, err := mdag.NewDagReader(dagnode, n.DAG)
19+
if err != nil {
20+
return fmt.Errorf("cat error: %v", err)
21+
}
22+
23+
_, err = io.Copy(out, read)
24+
if err != nil {
25+
return fmt.Errorf("cat error: %v", err)
26+
}
27+
}
28+
return nil
29+
}

core/commands/ls.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/jbenet/go-ipfs/core"
8+
)
9+
10+
func Ls(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
11+
for _, fn := range args {
12+
dagnode, err := n.Resolver.ResolvePath(fn)
13+
if err != nil {
14+
return fmt.Errorf("ls error: %v", err)
15+
}
16+
17+
for _, link := range dagnode.Links {
18+
fmt.Fprintf(out, "%s %d %s\n", link.Hash.B58String(), link.Size, link.Name)
19+
}
20+
}
21+
return nil
22+
}

core/commands/pin.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/jbenet/go-ipfs/core"
8+
)
9+
10+
func Pin(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
11+
for _, fn := range args {
12+
dagnode, err := n.Resolver.ResolvePath(fn)
13+
if err != nil {
14+
return fmt.Errorf("pin error: %v", err)
15+
}
16+
17+
err = n.PinDagNode(dagnode)
18+
if err != nil {
19+
return fmt.Errorf("pin: %v", err)
20+
}
21+
}
22+
return nil
23+
}

daemon/daemon.go

+10-67
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ package daemon
33
import (
44
"encoding/json"
55
"fmt"
6-
"io"
76
"net"
87

98
core "github.com/jbenet/go-ipfs/core"
10-
commands "github.com/jbenet/go-ipfs/core/commands"
11-
dag "github.com/jbenet/go-ipfs/merkledag"
9+
"github.com/jbenet/go-ipfs/core/commands"
1210
u "github.com/jbenet/go-ipfs/util"
1311
)
1412

@@ -74,75 +72,20 @@ func (dl *DaemonListener) handleConnection(conn net.Conn) {
7472
}
7573

7674
u.DOut("Got command: %v\n", command)
77-
err := ExecuteCommand(&command, dl.node, conn)
78-
if err != nil {
79-
fmt.Fprintln(conn, "%v\n", err)
80-
}
81-
}
82-
83-
func ExecuteCommand(com *Command, ipfsnode *core.IpfsNode, out io.Writer) error {
84-
u.DOut("executing command: %s\n", com.Command)
85-
switch com.Command {
75+
switch command.Command {
8676
case "add":
87-
depth := 1
88-
if r, ok := com.Opts["r"].(bool); r && ok {
89-
depth = -1
90-
}
91-
for _, path := range com.Args {
92-
nd, err := commands.AddPath(ipfsnode, path, depth)
93-
if err != nil {
94-
return fmt.Errorf("addFile error: %v", err)
95-
}
96-
97-
k, err := nd.Key()
98-
if err != nil {
99-
return fmt.Errorf("addFile error: %v", err)
100-
}
101-
102-
fmt.Fprintf(out, "Added node: %s = %s\n", path, k.Pretty())
103-
}
77+
err = commands.Add(dl.node, command.Args, command.Opts, conn)
10478
case "cat":
105-
for _, fn := range com.Args {
106-
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
107-
if err != nil {
108-
return fmt.Errorf("catFile error: %v", err)
109-
}
110-
111-
read, err := dag.NewDagReader(dagnode, ipfsnode.DAG)
112-
if err != nil {
113-
return fmt.Errorf("cat error: %v", err)
114-
}
115-
116-
_, err = io.Copy(out, read)
117-
if err != nil {
118-
return fmt.Errorf("cat error: %v", err)
119-
}
120-
}
79+
err = commands.Cat(dl.node, command.Args, command.Opts, conn)
12180
case "ls":
122-
for _, fn := range com.Args {
123-
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
124-
if err != nil {
125-
return fmt.Errorf("ls error: %v", err)
126-
}
127-
128-
for _, link := range dagnode.Links {
129-
fmt.Fprintf(out, "%s %d %s\n", link.Hash.B58String(), link.Size, link.Name)
130-
}
131-
}
81+
err = commands.Ls(dl.node, command.Args, command.Opts, conn)
13282
case "pin":
133-
for _, fn := range com.Args {
134-
dagnode, err := ipfsnode.Resolver.ResolvePath(fn)
135-
if err != nil {
136-
return fmt.Errorf("pin error: %v", err)
137-
}
138-
139-
err = ipfsnode.PinDagNode(dagnode)
140-
if err != nil {
141-
return fmt.Errorf("pin: %v", err)
142-
}
143-
}
83+
err = commands.Pin(dl.node, command.Args, command.Opts, conn)
14484
default:
145-
return fmt.Errord("Invalid Command: '%s'", com.Command)
85+
err = fmt.Errorf("Invalid Command: '%s'", command.Command)
86+
}
87+
if err != nil {
88+
fmt.Fprintln(conn, err)
14689
}
14790
}
14891

0 commit comments

Comments
 (0)