pkger/path.go

44 lines
717 B
Go
Raw Normal View History

2019-07-31 00:21:26 +03:00
package pkger
import (
"fmt"
"strings"
)
type Path struct {
2019-07-31 18:53:36 +03:00
Pkg string `json:"pkg"`
Name string `json:"name"`
2019-07-31 00:21:26 +03:00
}
func (p Path) String() string {
if len(p.Pkg) == 0 {
return p.Name
}
if len(p.Name) == 0 {
return p.Pkg
}
return fmt.Sprintf("%s:/%s", p.Pkg, p.Name)
}
func Parse(p string) (Path, error) {
var pt Path
res := strings.Split(p, ":")
if len(res) < 1 {
return pt, fmt.Errorf("could not parse %q (%d)", res, len(res))
}
if len(res) == 1 {
if strings.HasPrefix(res[0], "/") {
pt.Name = res[0]
} else {
pt.Pkg = res[0]
}
} else {
pt.Pkg = res[0]
pt.Name = res[1]
}
pt.Name = strings.TrimPrefix(pt.Name, "/")
pt.Pkg = strings.TrimPrefix(pt.Pkg, "/")
return pt, nil
}