forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_reader.go
62 lines (47 loc) · 1.34 KB
/
dir_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
package license
import (
gopath "path"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
. "github.com/cloudfoundry/bosh-cli/release/resource"
)
type DirReaderImpl struct {
archiveFactory ArchiveFunc
fs boshsys.FileSystem
}
func NewDirReaderImpl(archiveFactory ArchiveFunc, fs boshsys.FileSystem) DirReaderImpl {
return DirReaderImpl{archiveFactory: archiveFactory, fs: fs}
}
func (r DirReaderImpl) Read(path string) (*License, error) {
files, err := r.collectFiles(path)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Collecting license files")
}
if len(files) == 0 {
return nil, nil
}
archive := r.archiveFactory(files, nil, nil)
fp, err := archive.Fingerprint()
if err != nil {
return nil, err
}
return NewLicense(NewResource("license", fp, archive)), nil
}
func (r DirReaderImpl) collectFiles(path string) ([]File, error) {
var files []File
licenseMatches, err := r.fs.Glob(gopath.Join(path, "LICENSE*"))
if err != nil {
return nil, err
}
noticeMatches, err := r.fs.Glob(gopath.Join(path, "NOTICE*"))
if err != nil {
return nil, err
}
for _, filePath := range append(licenseMatches, noticeMatches...) {
file := NewFile(filePath, path)
file.UseBasename = true
file.ExcludeMode = true
files = append(files, file)
}
return files, nil
}