diff --git a/dir/dir.go b/dir/dir.go index f118d66..15c4cc2 100755 --- a/dir/dir.go +++ b/dir/dir.go @@ -4,9 +4,12 @@ import ( "io/ioutil" "log" "os" + "path" "path/filepath" "strings" "time" + + "github.com/pkg/errors" ) // GetCurrentDirectory 获得当前绝对路径 @@ -110,3 +113,28 @@ func ReadDirAll(path string, curHier int) ([]string, error) { } return ret, nil } + +type FileInfo struct { + Path string + Base string + BaseNoExt string + Ext string + ExtNoSpot string +} + +// GetPathInfo 获得路径信息 +func GetPathInfo(f string) (info FileInfo, err error) { + info.Path = f + f = path.Base(filepath.ToSlash(f)) + info.Base = f + ext := path.Ext(f) + info.Ext = ext + if ext == "" { + err = errors.New("ext error") + return + } + info.ExtNoSpot = ext[1:] + info.BaseNoExt = strings.TrimSuffix(f, ext) + + return +} diff --git a/dir/dir_test.go b/dir/dir_test.go new file mode 100644 index 0000000..8421c58 --- /dev/null +++ b/dir/dir_test.go @@ -0,0 +1,23 @@ +package dir + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" + "gopkg.in/go-playground/assert.v1" +) + +func TestGetFileInfo(t *testing.T) { + Convey("TestGetFileInfo", t, func() { + Convey("success", func() { + f := "/user/local/data.toml" + info, err := GetPathInfo(f) + assert.Equal(t, info.Path, f) + assert.Equal(t, info.Base, "data.toml") + assert.Equal(t, info.BaseNoExt, "data") + assert.Equal(t, info.Ext, ".toml") + assert.Equal(t, info.ExtNoSpot, "toml") + assert.Equal(t, err, nil) + }) + }) +}