This repository was archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
76 lines (62 loc) 路 1.93 KB
/
Copy pathutil.go
File metadata and controls
76 lines (62 loc) 路 1.93 KB
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
package build
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// getTrueDirectory converts given filepath to host-based filepath if applicable
// - Docker commands are sent to the mounted Docker socket and hence are
// executed on the host, using the host's filepaths, which means Docker client
// commands must use this function when dealing with paths
func getTrueDirectory(path string) string {
return strings.Replace(path, "/app/host", os.Getenv("HOME"), 1)
}
// buildTar takes a source and variable writers and walks 'source' writing each file
// found to the tar writer; the purpose for accepting multiple writers is to allow
// for multiple outputs (for example a file, or md5 hash)
// Sourced from https://gist.github.com/sdomino/e6bc0c98f87843bc26bb#file-targz-go
func buildTar(dir string, outputs ...io.Writer) error {
// ensure the src actually exists before trying to tar it
if _, err := os.Stat(dir); err != nil {
return fmt.Errorf("Unable to tar files - %v", err.Error())
}
mw := io.MultiWriter(outputs...)
gzw := gzip.NewWriter(mw)
defer gzw.Close()
tw := tar.NewWriter(gzw)
defer tw.Close()
return filepath.Walk(dir, func(file string, fi os.FileInfo, err error) error {
// return on any error
if err != nil {
return err
}
// create a new dir/file header
header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}
// update the name to correctly reflect the desired destination when untaring
header.Name = strings.TrimPrefix(strings.Replace(file, dir, "", -1), string(filepath.Separator))
// write the header
if err := tw.WriteHeader(header); err != nil {
return err
}
// return on non-regular files
if !fi.Mode().IsRegular() {
return nil
}
// open files for taring
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
// copy file data into tar writer
_, err = io.Copy(tw, f)
return err
})
}