-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
96 lines (78 loc) · 1.73 KB
/
reader.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
90
91
92
93
94
95
96
package oci
import (
"archive/tar"
"compress/gzip"
"context"
"io"
"os"
"path/filepath"
"strings"
"github.com/rs/zerolog/log"
)
type bundleFileReader struct {
}
func (r *bundleFileReader) ListDirs(_ context.Context, localfilepath string) ([]string, error) {
file, err := os.Open(localfilepath)
if err != nil {
log.Info().Msgf("Error opening file %s", localfilepath)
return nil, err
}
defer file.Close()
gzr, err := gzip.NewReader(file)
if err != nil {
log.Info().Msgf("Error creating gzip reader for file %s", localfilepath)
return nil, err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
modulesMap := make(map[string]bool)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
break
}
if header == nil {
continue
}
switch header.Typeflag {
case tar.TypeReg:
// /abc/def/ghi.rego => /abc/def
dir := filepath.Dir(header.Name)
modulesMap[dir] = true
}
}
modules := make([]string, 0)
for module := range modulesMap {
modules = append(modules, module)
}
return modules, nil
}
func (r *bundleFileReader) FilterModules(_ context.Context, dirs []string) []string {
modules := make([]string, 0)
for _, dir := range dirs {
if strings.Count(dir, string(os.PathSeparator)) != 2 {
// skip files in the root directory and files in depth > 2
continue
}
nodes := strings.Split(dir, string(os.PathSeparator))
hidden := false
for _, node := range nodes {
if strings.HasPrefix(node, "_") {
hidden = true
break
}
}
if hidden {
// dirs that start with _ are used for hosting library functions and utility functions
continue
}
modules = append(modules, dir)
}
return modules
}
func NewReader() Reader {
return &bundleFileReader{}
}