Skip to content
Snippets Groups Projects
Unverified Commit 87e152c0 authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Refactor formatter (decrease complexity, set formatting on start)

parent 57c56dab
No related branches found
No related tags found
No related merge requests found
......@@ -39,6 +39,8 @@ func main() {
}()
app := cli.NewApp()
cli_helpers.LogRuntimePlatform(app)
cli_helpers.SetupLogLevelOptions(app)
formatter.SetRunnerFormatter(app)
app.Name = path.Base(os.Args[0])
......@@ -50,7 +52,6 @@ func main() {
Email: "ayufan@ayufan.eu",
},
}
cli_helpers.SetupLogLevelOptions(app)
app.Commands = common.GetCommands()
app.CommandNotFound = func(context *cli.Context, command string) {
logrus.Fatalln("Command", command, "not found")
......
......@@ -40,32 +40,7 @@ func (f *RunnerTextFormatter) Format(entry *logrus.Entry) ([]byte, error) {
}
func (f *RunnerTextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys []string) {
var levelColor, levelText string
switch entry.Level {
case logrus.DebugLevel:
levelColor = helpers.ANSI_BOLD_WHITE
case logrus.WarnLevel:
levelColor = helpers.ANSI_YELLOW
levelText = "WARNING: "
case logrus.ErrorLevel:
levelColor = helpers.ANSI_BOLD_RED
levelText = "ERROR: "
case logrus.FatalLevel:
levelColor = helpers.ANSI_BOLD_RED
levelText = "FATAL: "
case logrus.PanicLevel:
levelColor = helpers.ANSI_BOLD_RED
levelText = "PANIC: "
default:
}
resetColor := helpers.ANSI_RESET
if f.DisableColors == true && f.ForceColors != true {
levelColor = ""
resetColor = ""
}
levelColor, levelText, resetColor := f.getLevelColorAndText(entry.Level)
indentLength := 50 - len(levelText)
fmt.Fprintf(b, "%s%s%-*s%s ", levelColor, levelText, indentLength, entry.Message, resetColor)
......@@ -75,7 +50,36 @@ func (f *RunnerTextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry,
}
}
func (f *RunnerTextFormatter) getLevelColorAndText(level logrus.Level) (lvlColor, lvlText, resetColor string) {
color := map[logrus.Level]string{
logrus.DebugLevel: helpers.ANSI_BOLD_WHITE,
logrus.WarnLevel: helpers.ANSI_YELLOW,
logrus.ErrorLevel: helpers.ANSI_BOLD_RED,
logrus.FatalLevel: helpers.ANSI_BOLD_RED,
logrus.PanicLevel: helpers.ANSI_BOLD_RED,
}
text := map[logrus.Level]string{
logrus.DebugLevel: "",
logrus.WarnLevel: "WARNING: ",
logrus.ErrorLevel: "ERROR: ",
logrus.FatalLevel: "FATAL: ",
logrus.PanicLevel: "PANIC: ",
}
if f.DisableColors == true && f.ForceColors != true {
return
}
lvlText = text[level]
lvlColor = color[level]
resetColor = helpers.ANSI_RESET
return
}
func SetRunnerFormatter(app *cli.App) {
logrus.SetFormatter(&RunnerTextFormatter{})
newFlags := []cli.Flag{
cli.BoolFlag{
Name: "no-color",
......@@ -100,6 +104,7 @@ func SetRunnerFormatter(app *cli.App) {
if appBefore != nil {
return appBefore(c)
}
return nil
}
}
......@@ -47,13 +47,17 @@ func main() {
}
}()
app := cli.NewApp()
formatter.SetRunnerFormatter(app)
// Start background reaping of orphaned child processes.
// It allows the gitlab-runner to act as `init` process
go helpers.Reap()
app := cli.NewApp()
cli_helpers.LogRuntimePlatform(app)
cli_helpers.SetupLogLevelOptions(app)
cli_helpers.SetupCPUProfile(app)
cli_helpers.FixHOME(app)
formatter.SetRunnerFormatter(app)
app.Name = path.Base(os.Args[0])
app.Usage = "a GitLab Runner"
cli.VersionPrinter = common.VersionPrinter
......@@ -63,10 +67,6 @@ func main() {
Email: "ayufan@ayufan.eu",
},
}
cli_helpers.LogRuntimePlatform(app)
cli_helpers.SetupLogLevelOptions(app)
cli_helpers.SetupCPUProfile(app)
cli_helpers.FixHOME(app)
app.Commands = common.GetCommands()
app.CommandNotFound = func(context *cli.Context, command string) {
logrus.Fatalln("Command", command, "not found.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment