-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gobuffalo/paths
add path's
- Loading branch information
Showing
4 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("([^:]+)(:(/.+))?") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |