tar.go 2.39 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5 6 7 8
package commands

import (
	"io"
	"strings"

	cmds "github.com/ipfs/go-ipfs/commands"
	core "github.com/ipfs/go-ipfs/core"
9
	"github.com/ipfs/go-ipfs/core/coreunix"
Jeromy's avatar
Jeromy committed
10 11 12 13 14 15
	path "github.com/ipfs/go-ipfs/path"
	tar "github.com/ipfs/go-ipfs/tar"
)

var TarCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
16
		Tagline: "Utility functions for tar files in ipfs.",
Jeromy's avatar
Jeromy committed
17 18 19 20 21 22 23 24 25 26
	},

	Subcommands: map[string]*cmds.Command{
		"add": tarAddCmd,
		"cat": tarCatCmd,
	},
}

var tarAddCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
27
		Tagline: "Import a tar file into ipfs.",
Jeromy's avatar
Jeromy committed
28 29 30 31 32 33
		ShortDescription: `
'ipfs tar add' will parse a tar file and create a merkledag structure to represent it.
`,
	},

	Arguments: []cmds.Argument{
34
		cmds.FileArg("file", true, false, "Tar file to add.").EnableStdin(),
Jeromy's avatar
Jeromy committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	},
	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
		}

		k, err := node.Key()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		fi.FileName()
62
		res.SetOutput(&coreunix.AddedObject{
Jeromy's avatar
Jeromy committed
63 64 65 66
			Name: fi.FileName(),
			Hash: k.B58String(),
		})
	},
67
	Type: coreunix.AddedObject{},
Jeromy's avatar
Jeromy committed
68 69
	Marshalers: cmds.MarshalerMap{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
70
			o := res.Output().(*coreunix.AddedObject)
71
			return strings.NewReader(o.Hash + "\n"), nil
Jeromy's avatar
Jeromy committed
72 73 74 75 76 77
		},
	},
}

var tarCatCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
78
		Tagline: "Export a tar file from ipfs.",
Jeromy's avatar
Jeromy committed
79 80 81 82 83 84
		ShortDescription: `
'ipfs tar cat' will export a tar file from a previously imported one in ipfs
`,
	},

	Arguments: []cmds.Argument{
85
		cmds.StringArg("path", true, false, "Ipfs path of archive to export.").EnableStdin(),
Jeromy's avatar
Jeromy committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	},
	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
		}

		r, err := tar.ExportTar(req.Context(), root, nd.DAG)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		res.SetOutput(r)
	},
}