tar.go 2.35 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3
package commands

import (
4
	"fmt"
Jeromy's avatar
Jeromy committed
5 6
	"io"

7
	"github.com/ipfs/go-ipfs/core/commands/cmdenv"
Łukasz Magiera's avatar
Łukasz Magiera committed
8
	"github.com/ipfs/go-ipfs/namesys/resolve"
Jeromy's avatar
Jeromy committed
9
	tar "github.com/ipfs/go-ipfs/tar"
Jan Winkelmann's avatar
Jan Winkelmann committed
10

Jakub Sztandera's avatar
Jakub Sztandera committed
11 12 13
	"github.com/ipfs/go-ipfs-cmds"
	dag "github.com/ipfs/go-merkledag"
	"github.com/ipfs/go-path"
Jeromy's avatar
Jeromy committed
14 15 16
)

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

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

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

Steven Allen's avatar
Steven Allen committed
36 37
	Arguments: []cmds.Argument{
		cmds.FileArg("file", true, false, "Tar file to add.").EnableStdin(),
Jeromy's avatar
Jeromy committed
38
	},
39 40
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		nd, err := cmdenv.GetNode(env)
Jeromy's avatar
Jeromy committed
41
		if err != nil {
42
			return err
Jeromy's avatar
Jeromy committed
43 44
		}

45 46 47 48 49
		enc, err := cmdenv.GetCidEncoder(req)
		if err != nil {
			return err
		}

50
		it := req.Files.Entries()
51 52 53
		file, err := cmdenv.GetFileArg(it)
		if err != nil {
			return err
Jeromy's avatar
Jeromy committed
54 55
		}

56
		node, err := tar.ImportTar(req.Context, file, nd.DAG)
Jeromy's avatar
Jeromy committed
57
		if err != nil {
58
			return err
Jeromy's avatar
Jeromy committed
59 60
		}

Jeromy's avatar
Jeromy committed
61
		c := node.Cid()
Jeromy's avatar
Jeromy committed
62

63
		return cmds.EmitOnce(res, &AddEvent{
64
			Name: it.Name(),
65
			Hash: enc.Encode(c),
Jeromy's avatar
Jeromy committed
66 67
		})
	},
68
	Type: AddEvent{},
69
	Encoders: cmds.EncoderMap{
70
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *AddEvent) error {
71 72 73
			fmt.Fprintln(w, out.Hash)
			return nil
		}),
Jeromy's avatar
Jeromy committed
74 75 76 77
	},
}

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

Steven Allen's avatar
Steven Allen committed
85 86
	Arguments: []cmds.Argument{
		cmds.StringArg("path", true, false, "ipfs path of archive to export.").EnableStdin(),
Jeromy's avatar
Jeromy committed
87
	},
88 89
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		nd, err := cmdenv.GetNode(env)
Jeromy's avatar
Jeromy committed
90
		if err != nil {
91
			return err
Jeromy's avatar
Jeromy committed
92 93
		}

94
		p, err := path.ParsePath(req.Arguments[0])
Jeromy's avatar
Jeromy committed
95
		if err != nil {
96
			return err
Jeromy's avatar
Jeromy committed
97 98
		}

Łukasz Magiera's avatar
Łukasz Magiera committed
99
		root, err := resolve.Resolve(req.Context, nd.Namesys, nd.Resolver, p)
Jeromy's avatar
Jeromy committed
100
		if err != nil {
101
			return err
Jeromy's avatar
Jeromy committed
102 103
		}

104 105
		rootpb, ok := root.(*dag.ProtoNode)
		if !ok {
106
			return dag.ErrNotProtobuf
107 108
		}

109
		r, err := tar.ExportTar(req.Context, rootpb, nd.DAG)
Jeromy's avatar
Jeromy committed
110
		if err != nil {
111
			return err
Jeromy's avatar
Jeromy committed
112 113
		}

114
		return res.Emit(r)
Jeromy's avatar
Jeromy committed
115 116
	},
}