tar.go 2.45 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 8
	"github.com/ipfs/go-ipfs/core"
	"github.com/ipfs/go-ipfs/core/commands/cmdenv"
Jeromy's avatar
Jeromy committed
9
	tar "github.com/ipfs/go-ipfs/tar"
Jan Winkelmann's avatar
Jan Winkelmann committed
10

Steven Allen's avatar
Steven Allen committed
11 12
	"gx/ipfs/QmNYPETsdAu2uQ1k9q9S1jYEGURaLHV6cbYRSVFVRftpF8/go-path"
	dag "gx/ipfs/QmTQdH4848iTVCJmKXYyRiK72HufWTLYQQ8iN3JaQ8K1Hq/go-merkledag"
Hector Sanjuan's avatar
Hector Sanjuan committed
13
	"gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
14
	"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
Jeromy's avatar
Jeromy committed
15 16 17
)

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

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

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

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

46
		it := req.Files.Entries()
47 48 49
		file, err := cmdenv.GetFileArg(it)
		if err != nil {
			return err
Jeromy's avatar
Jeromy committed
50 51
		}

52
		node, err := tar.ImportTar(req.Context, file, nd.DAG)
Jeromy's avatar
Jeromy committed
53
		if err != nil {
54
			return err
Jeromy's avatar
Jeromy committed
55 56
		}

Jeromy's avatar
Jeromy committed
57
		c := node.Cid()
Jeromy's avatar
Jeromy committed
58

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

var tarCatCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
74
	Helptext: cmdkit.HelpText{
Richard Littauer's avatar
Richard Littauer committed
75
		Tagline: "Export a tar file from IPFS.",
Jeromy's avatar
Jeromy committed
76
		ShortDescription: `
Richard Littauer's avatar
Richard Littauer committed
77
'ipfs tar cat' will export a tar file from a previously imported one in IPFS.
Jeromy's avatar
Jeromy committed
78 79 80
`,
	},

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

90
		p, err := path.ParsePath(req.Arguments[0])
Jeromy's avatar
Jeromy committed
91
		if err != nil {
92
			return err
Jeromy's avatar
Jeromy committed
93 94
		}

95
		root, err := core.Resolve(req.Context, nd.Namesys, nd.Resolver, p)
Jeromy's avatar
Jeromy committed
96
		if err != nil {
97
			return err
Jeromy's avatar
Jeromy committed
98 99
		}

100 101
		rootpb, ok := root.(*dag.ProtoNode)
		if !ok {
102
			return dag.ErrNotProtobuf
103 104
		}

105
		r, err := tar.ExportTar(req.Context, rootpb, nd.DAG)
Jeromy's avatar
Jeromy committed
106
		if err != nil {
107
			return err
Jeromy's avatar
Jeromy committed
108 109
		}

110
		return res.Emit(r)
Jeromy's avatar
Jeromy committed
111 112
	},
}