Compare commits
No commits in common. "90f8d22b2374285de84557b5ebac63732de0afa4" and "25c1a6e891754b746de2fc4146913aef9b876d09" have entirely different histories.
90f8d22b23
...
25c1a6e891
8 changed files with 4 additions and 172 deletions
17
CHANGELOG.md
17
CHANGELOG.md
|
|
@ -1,24 +1,7 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
### [0.2.1] - 2025-03-14
|
|
||||||
|
|
||||||
#### Features
|
|
||||||
|
|
||||||
- Add Hash Generator
|
|
||||||
|
|
||||||
#### Dev Tooling
|
|
||||||
|
|
||||||
- Added a task to serve docs
|
|
||||||
- Added tasts to check code complexity
|
|
||||||
|
|
||||||
#### Miscellaneous
|
|
||||||
|
|
||||||
- Fixed some `go vet` complaints
|
|
||||||
|
|
||||||
### [0.2.0] - 2025-03-14
|
### [0.2.0] - 2025-03-14
|
||||||
|
|
||||||
#### Features
|
|
||||||
|
|
||||||
- Add `IncrementalFormat`* Generators
|
- Add `IncrementalFormat`* Generators
|
||||||
- Add `Slug`* Generators
|
- Add `Slug`* Generators
|
||||||
- Add `WithOriginalSlug`* Options
|
- Add `WithOriginalSlug`* Options
|
||||||
|
|
|
||||||
31
Taskfile.yml
31
Taskfile.yml
|
|
@ -26,7 +26,6 @@ tasks:
|
||||||
desc: Vet go code
|
desc: Vet go code
|
||||||
sources:
|
sources:
|
||||||
- '**/*.go'
|
- '**/*.go'
|
||||||
deps: [gen]
|
|
||||||
cmds:
|
cmds:
|
||||||
- go vet ./...
|
- go vet ./...
|
||||||
|
|
||||||
|
|
@ -44,26 +43,6 @@ tasks:
|
||||||
cmds:
|
cmds:
|
||||||
- staticcheck ./...
|
- staticcheck ./...
|
||||||
|
|
||||||
cog-complex:
|
|
||||||
desc: Calculate cognitive complexity
|
|
||||||
sources:
|
|
||||||
- '**/*.go'
|
|
||||||
cmds:
|
|
||||||
- gocognit -over 5 .
|
|
||||||
|
|
||||||
cyc-complex:
|
|
||||||
desc: Calculate cyclomatic complexity
|
|
||||||
sources:
|
|
||||||
- '**/*.go'
|
|
||||||
cmds:
|
|
||||||
- gocyclo -over 5 .
|
|
||||||
|
|
||||||
complex:
|
|
||||||
desc: Calculate complexities
|
|
||||||
deps:
|
|
||||||
- cog-complex
|
|
||||||
- cyc-complex
|
|
||||||
|
|
||||||
vuln:
|
vuln:
|
||||||
desc: Check for vulnerabilities
|
desc: Check for vulnerabilities
|
||||||
sources:
|
sources:
|
||||||
|
|
@ -77,12 +56,11 @@ tasks:
|
||||||
- vet
|
- vet
|
||||||
- critic
|
- critic
|
||||||
- staticcheck
|
- staticcheck
|
||||||
- complex
|
|
||||||
- vuln
|
- vuln
|
||||||
|
|
||||||
test:
|
test:
|
||||||
desc: Run unit tests
|
desc: Run unit tests
|
||||||
deps: [fmt, vet, gen]
|
deps: [fmt, vet]
|
||||||
sources:
|
sources:
|
||||||
- '**/*.go'
|
- '**/*.go'
|
||||||
generates:
|
generates:
|
||||||
|
|
@ -105,9 +83,4 @@ tasks:
|
||||||
deps: [coverage-report]
|
deps: [coverage-report]
|
||||||
cmds:
|
cmds:
|
||||||
- ip addr list | grep inet
|
- ip addr list | grep inet
|
||||||
- php -S 0.0.0.0:3434 -t build
|
- php -S 0.0.0.0:3265 -t build
|
||||||
|
|
||||||
serve-docs:
|
|
||||||
desc: Serve the current docs
|
|
||||||
cmds:
|
|
||||||
- godoc -http=0.0.0.0:3434 -play
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,8 @@
|
||||||
package nomino
|
package nomino
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
|
||||||
"crypto/sha1"
|
|
||||||
"crypto/sha256"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -189,44 +185,3 @@ func SlugWithLang(lang string) Generator {
|
||||||
return slug.MakeLang(name, lang), err
|
return slug.MakeLang(name, lang), err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HashingFunc is a function that generates a hash.Hash
|
|
||||||
type HashingFunc func() hash.Hash
|
|
||||||
|
|
||||||
//go:generate stringer -type=HashType
|
|
||||||
|
|
||||||
// HashType represents a particular hashing algorithm
|
|
||||||
type HashType uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
MD5 HashType = iota + 1
|
|
||||||
SHA1
|
|
||||||
SHA256
|
|
||||||
)
|
|
||||||
|
|
||||||
// ErrInvalidHashType is returned by the Hash generator when an invalid HashType is passed
|
|
||||||
var ErrInvalidHashType = errors.New("invalid hash type")
|
|
||||||
|
|
||||||
var hashMap = map[HashType]HashingFunc{
|
|
||||||
MD5: md5.New,
|
|
||||||
SHA1: sha1.New,
|
|
||||||
SHA256: sha256.New,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hash generates a name from a hash of the filename.
|
|
||||||
// When this is used, the original filename will be removed from the final filename.
|
|
||||||
func Hash(t HashType) Generator {
|
|
||||||
f, ok := hashMap[t]
|
|
||||||
return func(c *Config) (string, error) {
|
|
||||||
if !ok {
|
|
||||||
return "", fmt.Errorf("%w: %s", ErrInvalidHashType, t)
|
|
||||||
}
|
|
||||||
name, err := getOriginal(c)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
hs := f()
|
|
||||||
hs.Write([]byte(name))
|
|
||||||
return fmt.Sprintf("%x", hs.Sum(nil)), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -161,33 +161,3 @@ func ExampleSlugWithLang() {
|
||||||
|
|
||||||
// Output: diese-und-dass.txt
|
// Output: diese-und-dass.txt
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleHash_mD5() {
|
|
||||||
conf := NewConfig(
|
|
||||||
WithOriginal("foobar"),
|
|
||||||
WithGenerator(Hash(MD5)),
|
|
||||||
)
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
// Output: 3858f62230ac3c915f300c664312c63f.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleHash_sHA1() {
|
|
||||||
conf := NewConfig(
|
|
||||||
WithOriginal("foobar"),
|
|
||||||
WithGenerator(Hash(SHA1)),
|
|
||||||
)
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
// Output: 8843d7f92416211de9ebb963ff4ce28125932878.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleHash_sHA256() {
|
|
||||||
conf := NewConfig(
|
|
||||||
WithOriginal("foobar"),
|
|
||||||
WithGenerator(Hash(SHA256)),
|
|
||||||
)
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
// Output: c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2.txt
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package nomino
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -121,23 +120,3 @@ func TestSlugRemovesOriginal(t *testing.T) {
|
||||||
assert.Equal(t, "hello-world", st)
|
assert.Equal(t, "hello-world", st)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHashBadHash(t *testing.T) {
|
|
||||||
conf := NewConfig(WithOriginal("foobar"), WithGenerator(Hash(0)))
|
|
||||||
st, err := conf.generator(&conf)
|
|
||||||
assert.Equal(t, "", st)
|
|
||||||
assert.ErrorIs(t, err, ErrInvalidHashType)
|
|
||||||
assert.ErrorContains(t, err, "invalid hash type: HashType(0)")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHashMissingOriginal(t *testing.T) {
|
|
||||||
conf := NewConfig(WithGenerator(Hash(MD5)))
|
|
||||||
st, err := conf.generator(&conf)
|
|
||||||
assert.Equal(t, "", st)
|
|
||||||
assert.ErrorIs(t, err, ErrMissingOriginal)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHashTypeStringer(t *testing.T) {
|
|
||||||
s := fmt.Sprintf("%s", MD5)
|
|
||||||
assert.Equal(t, "MD5", s)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
// Code generated by "stringer -type=HashType"; DO NOT EDIT.
|
|
||||||
|
|
||||||
package nomino
|
|
||||||
|
|
||||||
import "strconv"
|
|
||||||
|
|
||||||
func _() {
|
|
||||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
|
||||||
// Re-run the stringer command to generate them again.
|
|
||||||
var x [1]struct{}
|
|
||||||
_ = x[MD5-1]
|
|
||||||
_ = x[SHA1-2]
|
|
||||||
_ = x[SHA256-3]
|
|
||||||
}
|
|
||||||
|
|
||||||
const _HashType_name = "MD5SHA1SHA256"
|
|
||||||
|
|
||||||
var _HashType_index = [...]uint8{0, 3, 7, 13}
|
|
||||||
|
|
||||||
func (i HashType) String() string {
|
|
||||||
i -= 1
|
|
||||||
if i >= HashType(len(_HashType_index)-1) {
|
|
||||||
return "HashType(" + strconv.FormatInt(int64(i+1), 10) + ")"
|
|
||||||
}
|
|
||||||
return _HashType_name[_HashType_index[i]:_HashType_index[i+1]]
|
|
||||||
}
|
|
||||||
2
make.go
2
make.go
|
|
@ -12,7 +12,7 @@ func Make(conf Config) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.prefix != "" {
|
if conf.prefix != "" {
|
||||||
conf.prefix += conf.separator
|
conf.prefix = conf.prefix + conf.separator
|
||||||
}
|
}
|
||||||
if conf.original != "" {
|
if conf.original != "" {
|
||||||
conf.original = conf.separator + conf.original
|
conf.original = conf.separator + conf.original
|
||||||
|
|
|
||||||
|
|
@ -36,9 +36,7 @@ func TestMake(t *testing.T) {
|
||||||
|
|
||||||
for _, testcase := range testcases {
|
for _, testcase := range testcases {
|
||||||
t.Run(testcase.name, func(sub *testing.T) {
|
t.Run(testcase.name, func(sub *testing.T) {
|
||||||
opts := testcase.opts
|
opts := append(testcase.opts, genOpt)
|
||||||
opts = append(opts, genOpt)
|
|
||||||
|
|
||||||
conf := NewConfig(opts...)
|
conf := NewConfig(opts...)
|
||||||
st, err := Make(conf)
|
st, err := Make(conf)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue