/
blueprint_cache.go
327 lines (269 loc) · 7.66 KB
/
blueprint_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// cmd Corbra CLI
package cmd
/*
Copyright © 2019 PavedRoad <info@pavedroad.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
homedir "github.com/mitchellh/go-homedir"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/yaml.v2"
)
// The release branch stores released blueprints
// The latest and stable tags are used to select which release
const (
gitBlueprintBranch plumbing.ReferenceName = "refs/heads/release"
gitLatestTag plumbing.ReferenceName = "refs/heads/latest"
gitStableTag plumbing.ReferenceName = "refs/heads/stable"
blueprintRepository = "https://github.com/pavedroad-io/blueprints"
githubAPI = "GitHub API"
gitclone = "git clone"
// File that holds meta-data about a cache
bpCacheFileName string = ".pr_cache"
)
// bpDirectory manages blueprint directory locations
type bpDirectory struct {
// Full path to the blueprint directory
location string
// Is it initialized
initialized bool
// How we determined the location
// default, so.GetEnv, command line option
locationFrom string
}
// bpCache manages information about blueprints
// stored in a blueprint directory
type bpCache struct {
// What directory is it in
location *bpDirectory
// Persist information to disk in a cache file
CacheFile bpCacheFile `json:"cache_file"`
}
// errno constants for bpCacheError
const (
tcBadBlueprintDirectory = iota
tcNoCacheFile
tcBadCacheFile
tcSuccess
)
// errmsg constants for bpCacheError
const (
TcBadBlueprintDirectory = "Unable to create blueprint directory, Got (%v)\n"
TcNoCacheFile = "Cache file not found (%v)\n"
TcBadCacheFile = "Bad cache file (%v)\n"
)
// bpCacheError
type bpCacheError struct {
errno int
errmsg string
}
func (tc *bpCacheError) Error() string {
return tc.errmsg
}
// bpCacheFile Store information to disk for later access
type bpCacheFile struct {
// Cache file syntax version
Version string `json:"version"`
// Time it was created on disk
Created time.Time `json:"created"`
// last updated
Updated time.Time `json:"updated"`
// Is it initialized
Initialized bool `json:"initialized"`
// git clone or github API
InitializedFrom string `json:"initialized_from"`
// git branch
Branch string `json:"branch"`
// git tags
Tags []string `json:"tags"`
}
// Location returns the location of the blueprint directory
// Initialize if necessary
func (t *bpDirectory) Location() string {
if !t.initialized && t.location == "" {
err := t.initialize()
if err != nil {
log.Fatal(err.Error())
return ""
}
}
return t.location
}
// initialize a private function for initializing
// the blueprint directory location, not the
// blueprints
func (t *bpDirectory) initialize() error {
// Order of precedence
// - roadctl CLI
// - PR_BLUEPRINT_DIR
// - defaultBlueprintDir
env := os.Getenv("PR_BLUEPRINT_DIR")
if blueprintDirectoryLocation != "" {
// Make sure they
if strings.Contains(blueprintDirectoryLocation, "/"+defaultBlueprintDir) {
fmt.Printf("Don't include /blueprint with --blueprint option\n")
if string(blueprintDirectoryLocation[len(blueprintDirectoryLocation)-1]) == "/" {
fmt.Printf("Use: --blueprint %s\n",
strings.TrimSuffix(blueprintDirectoryLocation, "/"+defaultBlueprintDir+"/"))
} else {
fmt.Printf("Use: --blueprint %s\n",
strings.TrimSuffix(blueprintDirectoryLocation, "/"+defaultBlueprintDir))
}
os.Exit(-1)
}
nl := blueprintDirectoryLocation + "/" + defaultBlueprintDir
t.location = nl
t.locationFrom = "CLI"
} else if env != "" && blueprintDirectoryLocation == "" {
t.location = env + "/" + defaultBlueprintDir
t.locationFrom = "PR_BLUEPRINT_DIR"
} else {
home, err := homedir.Dir()
if err != nil {
fmt.Println("error setting home directory")
}
home = home + "/" + prHome + "/" + defaultBlueprintDir
t.location = home
t.locationFrom = "default"
}
// test if directory exists
if !t.initialized {
if err := createDirectory(t.location); err != nil {
log.Fatal(err.Error())
}
}
t.initialized = true
return nil
}
func (t *bpDirectory) getDefault() string {
return ""
}
// New create a bpCache
// If it does not exists, initialize it using method specified
// td: a bpDirectory type
// method: GitHub API or git clone
func (tc *bpCache) CreateCache(method, branch string) error {
log.Println("Cloning with method: ", method)
switch method {
case gitclone:
if err := tc.Clone(branch); err != nil {
fmt.Errorf("Blueprint clone failed error = %v\n", err)
return err
}
case githubAPI:
//tc.API()
default:
fmt.Println("error")
}
return nil
}
// NewBlueprintCache read the blueprint directory and it's meta-data
func NewBlueprintCache() (*bpCache, bpCacheError) {
t := bpDirectory{}
tc := bpCache{location: &t}
te := bpCacheError{errno: tcSuccess}
if dir := t.Location(); dir == "" {
fmt.Println("baddirectory")
te.errno = tcBadBlueprintDirectory
te.errmsg = fmt.Sprintf(TcBadBlueprintDirectory, dir)
return &tc, te
}
err := tc.readCache()
if err != nil {
fmt.Println("failed read")
te.errno = tcNoCacheFile
te.errmsg = fmt.Sprintf(TcNoCacheFile, err)
return &tc, te
}
return &tc, te
}
// Clone Do a git clone if the repository doesn't exist
// in the desired directory
func (tc *bpCache) Clone(branch string) error {
// See if it already has been cloned
if e := tc.readCache(); e == nil {
log.Println("Cache exists exiting")
return e
}
/* TODO: Fix this
cb := gitBlueprintBranch
if branch != gitBlueprintBranch.String() {
cb = plumbing.ReferenceName(branch)
}
*/
_, err := git.PlainClone(defaultBlueprintDir, false, &git.CloneOptions{
URL: blueprintRepository,
ReferenceName: "refs/heads/release",
//ReferenceName: cb,
})
if err != nil {
log.Fatal(err)
}
tc.CacheFile.Created = time.Now()
tc.CacheFile.Updated = time.Now()
tc.CacheFile.InitializedFrom = "clone"
tc.CacheFile.Version = "1.0.0alpha"
tc.CacheFile.Initialized = true
tc.CacheFile.Branch = gitBlueprintBranch.String()
if err = tc.writeCache(defaultBlueprintDir); err != nil {
log.Fatal(err)
}
return nil
}
// writeCache save cache file in repository
// dir is set if not using tc.location
func (tc *bpCache) writeCache(dir string) error {
var cfn string
fileData, err := yaml.Marshal(tc.CacheFile)
if err != nil {
return err
}
if dir != "" {
cfn = dir + "/" + bpCacheFileName
} else {
cfn = tc.location.Location() + "/" + bpCacheFileName
}
err = ioutil.WriteFile(cfn, fileData, 0600)
if err != nil {
log.Fatal(err, cfn)
return err
}
return nil
}
// readCache Opens the cache file and reads contents
func (tc *bpCache) readCache() error {
fl := tc.location.Location() + "/" + bpCacheFileName
if _, err := os.Stat(fl); os.IsNotExist(err) {
return errors.New("Not found")
}
df, err := os.Open(fl)
if err != nil {
log.Println("failed to open:", fl, ", error:", err)
}
defer df.Close()
byteValue, e := ioutil.ReadAll(df)
if e != nil {
log.Fatal("read failed for ", fl)
}
err = yaml.Unmarshal(byteValue, &tc.CacheFile)
if err != nil {
return err
}
return nil
}