Skip to content

Commit

Permalink
feat: add deprecation notice interface (#399)
Browse files Browse the repository at this point in the history
* feat: add deprecation notice interface

* fix: use a simple writer

* fix: use println
  • Loading branch information
caarlos0 authored Nov 13, 2021
1 parent 5e665bd commit 2a86957
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
3 changes: 2 additions & 1 deletion deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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>"
}
}
Expand Down
31 changes: 31 additions & 0 deletions deprecation/deprecation.go
Original file line number Diff line number Diff line change
@@ -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...)
}
18 changes: 18 additions & 0 deletions deprecation/deprecation_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
5 changes: 3 additions & 2 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2a86957

Please sign in to comment.