1package release
2
3import (
4	"path/filepath"
5
6	boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
7	boshlog "github.com/cloudfoundry/bosh-utils/logger"
8	boshsys "github.com/cloudfoundry/bosh-utils/system"
9
10	bicrypto "github.com/cloudfoundry/bosh-cli/crypto"
11	boshjob "github.com/cloudfoundry/bosh-cli/release/job"
12	boshlic "github.com/cloudfoundry/bosh-cli/release/license"
13	boshpkg "github.com/cloudfoundry/bosh-cli/release/pkg"
14	. "github.com/cloudfoundry/bosh-cli/release/resource"
15)
16
17type Provider struct {
18	fingerprinterFactory func(bool) Fingerprinter
19
20	cmdRunner        boshsys.CmdRunner
21	compressor       boshcmd.Compressor
22	digestCalculator bicrypto.DigestCalculator
23	fs               boshsys.FileSystem
24	logger           boshlog.Logger
25}
26
27func NewProvider(
28	cmdRunner boshsys.CmdRunner,
29	compressor boshcmd.Compressor,
30	digestCalculator bicrypto.DigestCalculator,
31	fs boshsys.FileSystem,
32	logger boshlog.Logger,
33) Provider {
34	return Provider{
35		fingerprinterFactory: func(followSymlinks bool) Fingerprinter {
36			return NewFingerprinterImpl(digestCalculator, fs, followSymlinks)
37		},
38		cmdRunner:        cmdRunner,
39		compressor:       compressor,
40		digestCalculator: digestCalculator,
41		fs:               fs,
42		logger:           logger,
43	}
44}
45
46func (p Provider) NewMultiReader(dirPath string) MultiReader {
47	opts := MultiReaderOpts{
48		ArchiveReader:  p.NewArchiveReader(),
49		ManifestReader: p.NewManifestReader(),
50		DirReader:      p.NewDirReader(dirPath),
51	}
52	return NewMultiReader(opts, p.fs)
53}
54
55func (p Provider) NewExtractingArchiveReader() ArchiveReader { return p.archiveReader(true) }
56func (p Provider) NewArchiveReader() ArchiveReader           { return p.archiveReader(false) }
57
58func (p Provider) archiveReader(extracting bool) ArchiveReader {
59	jobReader := boshjob.NewArchiveReaderImpl(extracting, p.compressor, p.fs)
60	pkgReader := boshpkg.NewArchiveReaderImpl(extracting, p.compressor, p.fs)
61	return NewArchiveReader(jobReader, pkgReader, p.compressor, p.fs, p.logger)
62}
63
64func (p Provider) NewDirReader(dirPath string) DirReader {
65	archiveFactory := func(args ArchiveFactoryArgs) Archive {
66		return NewArchiveImpl(
67			args, dirPath, p.fingerprinterFactory(args.FollowSymlinks), p.compressor, p.digestCalculator, p.cmdRunner, p.fs)
68	}
69
70	srcDirPath := filepath.Join(dirPath, "src")
71	blobsDirPath := filepath.Join(dirPath, "blobs")
72
73	jobDirReader := boshjob.NewDirReaderImpl(archiveFactory, p.fs)
74	pkgDirReader := boshpkg.NewDirReaderImpl(archiveFactory, srcDirPath, blobsDirPath, p.fs)
75	licDirReader := boshlic.NewDirReaderImpl(archiveFactory, p.fs)
76
77	return NewDirReader(jobDirReader, pkgDirReader, licDirReader, p.fs, p.logger)
78}
79
80func (p Provider) NewManifestReader() ManifestReader {
81	return NewManifestReader(p.fs, p.logger)
82}
83
84func (p Provider) NewArchiveWriter() ArchiveWriter {
85	return NewArchiveWriter(p.compressor, p.fs, p.logger)
86}
87