2021-09-18 10:21:29 -03:00
|
|
|
package logging
|
2019-01-22 01:56:16 -02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/apex/log"
|
|
|
|
"github.com/apex/log/handlers/cli"
|
|
|
|
"github.com/fatih/color"
|
2021-09-18 10:21:29 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/middleware"
|
2019-01-22 01:56:16 -02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Padding is a logging initial padding.
|
|
|
|
type Padding int
|
|
|
|
|
|
|
|
// DefaultInitialPadding is the default padding in the log library.
|
|
|
|
const DefaultInitialPadding Padding = 3
|
|
|
|
|
|
|
|
// ExtraPadding is the double of the DefaultInitialPadding.
|
2021-04-19 09:31:57 -03:00
|
|
|
const ExtraPadding = DefaultInitialPadding * 2
|
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.
|
2019-01-22 01:56:16 -02:00
|
|
|
// You can have different padding levels by providing different initial
|
|
|
|
// paddings. The middleware will print the title in the given padding and the
|
|
|
|
// action logs in padding+default padding.
|
|
|
|
// The default padding in the log library is 3.
|
|
|
|
// The middleware always resets to the default padding.
|
2021-09-18 10:21:29 -03:00
|
|
|
func Log(title string, next middleware.Action, padding Padding) middleware.Action {
|
2019-01-22 01:56:16 -02:00
|
|
|
return func(ctx *context.Context) error {
|
|
|
|
defer func() {
|
|
|
|
cli.Default.Padding = int(DefaultInitialPadding)
|
|
|
|
}()
|
|
|
|
cli.Default.Padding = int(padding)
|
2020-03-06 01:25:09 -03:00
|
|
|
log.Infof(color.New(color.Bold).Sprint(title))
|
2019-01-22 01:56:16 -02:00
|
|
|
cli.Default.Padding = int(padding + DefaultInitialPadding)
|
|
|
|
return next(ctx)
|
|
|
|
}
|
|
|
|
}
|