Restore behavior of trimming / from kubecfg url

Also make the output and validation of input better for kubecfg api calls.
Kubecfg will now display a usage argument if the URL is incorrect or an
unrecognized storage type is passed.
This commit is contained in:
Clayton Coleman 2014-07-23 11:16:41 -04:00
parent 245feaf722
commit 586a9f4356

View File

@ -156,35 +156,59 @@ func main() {
} }
} }
func executeAPIRequest(method string, s *kube_client.Client) bool { // storagePathFromArg normalizes a path and breaks out the first segment if available
if len(flag.Args()) < 2 { func storagePathFromArg(arg string) (storage, path string, hasSuffix bool) {
glog.Fatalf("usage: kubecfg [OPTIONS] get|list|create|update|delete <%s>[/<id>]", prettyWireStorage()) path = strings.Trim(arg, "/")
segments := strings.SplitN(path, "/", 2)
storage = segments[0]
if len(segments) > 1 && segments[1] != "" {
hasSuffix = true
}
return storage, path, hasSuffix
} }
//checkStorage returns true if the provided storage is valid
func checkStorage(storage string) bool {
for _, allowed := range kubecfg.SupportedWireStorage() {
if allowed == storage {
return true
}
}
return false
}
func executeAPIRequest(method string, s *kube_client.Client) bool {
storage, path, hasSuffix := storagePathFromArg(flag.Arg(1))
validStorage := checkStorage(storage)
verb := "" verb := ""
segments := strings.SplitN(flag.Arg(1), "/", 2)
storage := segments[0]
path := strings.Trim(flag.Arg(1), "/")
setBody := false setBody := false
switch method { switch method {
case "get", "list": case "get":
verb = "GET" verb = "GET"
if !validStorage || !hasSuffix {
glog.Fatalf("usage: kubecfg [OPTIONS] %s <%s>[/<id>]", method, prettyWireStorage())
}
case "list":
verb = "GET"
if !validStorage || hasSuffix {
glog.Fatalf("usage: kubecfg [OPTIONS] %s <%s>", method, prettyWireStorage())
}
case "delete": case "delete":
verb = "DELETE" verb = "DELETE"
if len(segments) == 1 || segments[1] == "" { if !validStorage || !hasSuffix {
glog.Fatalf("usage: kubecfg [OPTIONS] delete <%s>/<id>", prettyWireStorage()) glog.Fatalf("usage: kubecfg [OPTIONS] %s <%s>/<id>", method, prettyWireStorage())
} }
case "create": case "create":
verb = "POST" verb = "POST"
setBody = true setBody = true
if len(segments) != 1 { if !validStorage || hasSuffix {
glog.Fatalf("usage: kubecfg [OPTIONS] create <%s>", prettyWireStorage()) glog.Fatalf("usage: kubecfg [OPTIONS] %s <%s>", method, prettyWireStorage())
} }
case "update": case "update":
verb = "PUT" verb = "PUT"
setBody = true setBody = true
if len(segments) == 1 || segments[1] == "" { if !validStorage || !hasSuffix {
glog.Fatalf("usage: kubecfg [OPTIONS] update <%s>/<id>", prettyWireStorage()) glog.Fatalf("usage: kubecfg [OPTIONS] %s <%s>/<id>", method, prettyWireStorage())
} }
default: default:
return false return false