|
| 1 | +package document |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/gobwas/glob" |
| 12 | + log "github.com/sirupsen/logrus" |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +// Near identical to https://github.com/helm/helm/blob/main/pkg/engine/files.go as to preserve the interface. |
| 17 | + |
| 18 | +type fileEntry struct { |
| 19 | + Path string |
| 20 | + data []byte |
| 21 | +} |
| 22 | + |
| 23 | +func (f *fileEntry) GetData() []byte { |
| 24 | + if f.data == nil { |
| 25 | + data, err := ioutil.ReadFile(f.Path) |
| 26 | + if err != nil { |
| 27 | + log.Warnf("Error reading file contents for %s: %s", f.Path, err.Error()) |
| 28 | + return []byte{} |
| 29 | + } |
| 30 | + f.data = data |
| 31 | + } |
| 32 | + |
| 33 | + return f.data |
| 34 | +} |
| 35 | + |
| 36 | +type files map[string]*fileEntry |
| 37 | + |
| 38 | +func getFiles(dir string) (files, error) { |
| 39 | + result := make(files) |
| 40 | + |
| 41 | + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + if info.IsDir() { |
| 47 | + return nil |
| 48 | + } |
| 49 | + |
| 50 | + result[path] = &fileEntry{Path: path} |
| 51 | + return nil |
| 52 | + }) |
| 53 | + if err != nil { |
| 54 | + return map[string]*fileEntry{}, err |
| 55 | + } |
| 56 | + |
| 57 | + return result, nil |
| 58 | +} |
| 59 | + |
| 60 | +func (f files) GetBytes(name string) []byte { |
| 61 | + if v, ok := f[name]; ok { |
| 62 | + return v.GetData() |
| 63 | + } |
| 64 | + return []byte{} |
| 65 | +} |
| 66 | + |
| 67 | +func (f files) Get(name string) string { |
| 68 | + return string(f.GetBytes(name)) |
| 69 | +} |
| 70 | + |
| 71 | +func (f files) Glob(pattern string) files { |
| 72 | + result := make(files) |
| 73 | + g, err := glob.Compile(pattern, '/') |
| 74 | + if err != nil { |
| 75 | + log.Warnf("Error compiling Glob patten %s: %s", pattern, err.Error()) |
| 76 | + return result |
| 77 | + } |
| 78 | + |
| 79 | + for filePath, entry := range f { |
| 80 | + if g.Match(filePath) { |
| 81 | + result[filePath] = entry |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return result |
| 86 | +} |
| 87 | + |
| 88 | +func (f files) AsConfig() string { |
| 89 | + if f == nil { |
| 90 | + return "" |
| 91 | + } |
| 92 | + |
| 93 | + m := make(map[string]string) |
| 94 | + |
| 95 | + // Explicitly convert to strings, and file names |
| 96 | + for k, v := range f { |
| 97 | + m[path.Base(k)] = string(v.GetData()) |
| 98 | + } |
| 99 | + |
| 100 | + return toYAML(m) |
| 101 | +} |
| 102 | + |
| 103 | +func (f files) AsSecrets() string { |
| 104 | + if f == nil { |
| 105 | + return "" |
| 106 | + } |
| 107 | + |
| 108 | + m := make(map[string]string) |
| 109 | + |
| 110 | + for k, v := range f { |
| 111 | + m[path.Base(k)] = base64.StdEncoding.EncodeToString(v.GetData()) |
| 112 | + } |
| 113 | + |
| 114 | + return toYAML(m) |
| 115 | +} |
| 116 | + |
| 117 | +func (f files) Lines(path string) []string { |
| 118 | + if f == nil { |
| 119 | + return []string{} |
| 120 | + } |
| 121 | + entry, exists := f[path] |
| 122 | + if !exists { |
| 123 | + return []string{} |
| 124 | + } |
| 125 | + |
| 126 | + return strings.Split(string(entry.GetData()), "\n") |
| 127 | +} |
| 128 | + |
| 129 | +func toYAML(v interface{}) string { |
| 130 | + data, err := yaml.Marshal(v) |
| 131 | + if err != nil { |
| 132 | + // Swallow errors inside a template. |
| 133 | + return "" |
| 134 | + } |
| 135 | + return strings.TrimSuffix(string(data), "\n") |
| 136 | +} |
0 commit comments