-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
84 lines (72 loc) · 1.92 KB
/
option.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
// Copyright (c) 2020-2021 C4 Project
//
// This file is part of c4t.
// Licenced under the MIT licence; see `LICENSE`.
package analyser
import (
"errors"
"github.com/c4-project/c4t/internal/plan/analysis"
"github.com/c4-project/c4t/internal/stage/analyser/saver"
)
// ErrObserverNil occurs if we pass a nil Observer to ObserveWith.
var ErrObserverNil = errors.New("observer nil")
// Option is the type of options to the analyser stage constructor.
type Option func(*Analyser) error
// Options applies each option in opts in turn.
func Options(opts ...Option) Option {
return func(a *Analyser) error {
for _, o := range opts {
if err := o(a); err != nil {
return err
}
}
return nil
}
}
// Analysis sets some analysis options.
func Analysis(opts ...analysis.Option) Option {
return func(a *Analyser) error {
a.aopts = append(a.aopts, opts...)
return nil
}
}
// ObserveWith adds each observer in obs to the observer set for analysing.
func ObserveWith(obs ...Observer) Option {
return func(a *Analyser) error {
for _, o := range obs {
if o == nil {
return ErrObserverNil
}
}
a.observers = append(a.observers, obs...)
return nil
}
}
// ObserveSaveWith adds each observer in obs to the observer set for saving.
func ObserveSaveWith(obs ...saver.Observer) Option {
return func(a *Analyser) error {
for _, o := range obs {
if o == nil {
return ErrObserverNil
}
a.saveObservers = append(a.saveObservers, o)
}
return nil
}
}
// ErrorOnBadStatus makes the analyser fail if it observed any 'bad' statuses, if set to true.
func ErrorOnBadStatus(on bool) Option {
return func(a *Analyser) error {
a.errOnBadStatus = on
return nil
}
}
// SaveToPathset makes this analyser stage save to the given pathset.
// This can be nil, in which case saving is disabled.
func SaveToPathset(ps *saver.Pathset) Option {
return func(a *Analyser) error {
// ps can be nil
a.savePaths = ps
return nil
}
}