11package oldcache
22
33import (
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 ...
1613const 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 ...
7016type 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 ...
10640func (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}
0 commit comments