Skip to content

Commit f0e11df

Browse files
committed
Use stepenv.Repository instead of custom env setter/getter
1 parent 56c4fb5 commit f0e11df

File tree

3 files changed

+35
-102
lines changed

3 files changed

+35
-102
lines changed

oldcache/cache.go

Lines changed: 7 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package oldcache
22

33
import (
4-
"os"
54
"strings"
65

7-
"github.com/bitrise-io/go-steputils/v2/export"
8-
"github.com/bitrise-io/go-utils/v2/command"
96
"github.com/bitrise-io/go-utils/v2/env"
107
)
118

@@ -15,81 +12,18 @@ const CacheIncludePathsEnvKey = "BITRISE_CACHE_INCLUDE_PATHS"
1512
// CacheExcludePathsEnvKey ...
1613
const CacheExcludePathsEnvKey = "BITRISE_CACHE_EXCLUDE_PATHS"
1714

18-
// VariableSetter ...
19-
type VariableSetter interface {
20-
Set(key, value string) error
21-
}
22-
23-
// OSVariableSetter ...
24-
type OSVariableSetter struct{}
25-
26-
// NewOSVariableSetter ...
27-
func NewOSVariableSetter() VariableSetter {
28-
return OSVariableSetter{}
29-
}
30-
31-
// Set ...
32-
func (e OSVariableSetter) Set(key, value string) error {
33-
return os.Setenv(key, value)
34-
}
35-
36-
// EnvmanVariableSetter ...
37-
type EnvmanVariableSetter struct {
38-
}
39-
40-
// NewEnvmanVariableSetter ...
41-
func NewEnvmanVariableSetter() VariableSetter {
42-
return EnvmanVariableSetter{}
43-
}
44-
45-
// Set ...
46-
func (e EnvmanVariableSetter) Set(key, value string) error {
47-
exporter := export.NewExporter(command.NewFactory(env.NewRepository()))
48-
return exporter.ExportOutput(key, value)
49-
}
50-
51-
// VariableGetter ...
52-
type VariableGetter interface {
53-
Get(key string) (string, error)
54-
}
55-
56-
// OSVariableGetter ...
57-
type OSVariableGetter struct{}
58-
59-
// NewOSVariableGetter ...
60-
func NewOSVariableGetter() VariableGetter {
61-
return OSVariableGetter{}
62-
}
63-
64-
// Get ...
65-
func (e OSVariableGetter) Get(key string) (string, error) {
66-
return os.Getenv(key), nil
67-
}
68-
6915
// Cache ...
7016
type Cache struct {
71-
variableGetter VariableGetter
72-
variableSetters []VariableSetter
17+
envRepository env.Repository
7318

7419
include []string
7520
exclude []string
7621
}
7722

