Skip to content

Commit 27ef76c

Browse files
committed
Added Cache interface, added docs.
1 parent f0e11df commit 27ef76c

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

oldcache/cache.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,52 @@ package oldcache
33
import (
44
"strings"
55

6+
"github.com/bitrise-io/go-steputils/v2/stepenv"
67
"github.com/bitrise-io/go-utils/v2/env"
78
)
89

9-
// CacheIncludePathsEnvKey ...
10+
// CacheIncludePathsEnvKey's value is a newline separated list of paths that should be included in the cache.
1011
const CacheIncludePathsEnvKey = "BITRISE_CACHE_INCLUDE_PATHS"
1112

12-
// CacheExcludePathsEnvKey ...
13+
// CacheExcludePathsEnvKey's value is a newline separated list of paths that should be excluded from the cache.
1314
const CacheExcludePathsEnvKey = "BITRISE_CACHE_EXCLUDE_PATHS"
1415

15-
// Cache ...
16-
type Cache struct {
16+
// Cache is an interface for managing cache paths.
17+
type Cache interface {
18+
IncludePath(item ...string)
19+
ExcludePath(item ...string)
20+
Commit() error
21+
}
22+
23+
type cache struct {
1724
envRepository env.Repository
1825

1926
include []string
2027
exclude []string
2128
}
2229

23-
// New ...
30+
// NewDefault creates a new Cache instance that set envs using envman.
31+
func NewDefault() Cache {
32+
return New(stepenv.NewRepository(env.NewRepository()))
33+
}
34+
35+
// New creates a new Cache instance with a custom env.Repository.
2436
func New(envRepository env.Repository) Cache {
25-
// defaultConfig := Config{NewOSVariableGetter(), []VariableSetter{NewOSVariableSetter(), NewEnvmanVariableSetter()}}
26-
return Cache{envRepository: envRepository}
37+
return &cache{envRepository: envRepository}
2738
}
2839

29-
// IncludePath ...
30-
func (cache *Cache) IncludePath(item ...string) {
40+
// IncludePath appends paths to the cache include list.
41+
func (cache *cache) IncludePath(item ...string) {
3142
cache.include = append(cache.include, item...)
3243
}
3344

34-
// ExcludePath ...
35-
func (cache *Cache) ExcludePath(item ...string) {
45+
// ExcludePath appends paths to the cache exclude list.
46+
func (cache *cache) ExcludePath(item ...string) {
3647
cache.exclude = append(cache.exclude, item...)
3748
}
3849

39-
// Commit ...
40-
func (cache *Cache) Commit() error {
50+
// Commit writes paths to environment variables (also exported by envman when using stepenv.Repository).
51+
func (cache *cache) Commit() error {
4152
commitCachePath := func(key string, values []string) error {
4253
content := cache.envRepository.Get(key)
4354
if content != "" {

oldcache/cache_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"testing"
88

99
cache "github.com/bitrise-io/go-steputils/v2/oldcache"
10-
"github.com/bitrise-io/go-steputils/v2/stepenv"
1110
"github.com/bitrise-io/go-utils/v2/command"
1211
"github.com/bitrise-io/go-utils/v2/env"
1312
"github.com/bitrise-io/go-utils/v2/fileutil"
@@ -75,7 +74,6 @@ const testThirdCommitIgnoreEnvVarContent = `/*.log
7574
func TestCacheFunctions(t *testing.T) {
7675
filemanager := fileutil.NewFileManager()
7776
pathmodifier := pathutil.NewPathModifier()
78-
cacheEnvRepository := stepenv.NewRepository(env.NewRepository())
7977

8078
t.Log("Init envman")
8179
{
@@ -102,7 +100,7 @@ func TestCacheFunctions(t *testing.T) {
102100

103101
t.Log("Test - cache")
104102
{
105-
c := cache.New(cacheEnvRepository)
103+
c := cache.NewDefault()
106104
c.IncludePath("/tmp/mypath -> /tmp/mypath/cachefile")
107105
c.IncludePath("/tmp/otherpath")
108106
c.IncludePath("/tmp/anotherpath")
@@ -122,12 +120,12 @@ func TestCacheFunctions(t *testing.T) {
122120
require.NoError(t, err)
123121
require.Equal(t, testIgnoreEnvVarContent, content)
124122

125-
c = cache.New(cacheEnvRepository)
123+
c = cache.NewDefault()
126124
c.ExcludePath("/*.lock")
127125
err = c.Commit()
128126
require.NoError(t, err)
129127

130-
c = cache.New(cacheEnvRepository)
128+
c = cache.NewDefault()
131129
c.ExcludePath("/*.lock")
132130
err = c.Commit()
133131
require.NoError(t, err)

0 commit comments

Comments
 (0)