Repository: goreleaser/nfpm
Pull request #399 (2021-11-12 02:19:24)

feat: add deprecation notice interface
as discussed in another issue

this would allow, for example, goreleaser, to set its own deprecation notice "provider" to nfpm...

codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/399?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#399](https://codecov.io/gh/goreleaser/nfpm/pull/399?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (9a92a5d) into [master](https://codecov.io/gh/goreleaser/nfpm/commit/b909114591c2c27623d1bf1fd5e8f311885207cf?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (b909114) will **increase** coverage by `0.20%`.
> The diff coverage is `85.71%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/nfpm/pull/399/graphs/tree.svg?width=650&height=150&src=pr&token=9QKGIxn7wg&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/nfpm/pull/399?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master     #399      +/-   ##
==========================================
+ Coverage   65.25%   65.46%   +0.20%     
==========================================
  Files          14       15       +1     
  Lines        1842     1853      +11     
==========================================
+ Hits         1202     1213      +11     
  Misses        502      502              
  Partials      138      138              
```


| [Impacted Files](https://codecov.io/gh/goreleaser/nfpm/pull/399?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [nfpm.go](https://codecov.io/gh/goreleaser/nfpm/pull/399/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-bmZwbS5nbw==) | `80.72% <0.00%> (ø)` | |
| [deb/deb.go](https://codecov.io/gh/goreleaser/nfpm/pull/399/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-ZGViL2RlYi5nbw==) | `68.90% <100.00%> (ø)` | |
| [deprecation/deprecation.go](https://codecov.io/gh/goreleaser/nfpm/pull/399/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-ZGVwcmVjYXRpb24vZGVwcmVjYXRpb24uZ28=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/399?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/nfpm/pull/399?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [b909114...9a92a5d](https://codecov.io/gh/goreleaser/nfpm/pull/399?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).

erikgeiser: I intuition tells me to simplify it. What's the advantage compared to this?
```go
var DeprecationNoticeWriter io.Writer = os.Stderr

 // Notice will notice a deprecation with the previously set noticer.
func Notice(s string) {
     DeprecationNoticeWriter.Write(s)
 }

func Noticef(format string, a ...interface{}) {
    DeprecationNoticeWriter.Write(fmt.Sprintf(format, a...)
}
```
caarlos0: on my impl it would be easier to use, say, a logger library... but its fair, the caller can always implement a simple writer that logs...

will change it :) 

Patch:
diff --git a/deb/deb.go b/deb/deb.go
index ede009c..b502fa2 100644
--- a/deb/deb.go
+++ b/deb/deb.go
@@ -19,6 +19,7 @@ import (
 	"github.com/blakesmith/ar"
 	"github.com/goreleaser/chglog"
 	"github.com/goreleaser/nfpm/v2"
+	"github.com/goreleaser/nfpm/v2/deprecation"
 	"github.com/goreleaser/nfpm/v2/files"
 	"github.com/goreleaser/nfpm/v2/internal/sign"
 	"github.com/ulikunitz/xz"
@@ -170,7 +171,7 @@ func (*Deb) SetPackagerDefaults(info *nfpm.Info) {
 	// if in the long run we should be more strict about this and error when
 	// not set?
 	if info.Maintainer == "" {
-		fmt.Fprintf(os.Stderr, "DEPRECATION WARNING: Leaving the 'maintainer' field unset will not be allowed in a future version")
+		deprecation.Println("Leaving the 'maintainer' field unset will not be allowed in a future version")
 		info.Maintainer = "Unset Maintainer <unset@localhost>"
 	}
 }
diff --git a/deprecation/deprecation.go b/deprecation/deprecation.go
new file mode 100644
index 0000000..aab9ada
--- /dev/null
+++ b/deprecation/deprecation.go
@@ -0,0 +1,31 @@
+// Package deprecation provides centralized deprecation notice messaging for nfpm.
+package deprecation
+
+import (
+	"fmt"
+	"io"
+	"os"
+)
+
+type prefixed struct{ io.Writer }
+
+func (p prefixed) Write(b []byte) (int, error) {
+	return p.Writer.Write(append([]byte("DEPRECATION WARNING: "), b...))
+}
+
+var Noticer io.Writer = prefixed{os.Stderr}
+
+// Print prints the given string to the Noticer.
+func Print(s string) {
+	fmt.Fprint(Noticer, s)
+}
+
+// Println printslns the given string to the Noticer.
+func Println(s string) {
+	fmt.Fprintln(Noticer, s)
+}
+
+// Printf printfs the given string to the Noticer.
+func Printf(format string, a ...interface{}) {
+	fmt.Fprintf(Noticer, format, a...)
+}
diff --git a/nfpm.go b/nfpm.go
index be5df6d..848c618 100644
--- a/nfpm.go
+++ b/nfpm.go
@@ -11,6 +11,7 @@ import (
 	"github.com/AlekSi/pointer"
 	"github.com/Masterminds/semver/v3"
 	"github.com/goreleaser/chglog"
+	"github.com/goreleaser/nfpm/v2/deprecation"
 	"github.com/goreleaser/nfpm/v2/files"
 	"github.com/imdario/mergo"
 	"gopkg.in/yaml.v3"
@@ -375,8 +376,8 @@ func Validate(info *Info) (err error) {
 	}
 
 	if len(info.EmptyFolders) > 0 {
-		fmt.Fprintf(os.Stderr, "DEPRECATION WARNING: 'empty_folders' is deprecated and "+
-			"will be removed in a future version, create content with type 'dir' and "+
+		deprecation.Println("'empty_folders' is deprecated and " +
+			"will be removed in a future version, create content with type 'dir' and " +
 			"directoy name as 'dst' instead")
 
 		for _, emptyFolder := range info.EmptyFolders {


Test patch:
diff --git a/deprecation/deprecation_test.go b/deprecation/deprecation_test.go
new file mode 100644
index 0000000..c5b5108
--- /dev/null
+++ b/deprecation/deprecation_test.go
@@ -0,0 +1,18 @@
+package deprecation
+
+import (
+	"bytes"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestNotice(t *testing.T) {
+	Print("before")
+	var b bytes.Buffer
+	Noticer = prefixed{&b}
+	Print("blah\n")
+	Printf("blah: %v\n", true)
+	Println("foobar")
+	require.Equal(t, "DEPRECATION WARNING: blah\nDEPRECATION WARNING: blah: true\nDEPRECATION WARNING: foobar\n", b.String())
+}