forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.go
297 lines (251 loc) · 8.48 KB
/
emitter.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
package scribe
import (
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/postal"
)
type launchProcess interface {
GetType() string
GetCommand() []string
GetArgs() []string
GetDefault() bool
}
type indirectProcess struct {
packit.Process
}
func (p indirectProcess) GetType() string {
return p.Type
}
func (p indirectProcess) GetCommand() []string {
return strings.Split(p.Command, " ")
}
func (p indirectProcess) GetArgs() []string {
return p.Args
}
func (p indirectProcess) GetDefault() bool {
return p.Default
}
type directProcess struct {
packit.DirectProcess
}
func (p directProcess) GetType() string {
return p.Type
}
func (p directProcess) GetCommand() []string {
return p.Command
}
func (p directProcess) GetArgs() []string {
return p.Args
}
func (p directProcess) GetDefault() bool {
return p.Default
}
// An Emitter embeds the scribe.Logger type to provide an interface for
// complicated shared logging tasks.
type Emitter struct {
// Logger is embedded and therefore delegates all of its functions to the
// Emitter.
Logger
}
// NewEmitter returns an emitter that writes to the given output.
func NewEmitter(output io.Writer) Emitter {
return Emitter{
Logger: NewLogger(output),
}
}
// WithLevel takes in a log level string and configures the underlying Logger
// log level. To enable debug logging the log level must be set to "DEBUG".
func (e Emitter) WithLevel(level string) Emitter {
e.Logger = e.Logger.WithLevel(level)
return e
}
// SelectedDependency takes in a buildpack plan entry, a postal dependency, and
// the current time, and prints out a message giving the name and version of
// the dependency as well as the source of the request for that given
// dependency, it will also print a deprecation warning and an EOL warning
// based if the given dependency is set to be deprecated within the next 30 or
// is past that window.
func (e Emitter) SelectedDependency(entry packit.BuildpackPlanEntry, dependency postal.Dependency, now time.Time) {
source, ok := entry.Metadata["version-source"].(string)
if !ok {
source = "<unknown>"
}
e.Subprocess("Selected %s version (using %s): %s", dependency.Name, source, dependency.Version)
if (dependency.DeprecationDate != time.Time{}) {
deprecationDate := dependency.DeprecationDate
switch {
case (deprecationDate.Add(-30*24*time.Hour).Before(now) && deprecationDate.After(now)):
e.Action("Version %s of %s will be deprecated after %s.", dependency.Version, dependency.Name, dependency.DeprecationDate.Format("2006-01-02"))
e.Action("Migrate your application to a supported version of %s before this time.", dependency.Name)
case (deprecationDate == now || deprecationDate.Before(now)):
e.Action("Version %s of %s is deprecated.", dependency.Version, dependency.Name)
e.Action("Migrate your application to a supported version of %s.", dependency.Name)
}
}
e.Break()
}
// Candidates takes a priority sorted list of buildpack plan entries and prints
// out a formatted table in priority order removing any duplicate entries.
func (e Emitter) Candidates(entries []packit.BuildpackPlanEntry) {
e.Subprocess("Candidate version sources (in priority order):")
var (
sources [][2]string
maxLen int
)
Entries:
for _, entry := range entries {
versionSource, ok := entry.Metadata["version-source"].(string)
if !ok {
versionSource = "<unknown>"
}
if len(versionSource) > maxLen {
maxLen = len(versionSource)
}
version, ok := entry.Metadata["version"].(string)
if !ok {
version = ""
}
source := [2]string{versionSource, version}
// Removes any duplicate entries
for _, s := range sources {
if s == source {
continue Entries
}
}
sources = append(sources, source)
}
for _, source := range sources {
e.Action(("%-" + strconv.Itoa(maxLen) + "s -> %q"), source[0], source[1])
}
e.Break()
}
// LaunchProcesses take a list of (indirect) processes and a map of process specific
// enivronment varables and prints out a formatted table including the type
// name, whether or not it is a default process, the command, arguments, and
// any process specific environment variables.
func (e Emitter) LaunchProcesses(processes []packit.Process, processEnvs ...map[string]packit.Environment) {
launchProcesses := []launchProcess{}
for _, process := range processes {
launchProcesses = append(launchProcesses, indirectProcess{process})
}
e.launch(launchProcesses, processEnvs...)
}
// LaunchDirectProcesses take a list of direct processes and a map of process specific
// enivronment varables and prints out a formatted table including the type
// name, whether or not it is a default process, the command, arguments, and
// any process specific environment variables.
func (e Emitter) LaunchDirectProcesses(processes []packit.DirectProcess, processEnvs ...map[string]packit.Environment) {
launchProcesses := []launchProcess{}
for _, process := range processes {
launchProcesses = append(launchProcesses, directProcess{process})
}
e.launch(launchProcesses, processEnvs...)
}
func (e Emitter) launch(processes []launchProcess, processEnvs ...map[string]packit.Environment) {
e.Process("Assigning launch processes:")
var (
typePadding int
)
for _, process := range processes {
pType := process.GetType()
if process.GetDefault() {
pType += " " + "(default)"
}
if len(pType) > typePadding {
typePadding = len(pType)
}
}
for _, process := range processes {
pType := process.GetType()
if process.GetDefault() {
pType += " " + "(default)"
}
command := strings.Join(process.GetCommand(), " ")
pad := typePadding + len(command) - len(pType)
p := fmt.Sprintf("%s: %*s", pType, pad, command)
if process.GetArgs() != nil {
p += " " + strings.Join(process.GetArgs(), " ")
}
e.Subprocess(p)
// This ensures that the process environment variable is always the same no
// matter the order of the process envs map list
processEnv := packit.Environment{}
for _, pEnvs := range processEnvs {
if env, ok := pEnvs[process.GetType()]; ok {
for key, value := range env {
processEnv[key] = value
}
}
}
if len(processEnv) != 0 {
e.Action("%s", NewFormattedMapFromEnvironment(processEnv))
}
}
e.Break()
}
// EnvironmentVariables takes a layer and prints out a formatted table of the
// build and launch time environment variables set in the layer.
func (e Emitter) EnvironmentVariables(layer packit.Layer) {
buildEnv := packit.Environment{}
launchEnv := packit.Environment{}
// Makes deep local copy of the env map on the layer
for key, value := range layer.BuildEnv {
buildEnv[key] = value
}
for key, value := range layer.LaunchEnv {
launchEnv[key] = value
}
// Merge the shared env map with the launch and build to remove CNB spec
// specific terminiology from the output
for key, value := range layer.SharedEnv {
buildEnv[key] = value
launchEnv[key] = value
}
if len(buildEnv) != 0 {
e.Process("Configuring build environment")
e.Subprocess("%s", NewFormattedMapFromEnvironment(buildEnv))
e.Break()
}
if len(launchEnv) != 0 {
e.Process("Configuring launch environment")
e.Subprocess("%s", NewFormattedMapFromEnvironment(launchEnv))
e.Break()
}
}
// LayerFlags takes a layer and prints out the state of the build, launch,
// and cache layer flags in human-readable language.
func (e Emitter) LayerFlags(layer packit.Layer) {
e.Debug.Process("Setting up layer '%s'", layer.Name)
e.Debug.Subprocess("Available at app launch: %t", layer.Launch)
e.Debug.Subprocess("Available to other buildpacks: %t", layer.Build)
e.Debug.Subprocess("Cached for rebuilds: %t", layer.Cache)
e.Debug.Break()
}
// GeneratingSBOM takes a path to a directory and logs that an SBOM is
// being generated for that directory.
func (e Emitter) GeneratingSBOM(path string) {
e.Process("Generating SBOM for %s", path)
}
// FormattingSBOM takes a list of SBOM formats and logs that an SBOM is
// generated in each format. Note: Only logs when the emitter is in DEBUG
// mode.
func (e Emitter) FormattingSBOM(formats ...string) {
e.Debug.Process("Writing SBOM in the following format(s):")
for _, f := range formats {
e.Debug.Subprocess(f)
}
e.Debug.Break()
}
// BuildConfiguration takes a map representing environment variables
// that will configure a buildpack build and prints them in a formatted
// table.
func (e Emitter) BuildConfiguration(envVars map[string]string) {
formatted := NewFormattedMapFromEnvironment(envVars)
e.Debug.Process("Build configuration:")
e.Debug.Subprocess(formatted.String())
e.Debug.Break()
}