diff --git a/go.mod b/go.mod index ff9df1b..d428f6c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module codeberg.org/danjones000/nomino go 1.23.6 + +require github.com/stretchr/testify v1.10.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..713a0b4 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/options.go b/options.go new file mode 100644 index 0000000..395925f --- /dev/null +++ b/options.go @@ -0,0 +1,52 @@ +package nomino + +import "strings" + +type generator func() string + +type config struct { + original string + prefix string + suffix string + extension string + generator generator +} + +func defaultConf() config { + return config{ + extension: ".txt", + generator: func() string { return "abc" }, + } +} + +type Option func(c *config) + +func WithOriginal(o string) Option { + return func(c *config) { + c.original = "_" + o + } +} + +func WithPrefix(p string) Option { + return func(c *config) { + c.prefix = p + "_" + } +} + +func WithSuffix(s string) Option { + return func(c *config) { + c.suffix = "_" + s + } +} + +func WithoutExtension() Option { + return func(c *config) { + c.extension = "" + } +} + +func WithExtension(ext string) Option { + return func(c *config) { + c.extension = "." + strings.TrimPrefix(ext, ".") + } +} diff --git a/options_test.go b/options_test.go new file mode 100644 index 0000000..1c7486b --- /dev/null +++ b/options_test.go @@ -0,0 +1,48 @@ +package nomino + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDefaultConf(t *testing.T) { + c := defaultConf() + assert.Equal(t, ".txt", c.extension) + // assert.Nil(t, c.generator) + assert.Equal(t, "abc", c.generator()) +} + +func TestWithOriginal(t *testing.T) { + var c config + name := "foobar" + WithOriginal(name)(&c) + assert.Equal(t, "_"+name, c.original) +} + +func TestWithPrefix(t *testing.T) { + var c config + pref := "draft" + WithPrefix(pref)(&c) + assert.Equal(t, pref+"_", c.prefix) +} + +func TestWithSuffix(t *testing.T) { + var c config + suff := "out" + WithSuffix(suff)(&c) + assert.Equal(t, "_"+suff, c.suffix) +} + +func TestWithoutExtension(t *testing.T) { + c := config{extension: ".foobar"} + WithoutExtension()(&c) + assert.Equal(t, "", c.extension) +} + +func TestWithExtension(t *testing.T) { + var c config + ext := "yaml" + WithExtension(ext)(&c) + assert.Equal(t, "."+ext, c.extension) +}