From dbce61c7609943e8c13bded8763df9cc20b247f9 Mon Sep 17 00:00:00 2001 From: dtluna Date: Tue, 26 Mar 2019 13:05:19 +0300 Subject: [PATCH] Add basename --- README.md | 1 + basename/basename.go | 35 +++++++++++++++++++++++++++++++++++ basename/basename_test.go | 28 ++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 7 +++++++ 5 files changed, 73 insertions(+) create mode 100644 basename/basename.go create mode 100644 basename/basename_test.go create mode 100644 go.sum diff --git a/README.md b/README.md index 3091579..547467a 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,4 @@ Implemeted utilities: * `false` * `yes` * `dirname` + * `basename` diff --git a/basename/basename.go b/basename/basename.go new file mode 100644 index 0000000..cdcb457 --- /dev/null +++ b/basename/basename.go @@ -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)) +} diff --git a/basename/basename_test.go b/basename/basename_test.go new file mode 100644 index 0000000..550025f --- /dev/null +++ b/basename/basename_test.go @@ -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)) +} diff --git a/go.mod b/go.mod index 0c55116..053fd74 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module source.heropunch.io/tomo/go-coreutils + +require github.com/stretchr/testify v1.3.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4347755 --- /dev/null +++ b/go.sum @@ -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=