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

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

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

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

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

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

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

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

Steven Allen's avatar
Steven Allen committed
55
		node, err := tar.ImportTar(req.Context, file, api.Dag())
Jeromy's avatar
Jeromy committed
56
		if err != nil {
57
			return err
Jeromy's avatar
Jeromy committed
58 59
		}

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

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

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

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

Steven Allen's avatar
Steven Allen committed
93
		root, err := api.ResolveNode(req.Context, path.New(req.Arguments[0]))
Jeromy's avatar
Jeromy committed
94
		if err != nil {
95
			return err
Jeromy's avatar
Jeromy committed
96 97
		}

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

Steven Allen's avatar
Steven Allen committed
103
		r, err := tar.ExportTar(req.Context, rootpb, api.Dag())
Jeromy's avatar
Jeromy committed
104
		if err != nil {
105
			return err
Jeromy's avatar
Jeromy committed
106 107
		}

108
		return res.Emit(r)
Jeromy's avatar
Jeromy committed
109 110
	},
}