tar.go 2.53 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

Jakub Sztandera's avatar
Jakub Sztandera committed
11 12 13
	"gx/ipfs/QmUkb7H2WRutVR91pzoHZPwhbAMFgZMiuGZ2CZ3StuWsJ1/go-ipfs-cmds"
	"gx/ipfs/QmXgYy6EgbKrpXFtfdHumWsEMd1HrsqGXtovrfBnMgbpfy/go-path"
	dag "gx/ipfs/Qmccmovpo9isKeaaDzcxvT7mVJN1uKwn2xzSs1y8hb6PEs/go-merkledag"
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 47 48 49 50
		enc, err := cmdenv.GetCidEncoder(req)
		if err != nil {
			return err
		}

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

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

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

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

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

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

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

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

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

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

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