-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrungo_unix.go
89 lines (75 loc) · 2.17 KB
/
rungo_unix.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
85
86
87
88
89
// +build !windows
package main
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
)
func runGo(binary, baseDir string, args []string) error {
goBinary := filepath.Join(baseDir, "go", "bin", binary)
binaryWithArgs := append([]string{goBinary}, args...)
log.Debugf("Executing %q with arguments %v", goBinary, args)
// generally won't return
return syscall.Exec(goBinary, binaryWithArgs, os.Environ())
}
func extractFile(golangArchive, baseDir string) error {
log.Infof("Extracting %q", golangArchive)
err := os.MkdirAll(baseDir, os.ModeDir|0755)
if err != nil {
return errors.Wrapf(err, "mkdir %q failed", baseDir)
}
file, err := os.Open(golangArchive)
if err != nil {
return errors.Wrapf(err, "file open %q failed", golangArchive)
}
defer file.Close()
gzipReader, err := gzip.NewReader(file)
if err != nil {
return errors.Wrap(err, "gzip reader open failed")
}
defer gzipReader.Close()
tarReader := tar.NewReader(gzipReader)
// Extract all files, based off http://blog.ralch.com/tutorial/golang-working-with-tar-and-gzip/
fileCount := 0
for {
header, err := tarReader.Next()
if err == io.EOF {
break
} else if err != nil {
return errors.Wrap(err, "tar reader next failed")
}
path := filepath.Join(baseDir, header.Name)
fileInfo := header.FileInfo()
if fileInfo.IsDir() {
err = os.MkdirAll(path, fileInfo.Mode())
if err != nil {
return errors.Wrapf(err, "mkdir %q failed", path)
}
continue
} else {
// Make directory containing the current file, if needed. Some tarballs don't include the top-level directory entry
err = os.MkdirAll(filepath.Dir(path), os.ModeDir|0755)
if err != nil {
return errors.Wrapf(err, "mkdir %q failed", path)
}
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, fileInfo.Mode())
if err != nil {
return errors.Wrapf(err, "open file %q failed", path)
}
_, err = io.Copy(file, tarReader)
if err != nil {
file.Close()
return errors.Wrapf(err, "copy for %q failed", path)
}
file.Close()
fileCount++
}
log.Debugf("Wrote %d files to %q", fileCount, baseDir)
return nil
}