handle.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "errors"
  17. "fmt"
  18. "os"
  19. "strings"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  22. )
  23. type handlerFunc func(*cli.Context, *etcd.Client) (*etcd.Response, error)
  24. type printFunc func(*etcd.Response, string)
  25. type contextualPrintFunc func(*cli.Context, *etcd.Response, string)
  26. // dumpCURL blindly dumps all curl output to os.Stderr
  27. func dumpCURL(client *etcd.Client) {
  28. client.OpenCURL()
  29. for {
  30. fmt.Fprintf(os.Stderr, "Curl-Example: %s\n", client.RecvCURL())
  31. }
  32. }
  33. // rawhandle wraps the command function handlers and sets up the
  34. // environment but performs no output formatting.
  35. func rawhandle(c *cli.Context, fn handlerFunc) (*etcd.Response, error) {
  36. endpoints, err := getEndpoints(c)
  37. if err != nil {
  38. return nil, err
  39. }
  40. tr, err := getTransport(c)
  41. if err != nil {
  42. return nil, err
  43. }
  44. client := etcd.NewClient(endpoints)
  45. client.SetTransport(tr)
  46. if c.GlobalBool("debug") {
  47. go dumpCURL(client)
  48. }
  49. // Sync cluster.
  50. if !c.GlobalBool("no-sync") {
  51. if ok := client.SyncCluster(); !ok {
  52. handleError(ExitBadConnection, errors.New("cannot sync with the cluster using endpoints "+strings.Join(endpoints, ", ")))
  53. }
  54. }
  55. if c.GlobalBool("debug") {
  56. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(client.GetCluster(), ", "))
  57. }
  58. // Execute handler function.
  59. return fn(c, client)
  60. }
  61. // handlePrint wraps the command function handlers to parse global flags
  62. // into a client and to properly format the response objects.
  63. func handlePrint(c *cli.Context, fn handlerFunc, pFn printFunc) {
  64. resp, err := rawhandle(c, fn)
  65. // Print error and exit, if necessary.
  66. if err != nil {
  67. handleError(ExitServerError, err)
  68. }
  69. if resp != nil && pFn != nil {
  70. pFn(resp, c.GlobalString("output"))
  71. }
  72. }
  73. // Just like handlePrint but also passed the context of the command
  74. func handleContextualPrint(c *cli.Context, fn handlerFunc, pFn contextualPrintFunc) {
  75. resp, err := rawhandle(c, fn)
  76. if err != nil {
  77. handleError(ExitServerError, err)
  78. }
  79. if resp != nil && pFn != nil {
  80. pFn(c, resp, c.GlobalString("output"))
  81. }
  82. }
  83. // handleDir handles a request that wants to do operations on a single dir.
  84. // Dir cannot be printed out, so we set NIL print function here.
  85. func handleDir(c *cli.Context, fn handlerFunc) {
  86. handlePrint(c, fn, nil)
  87. }
  88. // handleKey handles a request that wants to do operations on a single key.
  89. func handleKey(c *cli.Context, fn handlerFunc) {
  90. handlePrint(c, fn, printKey)
  91. }
  92. func handleAll(c *cli.Context, fn handlerFunc) {
  93. handlePrint(c, fn, printAll)
  94. }