Skip to content

Commit

Permalink
Merge pull request #5 from gobuffalo/paths
Browse files Browse the repository at this point in the history
add path's
  • Loading branch information
markbates authored Dec 3, 2019
2 parents b2f97e9 + 8e97fc9 commit 3546a4d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 3 deletions.
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
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/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -12,11 +11,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
63 changes: 63 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package here

import (
"fmt"
"path/filepath"
"regexp"
"strings"
)

func (i Info) Parse(p string) (Path, error) {
p = strings.TrimSpace(p)
p = filepath.Clean(p)
p = strings.TrimPrefix(p, i.Dir)

p = strings.Replace(p, "\\", "/", -1)
p = strings.TrimSpace(p)

if len(p) == 0 || p == ":" || p == "." {
return i.build("", "", "")
}

res := pathrx.FindAllStringSubmatch(p, -1)
if len(res) == 0 {
return Path{}, fmt.Errorf("could not parse %q", p)
}

matches := res[0]

if len(matches) != 4 {
return Path{}, fmt.Errorf("could not parse %q", p)
}

return i.build(p, matches[1], matches[3])
}

func (i Info) build(p, pkg, name string) (Path, error) {
pt := Path{
Pkg: pkg,
Name: name,
}

if strings.HasPrefix(pt.Pkg, "/") || len(pt.Pkg) == 0 {
pt.Name = pt.Pkg
pt.Pkg = i.Module.Path
}

if len(pt.Name) == 0 {
pt.Name = "/"
}

if pt.Pkg == pt.Name {
pt.Pkg = i.Module.Path
pt.Name = "/"
}

if !strings.HasPrefix(pt.Name, "/") {
pt.Name = "/" + pt.Name
}
pt.Name = strings.TrimPrefix(pt.Name, i.Dir)
return pt, nil
}

var pathrx = regexp.MustCompile("([^:]+)(:(/.+))?")
60 changes: 60 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package here_test

import (
"path/filepath"
"strings"
"testing"

"github.com/gobuffalo/here"
"github.com/stretchr/testify/require"
)

func Test_Info_Parse(t *testing.T) {
const name = "/public/index.html"

r := require.New(t)

info, err := here.Current()
r.NoError(err)

ip := info.ImportPath
ip2 := "another/app"

table := []struct {
in string
exp here.Path
err bool
}{
{in: name, exp: here.Path{Pkg: ip, Name: name}},
{in: "", exp: here.Path{Pkg: ip, Name: "/"}},
{in: "/", exp: here.Path{Pkg: ip, Name: "/"}},
{in: filepath.Join(info.Dir, name), exp: here.Path{Pkg: ip, Name: name}},
{in: ":" + name, exp: here.Path{Pkg: ip, Name: name}},
{in: ip + ":" + name, exp: here.Path{Pkg: ip, Name: name}},
{in: ip, exp: here.Path{Pkg: ip, Name: "/"}},
{in: ":", exp: here.Path{Pkg: ip, Name: "/"}},
{in: ip2 + ":" + name, exp: here.Path{Pkg: ip2, Name: name}},
{in: ip2, exp: here.Path{Pkg: ip2, Name: "/"}},
{in: ip2 + ":", exp: here.Path{Pkg: ip2, Name: "/"}},
{in: filepath.Join(info.Dir, "public"), exp: here.Path{Pkg: ip, Name: "/public"}},
}

for _, tt := range table {
for _, in := range []string{tt.in, strings.ReplaceAll(tt.in, "/", "\\")} {
t.Run(in, func(st *testing.T) {
r := require.New(st)

pt, err := info.Parse(in)

if tt.err {
r.Error(err)
return
}
r.NoError(err)

r.Equal(tt.exp, pt)

})
}
}
}
20 changes: 20 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package here

import (
"fmt"
)

type Path struct {
Pkg string
Name string
}

func (p Path) String() string {
if p.Name == "" {
p.Name = "/"
}
if p.Pkg == "" {
return p.Name
}
return fmt.Sprintf("%s:%s", p.Pkg, p.Name)
}

0 comments on commit 3546a4d

Please sign in to comment.