2020-02-07 00:17:58 -06:00
|
|
|
package runner
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-02-11 11:10:35 -06:00
|
|
|
"context"
|
2019-01-12 22:45:25 -06:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2020-02-12 01:38:30 -06:00
|
|
|
"sync"
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-11 11:10:35 -06:00
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-01-12 00:39:43 -06:00
|
|
|
"golang.org/x/term"
|
2019-01-12 22:45:25 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-12 00:39:43 -06:00
|
|
|
// nocolor = 0
|
2020-02-11 11:10:35 -06:00
|
|
|
red = 31
|
|
|
|
green = 32
|
|
|
|
yellow = 33
|
|
|
|
blue = 34
|
|
|
|
magenta = 35
|
|
|
|
cyan = 36
|
|
|
|
gray = 37
|
2019-01-12 22:45:25 -06:00
|
|
|
)
|
|
|
|
|
2020-02-11 11:10:35 -06:00
|
|
|
var colors []int
|
|
|
|
var nextColor int
|
2020-02-12 01:38:30 -06:00
|
|
|
var mux sync.Mutex
|
2020-02-11 11:10:35 -06:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
nextColor = 0
|
|
|
|
colors = []int{
|
|
|
|
blue, yellow, green, magenta, red, gray, cyan,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithJobLogger attaches a new logger to context that is aware of steps
|
2021-01-12 00:28:45 -06:00
|
|
|
func WithJobLogger(ctx context.Context, jobName string, secrets map[string]string, insecureSecrets bool) context.Context {
|
2020-02-12 01:38:30 -06:00
|
|
|
mux.Lock()
|
|
|
|
defer mux.Unlock()
|
2020-02-11 11:10:35 -06:00
|
|
|
formatter := new(stepLogFormatter)
|
|
|
|
formatter.color = colors[nextColor%len(colors)]
|
2020-05-13 17:22:31 -05:00
|
|
|
formatter.secrets = secrets
|
2021-01-12 00:28:45 -06:00
|
|
|
formatter.insecureSecrets = insecureSecrets
|
2020-02-12 01:38:30 -06:00
|
|
|
nextColor++
|
2020-02-11 11:10:35 -06:00
|
|
|
|
2021-11-19 08:42:39 -06:00
|
|
|
logger := logrus.New()
|
2021-11-27 11:45:56 -06:00
|
|
|
if common.TestContext(ctx) {
|
|
|
|
fieldLogger := common.Logger(ctx)
|
|
|
|
if fieldLogger != nil {
|
|
|
|
logger = fieldLogger.(*logrus.Logger)
|
|
|
|
}
|
|
|
|
}
|
2020-02-11 11:10:35 -06:00
|
|
|
logger.SetFormatter(formatter)
|
2019-02-18 18:30:34 -06:00
|
|
|
logger.SetOutput(os.Stdout)
|
2019-01-12 22:45:25 -06:00
|
|
|
logger.SetLevel(logrus.GetLevel())
|
2020-02-11 11:10:35 -06:00
|
|
|
rtn := logger.WithFields(logrus.Fields{"job": jobName, "dryrun": common.Dryrun(ctx)})
|
|
|
|
|
|
|
|
return common.WithLogger(ctx, rtn)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2020-02-11 11:10:35 -06:00
|
|
|
type stepLogFormatter struct {
|
2021-01-12 00:28:45 -06:00
|
|
|
color int
|
|
|
|
secrets map[string]string
|
|
|
|
insecureSecrets bool
|
2020-02-07 00:17:58 -06:00
|
|
|
}
|
|
|
|
|
2020-02-11 11:10:35 -06:00
|
|
|
func (f *stepLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
2019-01-12 22:45:25 -06:00
|
|
|
b := &bytes.Buffer{}
|
|
|
|
|
2021-01-12 00:28:45 -06:00
|
|
|
// Replace any secrets in the entry if insecure-secrets flag is not used
|
|
|
|
if !f.insecureSecrets {
|
|
|
|
for _, v := range f.secrets {
|
2021-06-07 08:54:12 -05:00
|
|
|
if v != "" {
|
|
|
|
entry.Message = strings.ReplaceAll(entry.Message, v, "***")
|
|
|
|
}
|
2021-01-12 00:28:45 -06:00
|
|
|
}
|
2020-05-13 17:22:31 -05:00
|
|
|
}
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
if f.isColored(entry) {
|
|
|
|
f.printColored(b, entry)
|
|
|
|
} else {
|
|
|
|
f.print(b, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.WriteByte('\n')
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-02-11 11:10:35 -06:00
|
|
|
func (f *stepLogFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry) {
|
2019-01-12 22:45:25 -06:00
|
|
|
entry.Message = strings.TrimSuffix(entry.Message, "\n")
|
2020-02-11 11:10:35 -06:00
|
|
|
jobName := entry.Data["job"]
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-12 01:38:30 -06:00
|
|
|
if entry.Data["raw_output"] == true {
|
|
|
|
fmt.Fprintf(b, "\x1b[%dm|\x1b[0m %s", f.color, entry.Message)
|
|
|
|
} else if entry.Data["dryrun"] == true {
|
2020-02-11 11:10:35 -06:00
|
|
|
fmt.Fprintf(b, "\x1b[1m\x1b[%dm\x1b[7m*DRYRUN*\x1b[0m \x1b[%dm[%s] \x1b[0m%s", gray, f.color, jobName, entry.Message)
|
2019-01-12 22:45:25 -06:00
|
|
|
} else {
|
2020-02-11 11:10:35 -06:00
|
|
|
fmt.Fprintf(b, "\x1b[%dm[%s] \x1b[0m%s", f.color, jobName, entry.Message)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 11:10:35 -06:00
|
|
|
func (f *stepLogFormatter) print(b *bytes.Buffer, entry *logrus.Entry) {
|
2019-01-12 22:45:25 -06:00
|
|
|
entry.Message = strings.TrimSuffix(entry.Message, "\n")
|
2020-02-11 11:10:35 -06:00
|
|
|
jobName := entry.Data["job"]
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-02-12 01:38:30 -06:00
|
|
|
if entry.Data["raw_output"] == true {
|
|
|
|
fmt.Fprintf(b, "[%s] | %s", jobName, entry.Message)
|
|
|
|
} else if entry.Data["dryrun"] == true {
|
2020-02-07 00:17:58 -06:00
|
|
|
fmt.Fprintf(b, "*DRYRUN* [%s] %s", jobName, entry.Message)
|
2019-01-12 22:45:25 -06:00
|
|
|
} else {
|
2020-02-07 00:17:58 -06:00
|
|
|
fmt.Fprintf(b, "[%s] %s", jobName, entry.Message)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 11:10:35 -06:00
|
|
|
func (f *stepLogFormatter) isColored(entry *logrus.Entry) bool {
|
2019-01-12 22:45:25 -06:00
|
|
|
isColored := checkIfTerminal(entry.Logger.Out)
|
|
|
|
|
|
|
|
if force, ok := os.LookupEnv("CLICOLOR_FORCE"); ok && force != "0" {
|
|
|
|
isColored = true
|
|
|
|
} else if ok && force == "0" {
|
|
|
|
isColored = false
|
|
|
|
} else if os.Getenv("CLICOLOR") == "0" {
|
|
|
|
isColored = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return isColored
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkIfTerminal(w io.Writer) bool {
|
|
|
|
switch v := w.(type) {
|
|
|
|
case *os.File:
|
2021-01-12 00:39:43 -06:00
|
|
|
return term.IsTerminal(int(v.Fd()))
|
2019-01-12 22:45:25 -06:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|