mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 19:44:01 +08:00

Also change existing 'Node' type to 'ProtoNode' and use that most everywhere for now. As we move forward with the integration we will try and use the Node interface in more places that we're currently using ProtoNode. License: MIT Signed-off-by: Jeromy <why@ipfs.io>
119 lines
2.5 KiB
Go
119 lines
2.5 KiB
Go
package commands
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
|
|
cmds "github.com/ipfs/go-ipfs/commands"
|
|
core "github.com/ipfs/go-ipfs/core"
|
|
"github.com/ipfs/go-ipfs/core/coreunix"
|
|
dag "github.com/ipfs/go-ipfs/merkledag"
|
|
path "github.com/ipfs/go-ipfs/path"
|
|
tar "github.com/ipfs/go-ipfs/tar"
|
|
)
|
|
|
|
var TarCmd = &cmds.Command{
|
|
Helptext: cmds.HelpText{
|
|
Tagline: "Interact with tar files in IPFS.",
|
|
},
|
|
|
|
Subcommands: map[string]*cmds.Command{
|
|
"add": tarAddCmd,
|
|
"cat": tarCatCmd,
|
|
},
|
|
}
|
|
|
|
var tarAddCmd = &cmds.Command{
|
|
Helptext: cmds.HelpText{
|
|
Tagline: "Import a tar file into ipfs.",
|
|
ShortDescription: `
|
|
'ipfs tar add' will parse a tar file and create a merkledag structure to
|
|
represent it.
|
|
`,
|
|
},
|
|
|
|
Arguments: []cmds.Argument{
|
|
cmds.FileArg("file", true, false, "Tar file to add.").EnableStdin(),
|
|
},
|
|
Run: func(req cmds.Request, res cmds.Response) {
|
|
nd, err := req.InvocContext().GetNode()
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
fi, err := req.Files().NextFile()
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
node, err := tar.ImportTar(fi, nd.DAG)
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
c := node.Cid()
|
|
|
|
fi.FileName()
|
|
res.SetOutput(&coreunix.AddedObject{
|
|
Name: fi.FileName(),
|
|
Hash: c.String(),
|
|
})
|
|
},
|
|
Type: coreunix.AddedObject{},
|
|
Marshalers: cmds.MarshalerMap{
|
|
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
|
o := res.Output().(*coreunix.AddedObject)
|
|
return strings.NewReader(o.Hash + "\n"), nil
|
|
},
|
|
},
|
|
}
|
|
|
|
var tarCatCmd = &cmds.Command{
|
|
Helptext: cmds.HelpText{
|
|
Tagline: "Export a tar file from IPFS.",
|
|
ShortDescription: `
|
|
'ipfs tar cat' will export a tar file from a previously imported one in IPFS.
|
|
`,
|
|
},
|
|
|
|
Arguments: []cmds.Argument{
|
|
cmds.StringArg("path", true, false, "IPFS path of archive to export.").EnableStdin(),
|
|
},
|
|
Run: func(req cmds.Request, res cmds.Response) {
|
|
nd, err := req.InvocContext().GetNode()
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
p, err := path.ParsePath(req.Arguments()[0])
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
root, err := core.Resolve(req.Context(), nd, p)
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
rootpb, ok := root.(*dag.ProtoNode)
|
|
if !ok {
|
|
res.SetError(dag.ErrNotProtobuf, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
r, err := tar.ExportTar(req.Context(), rootpb, nd.DAG)
|
|
if err != nil {
|
|
res.SetError(err, cmds.ErrNormal)
|
|
return
|
|
}
|
|
|
|
res.SetOutput(r)
|
|
},
|
|
}
|