@@ -3,12 +3,10 @@ package daemon
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
- "io"
7
6
"net"
8
7
9
8
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"
12
10
u "github.com/jbenet/go-ipfs/util"
13
11
)
14
12
@@ -74,75 +72,20 @@ func (dl *DaemonListener) handleConnection(conn net.Conn) {
74
72
}
75
73
76
74
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 {
86
76
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 )
104
78
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 )
121
80
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 )
132
82
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 )
144
84
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 )
146
89
}
147
90
}
148
91
0 commit comments