-
Notifications
You must be signed in to change notification settings - Fork 3
/
manager.go
74 lines (65 loc) · 1.93 KB
/
manager.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package build
import (
"fmt"
)
// Manager is responsible for orchestrating the running of builds.
// By default it will return cached build results rather then re-running
// a build that's already been done.
type Manager struct {
Settings
runner *Runner
}
func NewManager(r *Runner, opts ...Option) (*Manager, error) {
s, err := newSettings(opts)
if err != nil {
return nil, err
}
return &Manager{
Settings: s,
runner: r,
}, nil
}
func (bm *Manager) Runner() *Runner {
return bm.runner
}
func (bm *Manager) Build() Build {
return bm.runner.build
}
func (bm *Manager) ResultFromCache() (Result, bool, error) {
return bm.runner.build.CachedResult()
}
// Result returns the build result either from cache if present and not forcing a rebuild,
// or by running the build to generate a new result. The only errors that can be returned are
// from the attempt to load from cache, so to check if the build failed or not you still need
// to call the Result's Error method.
func (bm *Manager) Result() (Result, error) {
bm.Debug("Beginning getting result.")
if bm.Build().IsVerification() {
return bm.runBuild("Verification builds never load from cache")
}
if bm.forceRebuild {
return bm.runBuild("Force-rebuild on")
}
bm.Debug("Inspecting cache.")
r, cached, err := bm.ResultFromCache()
if err != nil {
return r, fmt.Errorf("inspecting cache: %w", err)
}
if cached {
bm.Log("Loaded build result from cache; SourceID: %s; Dirty: %t", r.Config.Product.SourceHash, r.Config.Product.IsDirty())
return r, nil
}
return bm.runBuild("No build result available in cache")
}
func (bm *Manager) runBuild(why string) (Result, error) {
bm.Log("%s; Running a fresh build...", why)
result := bm.runner.Run()
cachePath, err := result.Save(bm.Build().IsVerification())
if err != nil {
return result, err
}
bm.Debug("Saved result to cache: %q", cachePath)
return result, nil
}