1
0
mirror of http://git.dtluna.net/tomo/go-coreutils.git synced 2025-01-07 06:39:54 +00:00

Add basename

This commit is contained in:
dtluna 2019-03-26 13:05:19 +03:00
parent 289851b20f
commit dbce61c760
5 changed files with 73 additions and 0 deletions

View File

@ -12,3 +12,4 @@ Implemeted utilities:
* `false`
* `yes`
* `dirname`
* `basename`

35
basename/basename.go Normal file
View File

@ -0,0 +1,35 @@
package main
import (
"fmt"
"os"
p "path"
"strings"
)
func usage() {
fmt.Printf("usage: %v path [suffix]\n", os.Args[0])
os.Exit(1)
}
func basename(path, suffix string) string {
result := p.Base(path)
if suffix == "" {
return result
}
return strings.SplitN(result, suffix, 2)[0]
}
func main() {
if len(os.Args) == 1 || len(os.Args) > 3 {
usage()
}
path := os.Args[1]
suffix := ""
if len(os.Args) == 3 {
suffix = os.Args[2]
}
fmt.Println(basename(path, suffix))
}

28
basename/basename_test.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"path"
"testing"
)
type BasenameTestSuite struct {
suite.Suite
}
func (suite *BasenameTestSuite) TestEmptySuffix() {
assert.Equal(suite.T(), "go-coreutils", basename(path.Join("tomo", "go-coreutils"), ""))
}
func (suite *BasenameTestSuite) TestSuffixNotInPath() {
assert.Equal(suite.T(), "go-coreutils", basename(path.Join("tomo", "go-coreutils"), ".git"))
}
func (suite *BasenameTestSuite) TestSuffixInPath() {
assert.Equal(suite.T(), "go-coreutils", basename(path.Join("tomo", "go-coreutils"), ".git"))
}
func TestBasenameTestSuite(t *testing.T) {
suite.Run(t, new(BasenameTestSuite))
}

2
go.mod
View File

@ -1 +1,3 @@
module source.heropunch.io/tomo/go-coreutils
require github.com/stretchr/testify v1.3.0

7
go.sum Normal file
View File

@ -0,0 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=