-
Notifications
You must be signed in to change notification settings - Fork 54
/
untar.go
41 lines (33 loc) 路 1.01 KB
/
untar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package untar
import (
"github.com/itchio/butler/comm"
"github.com/itchio/butler/mansion"
"github.com/itchio/wharf/archiver"
"github.com/pkg/errors"
)
var args = struct {
file *string
dir *string
}{}
func Register(ctx *mansion.Context) {
cmd := ctx.App.Command("untar", "Extract a .tar file").Hidden()
args.file = cmd.Arg("file", "Path of the .tar archive to extract").Required().String()
args.dir = cmd.Flag("dir", "An optional directory to which to extract files (defaults to CWD)").Default(".").Short('d').String()
ctx.Register(cmd, do)
}
func do(ctx *mansion.Context) {
ctx.Must(Do(ctx, *args.file, *args.dir))
}
func Do(ctx *mansion.Context, file string, dir string) error {
settings := archiver.ExtractSettings{
Consumer: comm.NewStateConsumer(),
}
comm.StartProgress()
res, err := archiver.ExtractTar(file, dir, settings)
comm.EndProgress()
if err != nil {
return errors.WithStack(err)
}
comm.Logf("Extracted %d dirs, %d files, %d symlinks", res.Dirs, res.Files, res.Symlinks)
return nil
}