feat: simplify flag parsing in buildpacks plugin
Commands should be written such that they take arguments as is.
This commit is contained in:
1 parent
ce154aff09
commit
6636c48838
3 files changed
+56
-87
No files matched your search
@@ -2,14 +2,26 @@ package buildpacks
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getAppName(args []string) (appName string, err error) {
|
func validBuildpackURL(buildpack string) (string, error) {
|
||||||
if len(args) >= 1 {
|
if buildpack == "" {
|
||||||
appName = args[0]
|
return buildpack, errors.New("Must specify a buildpack to add")
|
||||||
} else {
|
|
||||||
err = errors.New("Please specify an app to run the command on")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
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)
|
||||||
}
|
}
|
||||||
@@ -21,30 +21,39 @@ func main() {
|
|||||||
args := flag.NewFlagSet("buildpacks:add", flag.ExitOnError)
|
args := flag.NewFlagSet("buildpacks:add", flag.ExitOnError)
|
||||||
index := args.Int("index", 0, "--index: the 1-based index of the URL in the list of URLs")
|
index := args.Int("index", 0, "--index: the 1-based index of the URL in the list of URLs")
|
||||||
args.Parse(os.Args[2:])
|
args.Parse(os.Args[2:])
|
||||||
err = buildpacks.CommandAdd(args.Args(), *index)
|
appName := args.Arg(0)
|
||||||
|
buildpack := args.Arg(1)
|
||||||
|
err = buildpacks.CommandAdd(appName, buildpack, *index)
|
||||||
case "clear":
|
case "clear":
|
||||||
args := flag.NewFlagSet("buildpacks:clear", flag.ExitOnError)
|
args := flag.NewFlagSet("buildpacks:clear", flag.ExitOnError)
|
||||||
args.Parse(os.Args[2:])
|
args.Parse(os.Args[2:])
|
||||||
err = buildpacks.CommandClear(args.Args())
|
appName := args.Arg(0)
|
||||||
|
err = buildpacks.CommandClear(appName)
|
||||||
case "list":
|
case "list":
|
||||||
args := flag.NewFlagSet("buildpacks:list", flag.ExitOnError)
|
args := flag.NewFlagSet("buildpacks:list", flag.ExitOnError)
|
||||||
args.Parse(os.Args[2:])
|
args.Parse(os.Args[2:])
|
||||||
buildpacks.CommandList(args.Args())
|
appName := args.Arg(0)
|
||||||
|
err = buildpacks.CommandList(appName)
|
||||||
case "remove":
|
case "remove":
|
||||||
args := flag.NewFlagSet("buildpacks:remove", flag.ExitOnError)
|
args := flag.NewFlagSet("buildpacks:remove", flag.ExitOnError)
|
||||||
index := args.Int("index", 0, "--index: the 1-based index of the URL in the list of URLs")
|
index := args.Int("index", 0, "--index: the 1-based index of the URL in the list of URLs")
|
||||||
args.Parse(os.Args[2:])
|
args.Parse(os.Args[2:])
|
||||||
err = buildpacks.CommandRemove(args.Args(), *index)
|
appName := args.Arg(0)
|
||||||
|
buildpack := args.Arg(1)
|
||||||
|
err = buildpacks.CommandRemove(appName, buildpack, *index)
|
||||||
case "report":
|
case "report":
|
||||||
flag.Parse()
|
args := flag.NewFlagSet("buildpacks:report", flag.ExitOnError)
|
||||||
appName := flag.Arg(1)
|
args.Parse(os.Args[2:])
|
||||||
infoFlag := flag.Arg(2)
|
appName := args.Arg(0)
|
||||||
buildpacks.CommandReport(appName, infoFlag)
|
infoFlag := args.Arg(1)
|
||||||
|
err = buildpacks.CommandReport(appName, infoFlag)
|
||||||
case "set":
|
case "set":
|
||||||
args := flag.NewFlagSet("buildpacks:set", flag.ExitOnError)
|
args := flag.NewFlagSet("buildpacks:set", flag.ExitOnError)
|
||||||
index := args.Int("index", 0, "--index: the 1-based index of the URL in the list of URLs")
|
index := args.Int("index", 0, "--index: the 1-based index of the URL in the list of URLs")
|
||||||
args.Parse(os.Args[2:])
|
args.Parse(os.Args[2:])
|
||||||
err = buildpacks.CommandSet(args.Args(), *index)
|
appName := args.Arg(0)
|
||||||
|
buildpack := args.Arg(1)
|
||||||
|
err = buildpacks.CommandSet(appName, buildpack, *index)
|
||||||
default:
|
default:
|
||||||
common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand))
|
common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,71 +3,38 @@ package buildpacks
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/dokku/dokku/plugins/common"
|
"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
|
// CommandAdd implements buildpacks:add
|
||||||
func CommandAdd(args []string, index int) (err error) {
|
func CommandAdd(appName string, buildpack string, index int) error {
|
||||||
var appName string
|
if appName == "" {
|
||||||
appName, err = getAppName(args)
|
return errors.New("Please specify an app to run the command on")
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buildpack := ""
|
buildpack, err := validBuildpackURL(buildpack)
|
||||||
if len(args) >= 2 {
|
|
||||||
buildpack = args[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
buildpack, err = validBuildpackURL(buildpack)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = common.PropertyListAdd("buildpacks", appName, "buildpacks", buildpack, index)
|
return common.PropertyListAdd("buildpacks", appName, "buildpacks", buildpack, index)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommandClear implements buildpacks:clear
|
// CommandClear implements buildpacks:clear
|
||||||
func CommandClear(args []string) (err error) {
|
func CommandClear(appName string) error {
|
||||||
var appName string
|
if appName == "" {
|
||||||
appName, err = getAppName(args)
|
return errors.New("Please specify an app to run the command on")
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
common.PropertyDelete("buildpacks", appName, "buildpacks")
|
return common.PropertyDelete("buildpacks", appName, "buildpacks")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommandList implements buildpacks:list
|
// CommandList implements buildpacks:list
|
||||||
func CommandList(args []string) (err error) {
|
func CommandList(appName string) (err error) {
|
||||||
var appName string
|
if appName == "" {
|
||||||
appName, err = getAppName(args)
|
return errors.New("Please specify an app to run the command on")
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buildpacks, err := common.PropertyListGet("buildpacks", appName, "buildpacks")
|
buildpacks, err := common.PropertyListGet("buildpacks", appName, "buildpacks")
|
||||||
@@ -83,17 +50,11 @@ func CommandList(args []string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CommandRemove implements buildpacks:remove
|
// CommandRemove implements buildpacks:remove
|
||||||
func CommandRemove(args []string, index int) (err error) {
|
func CommandRemove(appName string, buildpack string, index int) (err error) {
|
||||||
var appName string
|
if appName == "" {
|
||||||
appName, err = getAppName(args)
|
return errors.New("Please specify an app to run the command on")
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buildpack := ""
|
|
||||||
if len(args) >= 2 {
|
|
||||||
buildpack = args[1]
|
|
||||||
}
|
|
||||||
if index != 0 && buildpack != "" {
|
if index != 0 && buildpack != "" {
|
||||||
err = errors.New("Please choose either index or Buildpack, but not both")
|
err = errors.New("Please choose either index or Buildpack, but not both")
|
||||||
return
|
return
|
||||||
@@ -164,23 +125,15 @@ func CommandReport(appName string, infoFlag string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ReportSingleApp(appName, infoFlag)
|
return ReportSingleApp(appName, infoFlag)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommandSet implements buildpacks:set
|
// CommandSet implements buildpacks:set
|
||||||
func CommandSet(args []string, index int) (err error) {
|
func CommandSet(appName string, buildpack string, index int) error {
|
||||||
var appName string
|
if appName == "" {
|
||||||
appName, err = getAppName(args)
|
return errors.New("Please specify an app to run the command on")
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buildpack := ""
|
buildpack, err := validBuildpackURL(buildpack)
|
||||||
if len(args) >= 2 {
|
|
||||||
buildpack = args[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
buildpack, err = validBuildpackURL(buildpack)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -189,10 +142,5 @@ func CommandSet(args []string, index int) (err error) {
|
|||||||
index--
|
index--
|
||||||
}
|
}
|
||||||
|
|
||||||
err = common.PropertyListSet("buildpacks", appName, "buildpacks", buildpack, index)
|
return common.PropertyListSet("buildpacks", appName, "buildpacks", buildpack, index)
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user