78-
// Config ...
79-
type Config struct {
80-
VariableGetter VariableGetter
81-
VariableSetters []VariableSetter
82-
}
83-
84-
// NewCache ...
85-
func (c Config) NewCache() Cache {
86-
return Cache{variableGetter: c.VariableGetter, variableSetters: c.VariableSetters}
87-
}
88-
8923
// New ...
90-
func New() Cache {
91-
defaultConfig := Config{NewOSVariableGetter(), []VariableSetter{NewOSVariableSetter(), NewEnvmanVariableSetter()}}
92-
return defaultConfig.NewCache()
24+
func New(envRepository env.Repository) Cache {
25+
// defaultConfig := Config{NewOSVariableGetter(), []VariableSetter{NewOSVariableSetter(), NewEnvmanVariableSetter()}}
26+
return Cache{envRepository: envRepository}
9327
}
9428

9529
// IncludePath ...
@@ -105,32 +39,20 @@ func (cache *Cache) ExcludePath(item ...string) {
10539
// Commit ...
10640
func (cache *Cache) Commit() error {
10741
commitCachePath := func(key string, values []string) error {
108-
content, err := cache.variableGetter.Get(key)
109-
if err != nil {
110-
return err
111-
}
112-
42+
content := cache.envRepository.Get(key)
11343
if content != "" {
11444
content += "\n"
11545
}
11646

11747
content += strings.Join(values, "\n")
11848
content += "\n"
11949

120-
for _, setter := range cache.variableSetters {
121-
if err := setter.Set(key, content); err != nil {
122-
return err
123-
}
124-
}
125-
return nil
50+
return cache.envRepository.Set(key, content)
12651
}
12752

12853
if err := commitCachePath(CacheIncludePathsEnvKey, cache.include); err != nil {
12954
return err
13055
}
13156

132-
if err := commitCachePath(CacheExcludePathsEnvKey, cache.exclude); err != nil {
133-
return err
134-
}
135-
return nil
57+
return commitCachePath(CacheExcludePathsEnvKey, cache.exclude)
13658
}

oldcache/cache_test.go

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

99
cache "github.com/bitrise-io/go-steputils/v2/oldcache"
10+
"github.com/bitrise-io/go-steputils/v2/stepenv"
1011
"github.com/bitrise-io/go-utils/v2/command"
1112
"github.com/bitrise-io/go-utils/v2/env"
1213
"github.com/bitrise-io/go-utils/v2/fileutil"
@@ -20,13 +21,13 @@ type MockGetterSetter struct {
2021
}
2122

2223
// NewMockGetterSetter ...
23-
func NewMockGetterSetter() MockGetterSetter {
24+
func NewMockGetterSetter() env.Repository {
2425
return MockGetterSetter{values: map[string]string{}}
2526
}
2627

2728
// Get ...
28-
func (g MockGetterSetter) Get(key string) (string, error) {
29-
return g.values[key], nil
29+
func (g MockGetterSetter) Get(key string) string {
30+
return g.values[key]
3031
}
3132

3233
// Set ...
@@ -35,6 +36,21 @@ func (g MockGetterSetter) Set(key, value string) error {
3536
return nil
3637
}
3738

39+
// List ...
40+
func (g MockGetterSetter) List() []string {
41+
var envs []string
42+
for k, v := range g.values {
43+
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
44+
}
45+
return envs
46+
}
47+
48+
// Unset ...
49+
func (g MockGetterSetter) Unset(key string) error {
50+
delete(g.values, key)
51+
return nil
52+
}
53+
3854
const testEnvVarContent = `/tmp/mypath -> /tmp/mypath/cachefile
3955
/tmp/otherpath
4056
/tmp/anotherpath
@@ -59,6 +75,7 @@ const testThirdCommitIgnoreEnvVarContent = `/*.log
5975
func TestCacheFunctions(t *testing.T) {
6076
filemanager := fileutil.NewFileManager()
6177
pathmodifier := pathutil.NewPathModifier()
78+
cacheEnvRepository := stepenv.NewRepository(env.NewRepository())
6279

6380
t.Log("Init envman")
6481
{
@@ -85,7 +102,7 @@ func TestCacheFunctions(t *testing.T) {
85102

86103
t.Log("Test - cache")
87104
{
88-
c := cache.New()
105+
c := cache.New(cacheEnvRepository)
89106
c.IncludePath("/tmp/mypath -> /tmp/mypath/cachefile")
90107
c.IncludePath("/tmp/otherpath")
91108
c.IncludePath("/tmp/anotherpath")
@@ -105,12 +122,12 @@ func TestCacheFunctions(t *testing.T) {
105122
require.NoError(t, err)
106123
require.Equal(t, testIgnoreEnvVarContent, content)
107124

108-
c = cache.New()
125+
c = cache.New(cacheEnvRepository)
109126
c.ExcludePath("/*.lock")
110127
err = c.Commit()
111128
require.NoError(t, err)
112129

113-
c = cache.New()
130+
c = cache.New(cacheEnvRepository)
114131
c.ExcludePath("/*.lock")
115132
err = c.Commit()
116133
require.NoError(t, err)

oldcache/example_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
cache "github.com/bitrise-io/go-steputils/v2/oldcache"
7+
"github.com/bitrise-io/go-utils/v2/env"
78
)
89

910
// Create ItemCollectors for listing Bitrise cache include and exclude patterns.
@@ -17,8 +18,7 @@ func (c SampleItemCollector) Collect(dir string, cacheLevel cache.Level) ([]stri
1718
func Example() {
1819
// Create a cache, usually using cache.New()
1920
getterSetter := NewMockGetterSetter()
20-
testConfig := cache.Config{getterSetter, []cache.VariableSetter{getterSetter}}
21-
c := testConfig.NewCache()
21+
c := cache.New(getterSetter)
2222

2323
for _, collector := range []cache.ItemCollector{SampleItemCollector{}} {
2424
// Run some Cache ItemCollectors
@@ -42,16 +42,10 @@ func Example() {
4242
// exclude_me.txt
4343
}
4444

45-
func printIncludeAndExcludeEnvs(getterSetter MockGetterSetter) {
46-
includePaths, err := getterSetter.Get(cache.CacheIncludePathsEnvKey)
47-
if err != nil {
48-
panic(err)
49-
}
45+
func printIncludeAndExcludeEnvs(getterSetter env.Repository) {
46+
includePaths := getterSetter.Get(cache.CacheIncludePathsEnvKey)
5047
fmt.Println(includePaths)
5148

52-
excludePaths, err := getterSetter.Get(cache.CacheExcludePathsEnvKey)
53-
if err != nil {
54-
panic(err)
55-
}
49+
excludePaths := getterSetter.Get(cache.CacheExcludePathsEnvKey)
5650
fmt.Println(excludePaths)
5751
}

0 commit comments

Comments
 (0)