Skip to content

Commit

Permalink
add path info func
Browse files Browse the repository at this point in the history
  • Loading branch information
why444216978 committed Mar 27, 2022
1 parent 9daa3c6 commit 1544db5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
28 changes: 28 additions & 0 deletions dir/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"

"github.com/pkg/errors"
)

// GetCurrentDirectory 获得当前绝对路径
Expand Down Expand Up @@ -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
}
23 changes: 23 additions & 0 deletions dir/dir_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
})
}

0 comments on commit 1544db5

Please sign in to comment.