1
1
package commands
2
2
3
3
import (
4
+ "fmt"
4
5
"io"
5
- "strings"
6
6
7
- cmds "github.com/ipfs/go-ipfs/commands"
8
7
core "github.com/ipfs/go-ipfs/core"
9
- e "github.com/ipfs/go-ipfs/core/commands/e "
8
+ cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv "
10
9
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
11
10
tar "github.com/ipfs/go-ipfs/tar"
12
- path "gx/ipfs/QmRKuTyCzg7HFBcV1YUhzStroGtJSb8iWgyxfsDCwFhWTS/go-path"
13
- dag "gx/ipfs/QmY8BMUSpCwNiTmFhACmC9Bt1qT63cHP35AoQAus4x14qH/go-merkledag"
14
11
15
- "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
12
+ "gx/ipfs/QmRKuTyCzg7HFBcV1YUhzStroGtJSb8iWgyxfsDCwFhWTS/go-path"
13
+ dag "gx/ipfs/QmY8BMUSpCwNiTmFhACmC9Bt1qT63cHP35AoQAus4x14qH/go-merkledag"
14
+ cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds"
15
+ cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
16
16
)
17
17
18
18
var TarCmd = & cmds.Command {
@@ -38,47 +38,36 @@ represent it.
38
38
Arguments : []cmdkit.Argument {
39
39
cmdkit .FileArg ("file" , true , false , "Tar file to add." ).EnableStdin (),
40
40
},
41
- Run : func (req cmds.Request , res cmds.Response ) {
42
- nd , err := req . InvocContext (). GetNode ()
41
+ Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds. Environment ) error {
42
+ nd , err := cmdenv . GetNode (env )
43
43
if err != nil {
44
- res .SetError (err , cmdkit .ErrNormal )
45
- return
44
+ return err
46
45
}
47
46
48
- fi , err := req .Files () .NextFile ()
47
+ fi , err := req .Files .NextFile ()
49
48
if err != nil {
50
- res .SetError (err , cmdkit .ErrNormal )
51
- return
49
+ return err
52
50
}
53
51
54
- node , err := tar .ImportTar (req .Context () , fi , nd .DAG )
52
+ node , err := tar .ImportTar (req .Context , fi , nd .DAG )
55
53
if err != nil {
56
- res .SetError (err , cmdkit .ErrNormal )
57
- return
54
+ return err
58
55
}
59
56
60
57
c := node .Cid ()
61
58
62
59
fi .FileName ()
63
- res .SetOutput (& coreiface.AddEvent {
60
+ return res .Emit (& coreiface.AddEvent {
64
61
Name : fi .FileName (),
65
62
Hash : c .String (),
66
63
})
67
64
},
68
65
Type : coreiface.AddEvent {},
69
- Marshalers : cmds.MarshalerMap {
70
- cmds .Text : func (res cmds.Response ) (io.Reader , error ) {
71
- v , err := unwrapOutput (res .Output ())
72
- if err != nil {
73
- return nil , err
74
- }
75
-
76
- o , ok := v .(* coreiface.AddEvent )
77
- if ! ok {
78
- return nil , e .TypeErr (o , v )
79
- }
80
- return strings .NewReader (o .Hash + "\n " ), nil
81
- },
66
+ Encoders : cmds.EncoderMap {
67
+ cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out * coreiface.AddEvent ) error {
68
+ fmt .Fprintln (w , out .Hash )
69
+ return nil
70
+ }),
82
71
},
83
72
}
84
73
@@ -93,37 +82,32 @@ var tarCatCmd = &cmds.Command{
93
82
Arguments : []cmdkit.Argument {
94
83
cmdkit .StringArg ("path" , true , false , "ipfs path of archive to export." ).EnableStdin (),
95
84
},
96
- Run : func (req cmds.Request , res cmds.Response ) {
97
- nd , err := req . InvocContext (). GetNode ()
85
+ Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds. Environment ) error {
86
+ nd , err := cmdenv . GetNode (env )
98
87
if err != nil {
99
- res .SetError (err , cmdkit .ErrNormal )
100
- return
88
+ return err
101
89
}
102
90
103
- p , err := path .ParsePath (req .Arguments () [0 ])
91
+ p , err := path .ParsePath (req .Arguments [0 ])
104
92
if err != nil {
105
- res .SetError (err , cmdkit .ErrNormal )
106
- return
93
+ return err
107
94
}
108
95
109
- root , err := core .Resolve (req .Context () , nd .Namesys , nd .Resolver , p )
96
+ root , err := core .Resolve (req .Context , nd .Namesys , nd .Resolver , p )
110
97
if err != nil {
111
- res .SetError (err , cmdkit .ErrNormal )
112
- return
98
+ return err
113
99
}
114
100
115
101
rootpb , ok := root .(* dag.ProtoNode )
116
102
if ! ok {
117
- res .SetError (dag .ErrNotProtobuf , cmdkit .ErrNormal )
118
- return
103
+ return dag .ErrNotProtobuf
119
104
}
120
105
121
- r , err := tar .ExportTar (req .Context () , rootpb , nd .DAG )
106
+ r , err := tar .ExportTar (req .Context , rootpb , nd .DAG )
122
107
if err != nil {
123
- res .SetError (err , cmdkit .ErrNormal )
124
- return
108
+ return err
125
109
}
126
110
127
- res .SetOutput (r )
111
+ return res .Emit (r )
128
112
},
129
113
}
0 commit comments