github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/internal/middleware/logging.go (about)

     1  package middleware
     2  
     3  import (
     4  	"github.com/apex/log"
     5  	"github.com/apex/log/handlers/cli"
     6  	"github.com/fatih/color"
     7  	"github.com/goreleaser/goreleaser/pkg/context"
     8  )
     9  
    10  // Padding is a logging initial padding.
    11  type Padding int
    12  
    13  // DefaultInitialPadding is the default padding in the log library.
    14  const DefaultInitialPadding Padding = 3
    15  
    16  // ExtraPadding is the double of the DefaultInitialPadding.
    17  const ExtraPadding Padding = DefaultInitialPadding * 2
    18  
    19  // Logging pretty prints the given action and its title.
    20  // You can have different padding levels by providing different initial
    21  // paddings. The middleware will print the title in the given padding and the
    22  // action logs in padding+default padding.
    23  // The default padding in the log library is 3.
    24  // The middleware always resets to the default padding.
    25  func Logging(title string, next Action, padding Padding) Action {
    26  	return func(ctx *context.Context) error {
    27  		defer func() {
    28  			cli.Default.Padding = int(DefaultInitialPadding)
    29  		}()
    30  		cli.Default.Padding = int(padding)
    31  		log.Infof(color.New(color.Bold).Sprint(title))
    32  		cli.Default.Padding = int(padding + DefaultInitialPadding)
    33  		return next(ctx)
    34  	}
    35  }