♻️ Export Config
This commit is contained in:
parent
5c4e66d144
commit
7bd5503613
6 changed files with 56 additions and 38 deletions
20
config.go
Normal file
20
config.go
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
package nomino
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
original string
|
||||||
|
prefix string
|
||||||
|
suffix string
|
||||||
|
extension string
|
||||||
|
generator Generator
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfig(options ...Option) Config {
|
||||||
|
conf := Config{
|
||||||
|
extension: ".txt",
|
||||||
|
generator: uuidGen,
|
||||||
|
}
|
||||||
|
for _, opt := range options {
|
||||||
|
opt(&conf)
|
||||||
|
}
|
||||||
|
return conf
|
||||||
|
}
|
||||||
22
config_test.go
Normal file
22
config_test.go
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
package nomino
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewConf(t *testing.T) {
|
||||||
|
c := NewConfig()
|
||||||
|
assert.Equal(t, ".txt", c.extension)
|
||||||
|
st, _ := c.generator()
|
||||||
|
_, parseErr := uuid.Parse(st)
|
||||||
|
assert.NoError(t, parseErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewConfWithOpts(t *testing.T) {
|
||||||
|
c := NewConfig(WithoutExtension(), WithPrefix("foobar"))
|
||||||
|
assert.Equal(t, "", c.extension)
|
||||||
|
assert.Equal(t, "foobar_", c.prefix)
|
||||||
|
}
|
||||||
|
|
@ -14,7 +14,7 @@ type Generator func() (string, error)
|
||||||
|
|
||||||
// WithGenerator sets the specified generator
|
// WithGenerator sets the specified generator
|
||||||
func WithGenerator(g Generator) Option {
|
func WithGenerator(g Generator) Option {
|
||||||
return func(c *config) {
|
return func(c *Config) {
|
||||||
c.generator = g
|
c.generator = g
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import (
|
||||||
|
|
||||||
func TestWithGenerator(t *testing.T) {
|
func TestWithGenerator(t *testing.T) {
|
||||||
g := func() (string, error) { return "abc", nil }
|
g := func() (string, error) { return "abc", nil }
|
||||||
var c config
|
var c Config
|
||||||
WithGenerator(g)(&c)
|
WithGenerator(g)(&c)
|
||||||
st, err := c.generator()
|
st, err := c.generator()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
|
||||||
29
options.go
29
options.go
|
|
@ -2,56 +2,41 @@ package nomino
|
||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
type config struct {
|
// Option sets configuration parameters for Config.
|
||||||
original string
|
type Option func(c *Config)
|
||||||
prefix string
|
|
||||||
suffix string
|
|
||||||
extension string
|
|
||||||
generator Generator
|
|
||||||
}
|
|
||||||
|
|
||||||
func defaultConf() config {
|
|
||||||
return config{
|
|
||||||
extension: ".txt",
|
|
||||||
generator: uuidGen,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Option is an option for nomino
|
|
||||||
type Option func(c *config)
|
|
||||||
|
|
||||||
// WithOriginal sets the original filename.
|
// WithOriginal sets the original filename.
|
||||||
// This will be included in the generated name after the generated string and before the suffix.
|
// This will be included in the generated name after the generated string and before the suffix.
|
||||||
func WithOriginal(o string) Option {
|
func WithOriginal(o string) Option {
|
||||||
return func(c *config) {
|
return func(c *Config) {
|
||||||
c.original = "_" + o
|
c.original = "_" + o
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithPrefix sets a prefix for the generated name.
|
// WithPrefix sets a prefix for the generated name.
|
||||||
func WithPrefix(p string) Option {
|
func WithPrefix(p string) Option {
|
||||||
return func(c *config) {
|
return func(c *Config) {
|
||||||
c.prefix = p + "_"
|
c.prefix = p + "_"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithSuffix sets a suffix for the generated name. It will be included in the base name before the suffix.
|
// WithSuffix sets a suffix for the generated name. It will be included in the base name before the suffix.
|
||||||
func WithSuffix(s string) Option {
|
func WithSuffix(s string) Option {
|
||||||
return func(c *config) {
|
return func(c *Config) {
|
||||||
c.suffix = "_" + s
|
c.suffix = "_" + s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithoutExtension sets no extension for the generated filename. By default, it will be txt
|
// WithoutExtension sets no extension for the generated filename. By default, it will be txt
|
||||||
func WithoutExtension() Option {
|
func WithoutExtension() Option {
|
||||||
return func(c *config) {
|
return func(c *Config) {
|
||||||
c.extension = ""
|
c.extension = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithExtension sets the extension for the generated filename.
|
// WithExtension sets the extension for the generated filename.
|
||||||
func WithExtension(ext string) Option {
|
func WithExtension(ext string) Option {
|
||||||
return func(c *config) {
|
return func(c *Config) {
|
||||||
c.extension = "." + strings.TrimPrefix(ext, ".")
|
c.extension = "." + strings.TrimPrefix(ext, ".")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,47 +3,38 @@ package nomino
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDefaultConf(t *testing.T) {
|
|
||||||
c := defaultConf()
|
|
||||||
assert.Equal(t, ".txt", c.extension)
|
|
||||||
st, _ := c.generator()
|
|
||||||
_, parseErr := uuid.Parse(st)
|
|
||||||
assert.NoError(t, parseErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithOriginal(t *testing.T) {
|
func TestWithOriginal(t *testing.T) {
|
||||||
var c config
|
var c Config
|
||||||
name := "foobar"
|
name := "foobar"
|
||||||
WithOriginal(name)(&c)
|
WithOriginal(name)(&c)
|
||||||
assert.Equal(t, "_"+name, c.original)
|
assert.Equal(t, "_"+name, c.original)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithPrefix(t *testing.T) {
|
func TestWithPrefix(t *testing.T) {
|
||||||
var c config
|
var c Config
|
||||||
pref := "draft"
|
pref := "draft"
|
||||||
WithPrefix(pref)(&c)
|
WithPrefix(pref)(&c)
|
||||||
assert.Equal(t, pref+"_", c.prefix)
|
assert.Equal(t, pref+"_", c.prefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithSuffix(t *testing.T) {
|
func TestWithSuffix(t *testing.T) {
|
||||||
var c config
|
var c Config
|
||||||
suff := "out"
|
suff := "out"
|
||||||
WithSuffix(suff)(&c)
|
WithSuffix(suff)(&c)
|
||||||
assert.Equal(t, "_"+suff, c.suffix)
|
assert.Equal(t, "_"+suff, c.suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithoutExtension(t *testing.T) {
|
func TestWithoutExtension(t *testing.T) {
|
||||||
c := config{extension: ".foobar"}
|
c := Config{extension: ".foobar"}
|
||||||
WithoutExtension()(&c)
|
WithoutExtension()(&c)
|
||||||
assert.Equal(t, "", c.extension)
|
assert.Equal(t, "", c.extension)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithExtension(t *testing.T) {
|
func TestWithExtension(t *testing.T) {
|
||||||
var c config
|
var c Config
|
||||||
ext := "yaml"
|
ext := "yaml"
|
||||||
WithExtension(ext)(&c)
|
WithExtension(ext)(&c)
|
||||||
assert.Equal(t, "."+ext, c.extension)
|
assert.Equal(t, "."+ext, c.extension)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue