/
utils.go
93 lines (76 loc) · 1.85 KB
/
utils.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
package cre
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/fristonio/xene/pkg/defaults"
"github.com/fristonio/xene/pkg/utils"
"github.com/sirupsen/logrus"
)
// logger is the logging helper for the executor
type logger struct {
// pipelineName contains the name of the pipeline we are working on.
pipelineName string
// pipelineID contains the run ID of the pipeline in context.
pipelineID string
// task contains the name of the task
task string
step string
}
// GetLogWriter return io.WriteCloser for the command logs
func (l *logger) getLogWriter() io.WriteCloser {
taskDir := fmt.Sprintf("%s/%s/%s/%s", defaults.AgentLogsDir, l.pipelineName, l.pipelineID, l.task)
filename := fmt.Sprintf("%s/%s", taskDir, l.step)
if !utils.DirExists(taskDir) {
if err := os.MkdirAll(taskDir, os.ModePerm); err != nil {
return nil
}
}
var (
f *os.File
err error
)
if _, err = os.Stat(filename); os.IsNotExist(err) {
f, err = os.Create(filename)
if err != nil {
logrus.Errorf("error while creating log file: %s", err)
return nil
}
} else {
f, err = os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0660)
if err != nil {
logrus.Errorf("error while opening log file: %s", err)
return nil
}
}
return &loggerWriter{f, bufio.NewWriter(f)}
}
func (l *logger) getLogFileName() string {
return fmt.Sprintf("/%s/%s/%s/%s", l.pipelineName, l.pipelineID, l.task, l.step)
}
func newLogger(pName, pID, tName, sName string) *logger {
return &logger{
pName,
pID,
tName,
sName,
}
}
type loggerWriter struct {
f *os.File
*bufio.Writer
}
func (w *loggerWriter) Close() error {
if err := w.Flush(); err != nil {
return err
}
return w.f.Close()
}
func parseImageCanonicalURL(image string) string {
if strings.Contains(image, "/") {
return fmt.Sprintf("docker.io/%s", image)
}
return fmt.Sprintf("docker.io/library/%s", image)
}