-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
136 lines (111 loc) · 3 KB
/
cache.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package astra
import (
"encoding/json"
"errors"
"os"
"path"
"strings"
"gopkg.in/yaml.v3"
)
// The caching mechanism is used to cache the service in a file so that it can be loaded later on
// At the moment it is only used the CLI to load the service and the files that are needed to be crawled by the AST parser with their respective inputs
// Plans are for the future to use it as a change only mechanism to only generate the files that have changed since the last build
const cacheFileName = "cache.json"
// Cache the service in a file.
func (s *Service) Cache() error {
s.Log.Debug().Msg("Caching service")
var cachePath string
if s.CachePath != "" {
cachePath = s.CachePath
} else {
cachePath = path.Join(s.getAstraDirPath(), cacheFileName)
}
if _, err := os.Stat(cachePath); err == nil {
err := s.ClearCache()
if err != nil {
return err
}
}
f, err := os.Create(cachePath)
if err != nil {
return err
}
var cacheStr []byte
if strings.HasSuffix(cachePath, ".yaml") || strings.HasSuffix(cachePath, ".yml") {
cacheStr, err = yaml.Marshal(s)
} else {
cacheStr, err = json.Marshal(s)
}
if err != nil {
return err
}
_, err = f.Write(cacheStr)
if err != nil {
return err
}
s.Log.Debug().Msg("Cached service")
return nil
}
// LoadCache Load the service from a file cache.
// If the file does not exist, it will not return an error.
func (s *Service) LoadCache() error {
s.Log.Debug().Msg("Loading cached service")
var cachePath string
if s.CachePath != "" {
cachePath = s.CachePath
} else {
cachePath = path.Join(s.getAstraDirPath(), cacheFileName)
}
if _, err := os.Stat(cachePath); err != nil {
return err
}
if err := s.LoadCacheFromCustomPath(cachePath); err != nil {
return err
}
s.Log.Debug().Msg("Loaded cached service")
return nil
}
// LoadCacheFromCustomPath Load the service from a file cache.
// If the file does not exist, it will return an error.
// Requires the path to the cache file.
func (s *Service) LoadCacheFromCustomPath(cachePath string) error {
f, err := os.Open(cachePath)
if err != nil {
return err
}
var service Service
if strings.HasSuffix(cachePath, ".json") {
err = json.NewDecoder(f).Decode(&service)
} else if strings.HasSuffix(cachePath, ".yaml") || strings.HasSuffix(cachePath, ".yml") {
err = yaml.NewDecoder(f).Decode(&service)
} else {
err = errors.New("unsupported file format")
}
if err != nil {
return err
}
s.Inputs = service.Inputs
s.Outputs = service.Outputs
s.Config = service.Config
s.Routes = service.Routes
s.Components = service.Components
return nil
}
// ClearCache Clear the cache file.
func (s *Service) ClearCache() error {
s.Log.Debug().Msg("Clearing cached service")
var cachePath string
if s.CachePath != "" {
cachePath = s.CachePath
} else {
cachePath = path.Join(s.getAstraDirPath(), cacheFileName)
}
if _, err := os.Stat(cachePath); err != nil {
return err
}
if err := os.RemoveAll(cachePath); err != nil {
return err
}
s.Log.Debug().Msg("Cleared cached service")
return nil
}