feat: simplify flag parsing in buildpacks plugin

Commands should be written such that they take arguments as is.
This commit is contained in:
Jose Diaz-Gonzalez
2020-05-28 19:47:07 -04:00
parent ce154aff09
commit 6636c48838
3 changed files with 56 additions and 87 deletions

View File

@@ -21,30 +21,39 @@ func main() {
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")
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":
args := flag.NewFlagSet("buildpacks:clear", flag.ExitOnError)
args.Parse(os.Args[2:])
err = buildpacks.CommandClear(args.Args())
appName := args.Arg(0)
err = buildpacks.CommandClear(appName)
case "list":
args := flag.NewFlagSet("buildpacks:list", flag.ExitOnError)
args.Parse(os.Args[2:])
buildpacks.CommandList(args.Args())
appName := args.Arg(0)
err = buildpacks.CommandList(appName)
case "remove":
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")
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":
flag.Parse()
appName := flag.Arg(1)
infoFlag := flag.Arg(2)
buildpacks.CommandReport(appName, infoFlag)
args := flag.NewFlagSet("buildpacks:report", flag.ExitOnError)
args.Parse(os.Args[2:])
appName := args.Arg(0)
infoFlag := args.Arg(1)
err = buildpacks.CommandReport(appName, infoFlag)
case "set":
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")
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:
common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand))
}