pkger/path.go

101 lines
1.6 KiB
Go
Raw Normal View History

2019-08-02 05:34:32 +03:00
package pkger
2019-08-01 19:03:12 +03:00
import (
2019-08-02 00:35:42 +03:00
"encoding/json"
2019-08-01 19:03:12 +03:00
"fmt"
2019-08-02 00:35:42 +03:00
"os"
2019-08-09 22:43:58 +03:00
"regexp"
"strings"
2019-08-01 19:03:12 +03:00
)
type Path struct {
Pkg string `json:"pkg"`
Name string `json:"name"`
}
2019-08-09 22:43:58 +03:00
func Parse(p string) (Path, error) {
dubeg("Parse", p)
p = strings.Replace(p, "\\", "/", -1)
pt, ok := pathsCache.Load(p)
if ok {
return pt, nil
}
if len(p) == 0 {
return build(p, "", "")
}
res := pathrx.FindAllStringSubmatch(p, -1)
if len(res) == 0 {
return pt, fmt.Errorf("could not parse %q", p)
}
matches := res[0]
if len(matches) != 4 {
return pt, fmt.Errorf("could not parse %q", p)
}
return build(p, matches[1], matches[3])
}
2019-08-01 19:03:12 +03:00
func (p Path) String() string {
2019-08-02 00:35:42 +03:00
if p.Name == "" {
p.Name = "/"
2019-08-01 19:03:12 +03:00
}
2019-08-02 00:35:42 +03:00
return fmt.Sprintf("%s:%s", p.Pkg, p.Name)
2019-08-01 19:03:12 +03:00
}
2019-08-02 00:35:42 +03:00
func (p Path) Format(st fmt.State, verb rune) {
switch verb {
case 'v':
if st.Flag('+') {
b, err := json.MarshalIndent(p, "", " ")
if err != nil {
fmt.Fprint(os.Stderr, err)
return
}
fmt.Fprint(st, string(b))
return
2019-08-01 19:03:12 +03:00
}
2019-08-02 00:35:42 +03:00
fmt.Fprint(st, p.String())
case 'q':
fmt.Fprintf(st, "%q", p.String())
default:
fmt.Fprint(st, p.String())
2019-08-01 19:03:12 +03:00
}
}
2019-08-09 22:43:58 +03:00
var pathrx = regexp.MustCompile("([^:]+)(:(/.+))?")
func build(p, pkg, name string) (Path, error) {
pt := Path{
Pkg: pkg,
Name: name,
}
current, err := Stat()
if err != nil {
return pt, err
}
if strings.HasPrefix(pt.Pkg, "/") || len(pt.Pkg) == 0 {
pt.Name = pt.Pkg
pt.Pkg = current.ImportPath
}
if len(pt.Name) == 0 {
pt.Name = "/"
}
if pt.Pkg == pt.Name {
pt.Pkg = current.ImportPath
pt.Name = "/"
}
if !strings.HasPrefix(pt.Name, "/") {
pt.Name = "/" + pt.Name
}
pathsCache.Store(p, pt)
return pt, nil
}