-
Notifications
You must be signed in to change notification settings - Fork 5
/
extractor.go
72 lines (60 loc) · 1.33 KB
/
extractor.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
package extractor
import (
"fmt"
"os"
"path"
"path/filepath"
cp "github.com/otiai10/copy"
ldd "github.com/u-root/u-root/pkg/ldd"
)
type config struct {
files []string
outDir string
}
// WithFiles sets the input files of the packer
func WithFiles(s ...string) option {
return func(k *config) error {
k.files = s
return nil
}
}
// WithOutputDir sets the output dir of the packer
func WithOutputDir(s string) option {
return func(k *config) error {
k.outDir = s
return nil
}
}
// Option is an extractor option
type option func(k *config) error
// Extract a binary and its deps into a folder
func Extract(o ...option) error {
config := &config{}
for _, oo := range o {
if err := oo(config); err != nil {
return err
}
}
files, err := ldd.Ldd(config.files)
if err != nil {
return err
}
// copy ldd libs
for _, f := range files {
fmt.Println("Found", f.Name(), f.FullName)
p := path.Dir(f.FullName)
os.MkdirAll(filepath.Join(config.outDir, p), os.ModePerm)
if err := cp.Copy(f.FullName, filepath.Join(config.outDir, p, f.Name())); err != nil {
return err
}
}
// copy files
for _, f := range config.files {
p := path.Dir(f)
os.MkdirAll(filepath.Join(config.outDir, p), os.ModePerm)
if err := cp.Copy(f, filepath.Join(config.outDir, p, filepath.Base(f))); err != nil {
return err
}
}
return nil
}