-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
output.go
110 lines (89 loc) · 2.72 KB
/
output.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
/*
Copyright 2021 The Skaffold Authors
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.
*/
package output
import (
"context"
"io"
"time"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/constants"
eventV2 "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/event/v2"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
)
const timestampFormat = "2006-01-02 15:04:05"
type skaffoldWriter struct {
MainWriter io.Writer
EventWriter io.Writer
task constants.Phase
subtask string
timestamps bool
}
func (s skaffoldWriter) Write(p []byte) (int, error) {
written := 0
if s.timestamps {
t, err := s.MainWriter.Write([]byte(time.Now().Format(timestampFormat) + " "))
if err != nil {
return t, err
}
written += t
}
n, err := s.MainWriter.Write(p)
if err != nil {
return n, err
}
if n != len(p) {
return n, io.ErrShortWrite
}
written += n
s.EventWriter.Write(p)
return written, nil
}
func GetWriter(ctx context.Context, out io.Writer, defaultColor int, forceColors bool, timestamps bool) io.Writer {
if _, isSW := out.(skaffoldWriter); isSW {
return out
}
return skaffoldWriter{
MainWriter: SetupColors(ctx, out, defaultColor, forceColors),
EventWriter: eventV2.NewLogger(constants.DevLoop, "-1"),
timestamps: timestamps,
}
}
// GetUnderlyingWriter returns the underlying writer if out is a colorableWriter
func GetUnderlyingWriter(out io.Writer) io.Writer {
sw, isSW := out.(skaffoldWriter)
if isSW {
out = sw.MainWriter
}
cw, isCW := out.(colorableWriter)
if isCW {
out = cw.Writer
}
return out
}
// WithEventContext will return a new skaffoldWriter with the given parameters to be used for the event writer.
// If the passed io.Writer is not a skaffoldWriter, then it is simply returned.
func WithEventContext(ctx context.Context, out io.Writer, phase constants.Phase, subtaskID string) (io.Writer, context.Context) {
ctx = context.WithValue(ctx, log.ContextKey, log.EventContext{
Task: phase,
Subtask: subtaskID,
})
if sw, isSW := out.(skaffoldWriter); isSW {
return skaffoldWriter{
MainWriter: sw.MainWriter,
EventWriter: eventV2.NewLogger(phase, subtaskID),
task: phase,
subtask: subtaskID,
timestamps: sw.timestamps,
}, ctx
}
return out, ctx
}