-
Notifications
You must be signed in to change notification settings - Fork 61
/
bundle_factory.go
53 lines (41 loc) · 1.37 KB
/
bundle_factory.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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package helpers
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
type BundleFactory struct {
assets *Assets
t *testing.T
bundleFolder string
}
func NewBundleDir(t *testing.T, assets *Assets) BundleFactory {
return BundleFactory{assets: assets, t: t}
}
func (b *BundleFactory) CreateBundleDir(bYml, iYml string) string {
b.t.Helper()
outDir := b.assets.CreateAndCopySimpleApp("main-bundle")
imgpkgDir := filepath.Join(outDir, ".imgpkg")
err := os.Mkdir(imgpkgDir, 0700)
require.NoError(b.t, err, "unable to create .imgpkg folder")
err = os.WriteFile(filepath.Join(imgpkgDir, BundleFile), []byte(bYml), 0600)
require.NoError(b.t, err, "unable to create bundle lock file")
err = os.WriteFile(filepath.Join(imgpkgDir, ImageFile), []byte(iYml), 0600)
require.NoError(b.t, err, "unable to create images lock file")
b.bundleFolder = outDir
return outDir
}
func (b *BundleFactory) AddFileToBundle(path, content string) {
b.t.Helper()
subfolders, _ := filepath.Split(path)
if subfolders != "" {
path := filepath.Join(b.bundleFolder, subfolders)
err := os.MkdirAll(path, 0700)
require.NoError(b.t, err, "unable to add subfolders to bundle")
}
err := os.WriteFile(filepath.Join(b.bundleFolder, path), []byte(content), 0600)
require.NoError(b.t, err)
}