|
| 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 | + "gopkg.in/yaml.v3" |
| 13 | +) |
| 14 | + |
| 15 | +// Near identical to https://github.com/helm/helm/blob/main/pkg/engine/files.go as to preserve the interface. |
| 16 | + |
| 17 | +type files map[string][]byte |
| 18 | + |
| 19 | +func getFiles(dir string) (files, error) { |
| 20 | + result := make(files) |
| 21 | + |
| 22 | + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + |
| 27 | + if info.IsDir() { |
| 28 | + return nil |
| 29 | + } |
| 30 | + |
| 31 | + data, err := ioutil.ReadFile(path) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + result[path] = data |
| 37 | + |
| 38 | + return nil |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + return map[string][]byte{}, err |
| 42 | + } |
| 43 | + |
| 44 | + return result, nil |
| 45 | +} |
| 46 | + |
| 47 | +func (f files) GetBytes(name string) []byte { |
| 48 | + if v, ok := f[name]; ok { |
| 49 | + return v |
| 50 | + } |
| 51 | + return []byte{} |
| 52 | +} |
| 53 | + |
| 54 | +func (f files) Get(name string) string { |
| 55 | + return string(f.GetBytes(name)) |
| 56 | +} |
| 57 | + |
| 58 | +func (f files) Glob(pattern string) files { |
| 59 | + g, err := glob.Compile(pattern, '/') |
| 60 | + if err != nil { |
| 61 | + g, _ = glob.Compile("**") |
| 62 | + } |
| 63 | + |
| 64 | + result := make(files) |
| 65 | + for name, contents := range f { |
| 66 | + if g.Match(name) { |
| 67 | + result[name] = contents |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return result |
| 72 | +} |
| 73 | + |
| 74 | +func (f files) AsConfig() string { |
| 75 | + if f == nil { |
| 76 | + return "" |
| 77 | + } |
| 78 | + |
| 79 | + m := make(map[string]string) |
| 80 | + |
| 81 | + // Explicitly convert to strings, and file names |
| 82 | + for k, v := range f { |
| 83 | + m[path.Base(k)] = string(v) |
| 84 | + } |
| 85 | + |
| 86 | + return toYAML(m) |
| 87 | +} |
| 88 | + |
| 89 | +func (f files) AsSecrets() string { |
| 90 | + if f == nil { |
| 91 | + return "" |
| 92 | + } |
| 93 | + |
| 94 | + m := make(map[string]string) |
| 95 | + |
| 96 | + for k, v := range f { |
| 97 | + m[path.Base(k)] = base64.StdEncoding.EncodeToString(v) |
| 98 | + } |
| 99 | + |
| 100 | + return toYAML(m) |
| 101 | +} |
| 102 | + |
| 103 | +func (f files) Lines(path string) []string { |
| 104 | + if f == nil || f[path] == nil { |
| 105 | + return []string{} |
| 106 | + } |
| 107 | + |
| 108 | + return strings.Split(string(f[path]), "\n") |
| 109 | +} |
| 110 | + |
| 111 | +func toYAML(v interface{}) string { |
| 112 | + data, err := yaml.Marshal(v) |
| 113 | + if err != nil { |
| 114 | + // Swallow errors inside a template. |
| 115 | + return "" |
| 116 | + } |
| 117 | + return strings.TrimSuffix(string(data), "\n") |
| 118 | +} |
0 commit comments