2021-09-18 10:21:29 -03:00
|
|
|
package logging
|
2019-01-22 01:56:16 -02:00
|
|
|
|
|
|
|
import (
|
2022-06-22 00:09:11 -03:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
2024-05-26 15:02:57 -03:00
|
|
|
"github.com/goreleaser/goreleaser/v2/internal/middleware"
|
|
|
|
"github.com/goreleaser/goreleaser/v2/pkg/context"
|
2019-01-22 01:56:16 -02:00
|
|
|
)
|
|
|
|
|
2022-06-22 00:09:11 -03:00
|
|
|
var (
|
|
|
|
bold = lipgloss.NewStyle().Bold(true)
|
|
|
|
faint = lipgloss.NewStyle().Italic(true).Faint(true)
|
|
|
|
)
|
2019-01-22 01:56:16 -02:00
|
|
|
|
2021-09-18 10:21:29 -03:00
|
|
|
// Log pretty prints the given action and its title.
|
2022-06-21 21:11:15 -03:00
|
|
|
func Log(title string, next middleware.Action) middleware.Action {
|
2019-01-22 01:56:16 -02:00
|
|
|
return func(ctx *context.Context) error {
|
2022-06-22 00:09:11 -03:00
|
|
|
start := time.Now()
|
2019-01-22 01:56:16 -02:00
|
|
|
defer func() {
|
2022-10-05 09:33:15 -03:00
|
|
|
logDuration(start)
|
2022-06-21 21:11:15 -03:00
|
|
|
log.ResetPadding()
|
2019-01-22 01:56:16 -02:00
|
|
|
}()
|
2024-06-22 22:43:57 -03:00
|
|
|
if title != "" {
|
|
|
|
log.Infof(bold.Render(title))
|
|
|
|
log.IncreasePadding()
|
|
|
|
}
|
2022-06-21 21:11:15 -03:00
|
|
|
return next(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PadLog pretty prints the given action and its title with an increased padding.
|
|
|
|
func PadLog(title string, next middleware.Action) middleware.Action {
|
|
|
|
return func(ctx *context.Context) error {
|
2022-10-05 09:33:15 -03:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
logDuration(start)
|
|
|
|
log.ResetPadding()
|
|
|
|
}()
|
2022-09-27 21:03:56 -03:00
|
|
|
log.ResetPadding()
|
2022-06-21 21:11:15 -03:00
|
|
|
log.IncreasePadding()
|
|
|
|
log.Infof(bold.Render(title))
|
|
|
|
log.IncreasePadding()
|
2019-01-22 01:56:16 -02:00
|
|
|
return next(ctx)
|
|
|
|
}
|
|
|
|
}
|
2022-10-05 09:33:15 -03:00
|
|
|
|
|
|
|
func logDuration(start time.Time) {
|
2024-06-22 22:43:57 -03:00
|
|
|
if took := time.Since(start).Round(time.Second); took > 10*time.Second {
|
2022-10-05 09:33:15 -03:00
|
|
|
log.Info(faint.Render(fmt.Sprintf("took: %s", took)))
|
|
|
|
}
|
|
|
|
}
|