-
Notifications
You must be signed in to change notification settings - Fork 221
/
archive.go
54 lines (46 loc) · 1.45 KB
/
archive.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
package project
import (
"archive/zip"
"fmt"
"github.com/jfrog/jfrog-cli-go/jfrog-client/utils/io/fileutils"
"github.com/jfrog/jfrog-cli-go/jfrog-client/utils/log"
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
// Archive project files according to the go project standard
func archiveProject(writer io.Writer, sourcePath, module, version string, excludePathsRegExp *regexp.Regexp) error {
zipWriter := zip.NewWriter(writer)
defer zipWriter.Close()
return filepath.Walk(sourcePath, func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil || info.IsDir() || fileutils.IsPathSymlink(path) {
return err
}
if excludePathsRegExp.FindString(path) != "" {
log.Debug(fmt.Sprintf("Excluding path '%s' from zip archive.", path))
return nil
}
fileName := getFileName(sourcePath, path, module, version)
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
zipFile, err := zipWriter.Create(fileName)
if err != nil {
return err
}
_, err = io.CopyN(zipFile, file, info.Size())
return err
})
}
// getFileName composes filename for zip to match standard specified as
// module@version/{filename}
func getFileName(sourcePath, filePath, moduleName, version string) string {
filename := strings.TrimPrefix(filePath, sourcePath)
filename = strings.TrimLeft(filename, string(os.PathSeparator))
moduleID := fmt.Sprintf("%s@%s", moduleName, version)
return filepath.Join(moduleID, filename)
}