Skip to content

Commit 395a648

Browse files
committed
bug fixes
1 parent 0812f24 commit 395a648

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

lib/app/workflow.go

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,22 @@ type WorkflowInclude struct {
4949
ValidationState string
5050
Contents string
5151
Hash string
52-
Workflow *StackupWorkflow
52+
//Workflow *StackupWorkflow
5353
}
5454

5555
type WorkflowSettings struct {
5656
Defaults *WorkflowSettingsDefaults `yaml:"defaults"`
5757
ExitOnChecksumMismatch bool `yaml:"exit-on-checksum-mismatch"`
5858
DotEnvFiles []string `yaml:"dotenv"`
59-
Cache struct {
60-
TtlMinutes int `yaml:"ttl-minutes"`
61-
} `yaml:"cache"`
62-
Domains struct {
59+
Cache *WorkflowSettingsCache `yaml:"cache"`
60+
Domains struct {
6361
Allowed []string `yaml:"allowed"`
6462
} `yaml:"domains"`
6563
}
6664

65+
type WorkflowSettingsCache struct {
66+
TtlMinutes int `yaml:"ttl-minutes"`
67+
}
6768
type WorkflowSettingsDefaults struct {
6869
Tasks *WorkflowSettingsDefaultsTasks `yaml:"tasks"`
6970
}
@@ -151,13 +152,13 @@ func (wi *WorkflowInclude) ValidateChecksum(contents string) (bool, error) {
151152
algorithm := ""
152153
storedChecksum := ""
153154
checksumContents := ""
154-
hashUrl := ""
155155

156156
for _, url := range checksumUrls {
157-
if wi.Workflow.Cache.Has(url) && !wi.Workflow.Cache.IsExpired(url) {
158-
hashUrl = url
159-
checksumContents = wi.Workflow.Cache.Get(url)
160-
fmt.Printf("using cached checksum file %s\n", url)
157+
158+
if !App.Workflow.Cache.IsExpired(url) {
159+
wi.ChecksumUrl = url
160+
checksumContents = App.Workflow.Cache.Get(url)
161+
// fmt.Printf("using cached checksum file %s\n", url)
161162
break
162163
}
163164

@@ -166,22 +167,27 @@ func (wi *WorkflowInclude) ValidateChecksum(contents string) (bool, error) {
166167
continue
167168
}
168169

170+
wi.ChecksumUrl = url
171+
169172
if checksumContents != "" {
170-
hashUrl = url
171-
wi.Workflow.Cache.Set(url, checksumContents, wi.Workflow.Settings.Cache.TtlMinutes)
172-
fmt.Printf("using non-cached checksum file %s\n", url)
173+
// fmt.Printf("using checksum file %s\n", wi.ChecksumUrl)
174+
App.Workflow.Cache.Set(url, checksumContents, 3)
175+
// fmt.Printf("using non-cached checksum file %s\n", url)
173176
break
174177
}
175178
}
176179

177180
if checksumContents != "" {
178181
storedChecksum = wi.getChecksumFromContents(checksumContents)
179182

180-
wi.ChecksumUrl = hashUrl
183+
// wi.ChecksumUrl = hashUrl
184+
// fmt.Println("checksum url: " + wi.ChecksumUrl)
181185
algorithm = wi.GetChecksumAlgorithm()
182186
}
183187

184-
if algorithm == "unknown" {
188+
// algorithm = "sha256"
189+
190+
if algorithm == "unknown" || algorithm == "" {
185191
return false, fmt.Errorf("unable to find valid checksum file for %s", wi.DisplayUrl())
186192
}
187193

@@ -318,7 +324,7 @@ func (workflow *StackupWorkflow) reversePreconditions(items []*Precondition) []*
318324
}
319325

320326
func (workflow *StackupWorkflow) Initialize() {
321-
workflow.Cache = cache.CreateCache(utils.GetProjectName())
327+
workflow.Cache = cache.CreateCache("")
322328

323329
// generate uuids for each task as the initial step, as other code below relies on a uuid existing
324330
for _, task := range workflow.Tasks {
@@ -336,6 +342,7 @@ func (workflow *StackupWorkflow) Initialize() {
336342
if workflow.Settings == nil {
337343
workflow.Settings = &WorkflowSettings{
338344
DotEnvFiles: []string{".env"},
345+
Cache: &WorkflowSettingsCache{TtlMinutes: 5},
339346
Defaults: &WorkflowSettingsDefaults{
340347
Tasks: &WorkflowSettingsDefaultsTasks{
341348
Silent: false,
@@ -371,7 +378,7 @@ func (workflow *StackupWorkflow) Initialize() {
371378

372379
// initialize the includes
373380
for _, inc := range workflow.Includes {
374-
inc.Initialize(workflow)
381+
inc.Initialize()
375382
}
376383

377384
workflow.ProcessIncludes()
@@ -416,9 +423,9 @@ func (workflow *StackupWorkflow) ProcessIncludes() {
416423
var wg sync.WaitGroup
417424
for _, include := range workflow.Includes {
418425
wg.Add(1)
419-
go func(include *WorkflowInclude) {
426+
go func(inc *WorkflowInclude) {
420427
defer wg.Done()
421-
workflow.ProcessInclude(include)
428+
workflow.ProcessInclude(inc)
422429
}(include)
423430
}
424431
wg.Wait()
@@ -429,17 +436,16 @@ func (workflow *StackupWorkflow) ProcessInclude(include *WorkflowInclude) bool {
429436
return false
430437
}
431438

432-
var contents string
433439
var err error
434440

435441
if workflow.Cache.Has(include.DisplayName()) && !workflow.Cache.IsExpired(include.DisplayName()) {
436442
include.Contents = workflow.Cache.Get(include.DisplayName())
437443
include.Hash = workflow.Cache.GetHash(include.DisplayName())
438-
fmt.Println("loaded from cache")
444+
// fmt.Println("loaded from cache")
439445
}
440446

441447
if !workflow.Cache.Has(include.DisplayName()) || workflow.Cache.IsExpired(include.DisplayName()) {
442-
fmt.Println("not loaded from cache")
448+
// fmt.Println("not loaded from cache")
443449
if include.IsLocalFile() {
444450
include.Contents, err = utils.GetFileContents(include.Filename())
445451
} else if include.IsRemoteUrl() {
@@ -453,7 +459,7 @@ func (workflow *StackupWorkflow) ProcessInclude(include *WorkflowInclude) bool {
453459
}
454460

455461
include.Hash = checksums.CalculateSha256Hash(include.Contents)
456-
workflow.Cache.Set(include.DisplayName(), include.Contents, workflow.Settings.Cache.TtlMinutes)
462+
workflow.Cache.Set(include.DisplayName(), include.Contents, 3)
457463
}
458464

459465
// fmt.Printf("value: %v\n", workflow.Cache.Get(include.DisplayName()))
@@ -473,8 +479,8 @@ func (workflow *StackupWorkflow) ProcessInclude(include *WorkflowInclude) bool {
473479

474480
if include.IsRemoteUrl() {
475481
if *include.VerifyChecksum == true || include.VerifyChecksum == nil {
476-
//support.StatusMessage("Validating checksum for remote include: "+include.DisplayUrl(), false)
477-
validated, err := include.ValidateChecksum(include.Contents)
482+
// support.StatusMessage("Validating checksum for remote include: "+include.DisplayUrl(), false)
483+
validated, _ := include.ValidateChecksum(include.Contents)
478484

479485
if include.ChecksumIsValid != nil && *include.ChecksumIsValid == true {
480486
include.ValidationState = "checksum validated"
@@ -484,10 +490,10 @@ func (workflow *StackupWorkflow) ProcessInclude(include *WorkflowInclude) bool {
484490
include.ValidationState = "checksum mismatch"
485491
}
486492

487-
if err != nil {
488-
fmt.Println(err)
489-
return false
490-
}
493+
// if err != nil {
494+
// fmt.Println(err)
495+
// return false
496+
// }
491497

492498
if !validated {
493499
if App.Workflow.Settings.ExitOnChecksumMismatch {
@@ -500,29 +506,29 @@ func (workflow *StackupWorkflow) ProcessInclude(include *WorkflowInclude) bool {
500506
}
501507

502508
template := &IncludedTemplate{}
503-
err = yaml.Unmarshal([]byte(contents), template)
509+
err = yaml.Unmarshal([]byte(include.Contents), template)
504510

505511
if err != nil {
506512
fmt.Println(err)
507513
return false
508514
}
509515

510516
if len(template.Init) > 0 {
511-
workflow.Init += "\n" + template.Init
517+
App.Workflow.Init += "\n" + template.Init
512518
}
513519

514520
// prepend the included preconditions; we reverse the order of the preconditions in the included file,
515521
// then reverse the existing preconditions, append them, then reverse the workflow preconditions again
516522
// to achieve the correct order.
517-
App.Workflow.Preconditions = workflow.reversePreconditions(App.Workflow.Preconditions)
518-
template.Preconditions = workflow.reversePreconditions(template.Preconditions)
523+
App.Workflow.Preconditions = App.Workflow.reversePreconditions(App.Workflow.Preconditions)
524+
template.Preconditions = App.Workflow.reversePreconditions(template.Preconditions)
519525

520526
for _, p := range template.Preconditions {
521527
p.FromRemote = true
522528
App.Workflow.Preconditions = append(App.Workflow.Preconditions, p)
523529
}
524530

525-
App.Workflow.Preconditions = workflow.reversePreconditions(App.Workflow.Preconditions)
531+
App.Workflow.Preconditions = App.Workflow.reversePreconditions(App.Workflow.Preconditions)
526532

527533
for _, t := range template.Tasks {
528534
t.FromRemote = true
@@ -535,8 +541,8 @@ func (workflow *StackupWorkflow) ProcessInclude(include *WorkflowInclude) bool {
535541
return true
536542
}
537543

538-
func (wi *WorkflowInclude) Initialize(workflow *StackupWorkflow) {
539-
wi.Workflow = workflow
544+
func (wi *WorkflowInclude) Initialize() {
545+
//wi.Workflow = workflow
540546

541547
// expand environment variables in the include headers
542548
for i, v := range wi.Headers {

lib/cache/cache.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Cache struct {
2323
func CreateCache(name string) *Cache {
2424
result := Cache{Name: name, Enabled: false}
2525
result.Init()
26+
result.Enabled = true
2627

2728
return &result
2829
}
@@ -39,7 +40,7 @@ func ensureConfigDirExists(dirName string) (string, error) {
3940
configDir := filepath.Join(homeDir, dirName)
4041

4142
// Ensure the directory exists
42-
err = os.MkdirAll(configDir, 0700)
43+
err = os.MkdirAll(configDir, 0744)
4344
if err != nil {
4445
return "", err
4546
}
@@ -79,22 +80,21 @@ func (c *Cache) Init() {
7980
})
8081

8182
c.Enabled = true
82-
c.purgeExpired()
8383
}
8484

8585
// The `Get` function in the `Cache` struct is used to retrieve the value of a cache entry with a given
8686
// key. It takes a `key` parameter (string) and returns the corresponding value (string).
8787
func (c *Cache) Get(key string) string {
8888
var result string
8989

90-
if !c.Has(key) {
91-
return ""
92-
}
90+
// if !c.Has(key) {
91+
// return ""
92+
// }
9393

94-
if c.IsExpired(key) {
95-
c.purgeExpired()
96-
return ""
97-
}
94+
// if c.IsExpired(key) {
95+
// c.purgeExpired()
96+
// return ""
97+
// }
9898

9999
c.Db.View(func(tx *bolt.Tx) error {
100100
b := tx.Bucket([]byte(c.Name))
@@ -112,6 +112,7 @@ func (c *Cache) Get(key string) string {
112112
// function ensures that expired cache entries are automatically removed from the cache to free up
113113
// space and maintain cache integrity.
114114
func (c *Cache) purgeExpired() {
115+
return
115116
c.Db.Update(func(tx *bolt.Tx) error {
116117
b := tx.Bucket([]byte(c.Name))
117118
cur := b.Cursor()

0 commit comments

Comments
 (0)