2018-02-17 13:43:29 -02:00
|
|
|
// Package deprecate provides simple functions to standardize the output
|
|
|
|
// of deprecation notices on goreleaser
|
|
|
|
package deprecate
|
|
|
|
|
|
|
|
import (
|
2021-03-01 14:18:57 -03:00
|
|
|
"bytes"
|
2021-11-13 22:23:11 -03:00
|
|
|
"io"
|
2018-02-17 13:43:29 -02:00
|
|
|
"strings"
|
2021-11-13 22:23:11 -03:00
|
|
|
"sync"
|
2021-03-01 14:18:57 -03:00
|
|
|
"text/template"
|
2018-02-17 13:43:29 -02:00
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
"github.com/apex/log/handlers/cli"
|
|
|
|
"github.com/fatih/color"
|
2020-04-12 14:31:35 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2018-02-17 13:43:29 -02:00
|
|
|
)
|
|
|
|
|
2018-04-30 20:58:10 -07:00
|
|
|
const baseURL = "https://goreleaser.com/deprecations#"
|
2018-02-17 13:43:29 -02:00
|
|
|
|
2021-11-13 22:23:11 -03:00
|
|
|
// NewWriter return a io.Writer that notices deprecations.
|
|
|
|
func NewWriter(ctx *context.Context) io.Writer {
|
|
|
|
return &writer{ctx: ctx}
|
|
|
|
}
|
|
|
|
|
|
|
|
type writer struct {
|
|
|
|
ctx *context.Context
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Write(p []byte) (n int, err error) {
|
|
|
|
w.once.Do(func() {
|
|
|
|
w.ctx.Deprecated = true
|
|
|
|
})
|
|
|
|
log.Warn(color.New(color.Bold, color.FgHiYellow).Sprintf("DEPRECATED: " + strings.TrimSuffix(string(p), "\n")))
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Notice warns the user about the deprecation of the given property.
|
2020-04-12 14:31:35 -03:00
|
|
|
func Notice(ctx *context.Context, property string) {
|
2021-03-01 14:18:57 -03:00
|
|
|
NoticeCustom(ctx, property, "`{{ .Property }}` should not be used anymore, check {{ .URL }} for more info")
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:49:49 -03:00
|
|
|
// NoticeCustom warns the user about the deprecation of the given property.
|
2021-03-01 14:18:57 -03:00
|
|
|
func NoticeCustom(ctx *context.Context, property, tmpl string) {
|
2020-04-12 14:31:35 -03:00
|
|
|
ctx.Deprecated = true
|
2022-02-02 15:31:11 -03:00
|
|
|
// XXX: this is very ugly!
|
2022-04-02 10:41:05 -03:00
|
|
|
oldHandler, ok := log.Log.(*log.Logger).Handler.(*cli.Handler)
|
|
|
|
if !ok {
|
|
|
|
// probably in a test, and the cli logger wasn't set
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w := oldHandler.Writer
|
2022-02-02 15:31:11 -03:00
|
|
|
handler := cli.New(w)
|
|
|
|
handler.Padding = cli.Default.Padding + 3
|
|
|
|
log := &log.Logger{
|
|
|
|
Handler: handler,
|
|
|
|
Level: log.InfoLevel,
|
|
|
|
}
|
2018-04-30 20:58:10 -07:00
|
|
|
// replaces . and _ with -
|
|
|
|
url := baseURL + strings.NewReplacer(
|
2020-07-06 14:48:17 +01:00
|
|
|
".", "",
|
|
|
|
"_", "",
|
2022-03-17 07:55:17 -03:00
|
|
|
":", "",
|
|
|
|
" ", "-",
|
2018-04-30 20:58:10 -07:00
|
|
|
).Replace(property)
|
2021-03-01 14:18:57 -03:00
|
|
|
var out bytes.Buffer
|
|
|
|
if err := template.Must(template.New("deprecation").Parse("DEPRECATED: "+tmpl)).Execute(&out, templateData{
|
|
|
|
URL: url,
|
|
|
|
Property: property,
|
|
|
|
}); err != nil {
|
|
|
|
panic(err) // this should never happen
|
|
|
|
}
|
|
|
|
log.Warn(color.New(color.Bold, color.FgHiYellow).Sprintf(out.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
type templateData struct {
|
|
|
|
URL string
|
|
|
|
Property string
|
2018-02-17 13:43:29 -02:00
|
|
|
}
|