feat: add proper support for various buildpack urls
Transform the urls to the correct values when necessary, and validate them otherwise. Closes #3923
This commit is contained in:
@@ -3,11 +3,31 @@ package buildpacks
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
|
||||
func validBuildpackURL(buildpack string) (string, error) {
|
||||
if buildpack == "" {
|
||||
return buildpack, errors.New("Must specify a buildpack to add")
|
||||
}
|
||||
|
||||
reHerokuValue := regexp.MustCompile(`(?m)^([\w]+\/[\w]+)$`)
|
||||
if found := reHerokuValue.Find([]byte(buildpack)); found != nil {
|
||||
parts := strings.SplitN(buildpack, "/", 2)
|
||||
return fmt.Sprintf("https://github.com/%s/heroku-buildpack-%s.git", parts[0], parts[1]), nil
|
||||
}
|
||||
|
||||
reString := regexp.MustCompile(`(?m)^(http|https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+)(.git(#derp)?)?$`)
|
||||
if found := reString.Find([]byte(buildpack)); found != nil {
|
||||
return buildpack, nil
|
||||
}
|
||||
|
||||
return buildpack, fmt.Errorf("Invalid buildpack specified: %v", buildpack)
|
||||
}
|
||||
|
||||
// CommandAdd implements buildpacks:add
|
||||
func CommandAdd(args []string, index int) (err error) {
|
||||
var appName string
|
||||
@@ -20,8 +40,10 @@ func CommandAdd(args []string, index int) (err error) {
|
||||
if len(args) >= 2 {
|
||||
buildpack = args[1]
|
||||
}
|
||||
if buildpack == "" {
|
||||
return errors.New("Must specify a buildpack to add")
|
||||
|
||||
buildpack, err = validBuildpackURL(buildpack)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = common.PropertyListAdd("buildpacks", appName, "buildpacks", buildpack, index)
|
||||
@@ -82,6 +104,11 @@ func CommandRemove(args []string, index int) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
buildpack, err = validBuildpackURL(buildpack)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var buildpacks []string
|
||||
buildpacks, err = common.PropertyListGet("buildpacks", appName, "buildpacks")
|
||||
if err != nil {
|
||||
@@ -152,8 +179,10 @@ func CommandSet(args []string, index int) (err error) {
|
||||
if len(args) >= 2 {
|
||||
buildpack = args[1]
|
||||
}
|
||||
if buildpack == "" {
|
||||
return errors.New("Must specify a buildpack to add")
|
||||
|
||||
buildpack, err = validBuildpackURL(buildpack)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if index > 0 {
|
||||
|
||||
Reference in New Issue
Block a user