forked from Azure/draft-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
95 lines (83 loc) · 2.05 KB
/
load.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
package pack
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"k8s.io/helm/pkg/chartutil"
)
// FromDir takes a string name, tries to resolve it to a file or directory, and then loads it.
//
// This is the preferred way to load a pack. It will discover the pack encoding
// and hand off to the appropriate pack reader.
func FromDir(dir string) (*Pack, error) {
pack := new(Pack)
pack.Files = make(map[string]io.ReadCloser)
topdir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
pack.Chart, err = chartutil.LoadDir(filepath.Join(topdir, ChartsDir))
if err != nil {
return nil, err
}
files, err := ioutil.ReadDir(topdir)
if err != nil {
return nil, fmt.Errorf("error reading %s: %s", topdir, err)
}
// load all files in pack directory
for _, fInfo := range files {
if !fInfo.IsDir() {
f, err := os.Open(filepath.Join(topdir, fInfo.Name()))
if err != nil {
return nil, err
}
if fInfo.Name() != "README.md" {
pack.Files[fInfo.Name()] = f
}
} else {
if fInfo.Name() != "charts" {
packFiles, err := extractFiles(filepath.Join(topdir, fInfo.Name()), "")
if err != nil {
return nil, err
}
for k, v := range packFiles {
pack.Files[k] = v
}
}
}
}
return pack, nil
}
func extractFiles(dir, base string) (map[string]io.ReadCloser, error) {
baseDir := filepath.Join(base, filepath.Base(dir))
packFiles := make(map[string]io.ReadCloser)
absDir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
files, err := ioutil.ReadDir(absDir)
if err != nil {
return packFiles, fmt.Errorf("error reading %s: %s", dir, err)
}
for _, fInfo := range files {
if !fInfo.IsDir() {
fPath := filepath.Join(dir, fInfo.Name())
f, err := os.Open(fPath)
if err != nil {
return nil, err
}
packFiles[filepath.Join(baseDir, fInfo.Name())] = f
} else {
nestedPackFiles, err := extractFiles(filepath.Join(dir, fInfo.Name()), baseDir)
if err != nil {
return nil, err
}
for k, v := range nestedPackFiles {
packFiles[k] = v
}
}
}
return packFiles, nil
}