-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
58 lines (45 loc) · 1.37 KB
/
info.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
package archive
import (
"errors"
"path"
"strings"
)
// ListArchive returns a new line separated string of all archive contents recursively.
func ListArchive(archiveName string) (string, error) {
if !FileExists(archiveName) {
return "", errors.New("No archive file in current directory")
}
out, err := tarGetContents(archiveName)
if err != nil {
return "", errors.New("An error occurred")
}
return out, nil
}
// ListArchiveTopLevel returns a new line separated string of archive contents without a parent directory.
func ListArchiveTopLevel(archiveName string) (string, error) {
if !FileExists(archiveName) {
return "", errors.New("No archive file in current directory")
}
out, err := tarGetContents(archiveName)
if err != nil {
errMsg := "An error occurred: " + out
return "", errors.New(errMsg)
}
allFilePaths := strings.Split(out, "\n")
topLevelFiles := filterTopLevelFiles(allFilePaths)
return strings.Join(topLevelFiles, "\n"), nil
}
func filterTopLevelFiles(allFilePaths []string) []string {
var topLevelFiles []string
for _, filePath := range allFilePaths {
if isTopLevel(filePath) {
topLevelFiles = append(topLevelFiles, filePath)
}
}
return topLevelFiles
}
func isTopLevel(filePath string) bool {
isTopLevelDir := path.Dir(filePath) == path.Base(filePath)
isTopLevelFile := path.Dir(filePath) == "."
return isTopLevelDir || isTopLevelFile
}