tar.go 2.54 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"
9
	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
Jeromy's avatar
Jeromy committed
10
	tar "github.com/ipfs/go-ipfs/tar"
Jan Winkelmann's avatar
Jan Winkelmann committed
11

Steven Allen's avatar
Steven Allen committed
12
	"gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path"
13
	"gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
Steven Allen's avatar
Steven Allen committed
14
	dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
15
	"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
Jeromy's avatar
Jeromy committed
16 17 18
)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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