This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
unpack.go
84 lines (69 loc) · 1.66 KB
/
unpack.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"path"
"regexp"
"github.com/go2c/optparse"
"github.com/hacdias/fileutils"
"github.com/onodera-punpun/prt/ports"
"github.com/onodera-punpun/prt/tar"
)
// TODO: Use https://github.com/mholt/archiver.
// unpackCommand unpacks port sources
func unpackCommand(input []string) error {
// Define valid arguments.
o := optparse.New()
argh := o.Bool("help", 'h', false)
// Parse arguments.
_, err := o.Parse(input)
if err != nil {
return fmt.Errorf("invaild argument, use `-h` for a list of arguments")
}
// Print help.
if *argh {
fmt.Println("Usage: prt unpack [arguments]")
fmt.Println("")
fmt.Println("arguments:")
fmt.Println(" -h, --help print help and exit")
return nil
}
if err := downloadCommand([]string{}); err != nil {
return err
}
p := ports.New(".")
if err := p.Pkgfile.Parse(true); err != nil {
return err
}
// Print a new line if files have been downloaded.
// TODO: Can I simplify this?
var sl = []string{}
r := regexp.MustCompile("^(http|https|ftp|file)://")
for _, s := range p.Pkgfile.Source {
if r.MatchString(s) {
sl = append(sl, s)
}
}
if len(sl) > 0 {
fmt.Println()
}
if err := p.CreateWrk(); err != nil {
return err
}
to := path.Join(config.WrkDir, p.Pkgfile.Name, "src")
for i, s := range p.Pkgfile.Source {
f := path.Base(s)
fmt.Printf("Unpacking source %d/%d, %s.\n", i+1, len(p.Pkgfile.Source),
light(f))
if tar.IsArchive(f) {
if err := tar.Unpack(path.Join(config.SrcDir, f), to); err != nil {
return err
}
} else {
if err := fileutils.CopyFile(path.Join(p.Location.Full(), f),
to); err != nil {
return err
}
}
}
return nil
}