Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bob/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ func (b *B) Aggregate() (aggregate *bobfile.Bobfile, err error) {
return nil, err
}

switch projectName.Type() {
switch projectName.Type(b.allowInsecure) {
case project.Local:
// Do nothing
case project.Remote:
// Initialize remote store
url, err := projectName.Remote()
url, err := projectName.Remote(b.allowInsecure)
if err != nil {
return nil, err
}
Expand Down
50 changes: 22 additions & 28 deletions bob/bobfile/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var RestrictedProjectNamePattern = regexp.MustCompile(`^` + RestrictedProjectNam
var ProjectNameDoubleSlashPattern = regexp.MustCompile(`//+`)

var (
ErrProjectIsLocal = fmt.Errorf("can't use Remote() with local project")
ErrProjectIsRemote = fmt.Errorf("can't use Local() with remote project")

ErrInvalidProjectName = fmt.Errorf("invalid project name")
Expand All @@ -37,25 +36,13 @@ const (

type Name string

func (n *Name) Type() T {
t, _, _ := parse(*n)
func (n *Name) Type(allowInsecure bool) T {
t, _, _ := parse(*n, allowInsecure)
return t
}

func (n *Name) Local() (string, error) {
t, l, _ := parse(*n)
switch t {
case Local:
return string(l), nil
case Remote:
return "", ErrProjectIsLocal
default:
return string(l), nil
}
}

func (n *Name) Remote() (*url.URL, error) {
t, _, url := parse(*n)
func (n *Name) Remote(allowInsecure bool) (*url.URL, error) {
t, _, url := parse(*n, allowInsecure)
switch t {
case Local:
return nil, ErrProjectIsRemote
Expand All @@ -66,44 +53,51 @@ func (n *Name) Remote() (*url.URL, error) {
}
}

// Parse a projectname and validate it against `RestrictedProjectNamePattern`
func Parse(projectname string) (Name, error) {
if !RestrictedProjectNamePattern.MatchString(projectname) {
// Parse a projectName and validate it against `RestrictedProjectNamePattern`
func Parse(projectName string) (Name, error) {
if !RestrictedProjectNamePattern.MatchString(projectName) {
return "", usererror.Wrap(errors.WithMessage(ErrInvalidProjectName,
"project name should be in the form 'project' or 'bob.build/user/project'",
))
}

// test for double slash (do not allow prepended schema)
if ProjectNameDoubleSlashPattern.MatchString(projectname) {
if ProjectNameDoubleSlashPattern.MatchString(projectName) {
return "", usererror.Wrap(errors.WithMessage(ErrInvalidProjectName, ProjectNameFormatHint))
}

return Name(projectname), nil
return Name(projectName), nil
}

func parse(projectname Name) (T, Name, *url.URL) {
n := string(projectname)
func parse(projectName Name, allowInsecure bool) (T, Name, *url.URL) {
n := string(projectName)
if n == "" {
return Local, "", nil
}

segs := strings.Split(n, "/")
if len(segs) <= 1 {
return Local, projectname, nil
return Local, projectName, nil
}

url, err := url.Parse("https://" + n)
if err != nil {
return Local, projectname, nil
return Local, projectName, nil
}

// in case o a relative path expect it to be local
if url.Host == "" {
return Local, projectname, nil
return Local, projectName, nil
}

url.Scheme = "https"
url.Scheme = scheme(allowInsecure)

return Remote, "", url
}

func scheme(allowInsecure bool) string {
if allowInsecure {
return "http"
}
return "https"
}