handle.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/bgentry/speakeasy"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  23. )
  24. type handlerFunc func(*cli.Context, *etcd.Client) (*etcd.Response, error)
  25. type printFunc func(*etcd.Response, string)
  26. type contextualPrintFunc func(*cli.Context, *etcd.Response, string)
  27. // dumpCURL blindly dumps all curl output to os.Stderr
  28. func dumpCURL(client *etcd.Client) {
  29. client.OpenCURL()
  30. for {
  31. fmt.Fprintf(os.Stderr, "Curl-Example: %s\n", client.RecvCURL())
  32. }
  33. }
  34. func getUsernamePasswordFromFlag(usernameFlag string) (username string, password string, err error) {
  35. colon := strings.Index(usernameFlag, ":")
  36. if colon == -1 {
  37. username = usernameFlag
  38. // Prompt for the password.
  39. password, err = speakeasy.Ask("Password: ")
  40. if err != nil {
  41. return "", "", err
  42. }
  43. } else {
  44. username = usernameFlag[:colon]
  45. password = usernameFlag[colon+1:]
  46. }
  47. return username, password, nil
  48. }
  49. func prepAuth(client *etcd.Client, usernameFlag string) error {
  50. username, password, err := getUsernamePasswordFromFlag(usernameFlag)
  51. if err != nil {
  52. return err
  53. }
  54. client.SetCredentials(username, password)
  55. return nil
  56. }
  57. // rawhandle wraps the command function handlers and sets up the
  58. // environment but performs no output formatting.
  59. func rawhandle(c *cli.Context, fn handlerFunc) (*etcd.Response, error) {
  60. endpoints, err := getEndpoints(c)
  61. if err != nil {
  62. return nil, err
  63. }
  64. tr, err := getTransport(c)
  65. if err != nil {
  66. return nil, err
  67. }
  68. client := etcd.NewClient(endpoints)
  69. client.SetTransport(tr)
  70. username := c.GlobalString("username")
  71. if username != "" {
  72. err := prepAuth(client, username)
  73. if err != nil {
  74. return nil, err
  75. }
  76. }
  77. if c.GlobalBool("debug") {
  78. go dumpCURL(client)
  79. }
  80. // Sync cluster.
  81. if !c.GlobalBool("no-sync") {
  82. if ok := client.SyncCluster(); !ok {
  83. handleError(ExitBadConnection, errors.New("cannot sync with the cluster using endpoints "+strings.Join(endpoints, ", ")))
  84. }
  85. }
  86. if c.GlobalBool("debug") {
  87. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(client.GetCluster(), ", "))
  88. }
  89. // Execute handler function.
  90. return fn(c, client)
  91. }
  92. // handlePrint wraps the command function handlers to parse global flags
  93. // into a client and to properly format the response objects.
  94. func handlePrint(c *cli.Context, fn handlerFunc, pFn printFunc) {
  95. resp, err := rawhandle(c, fn)
  96. // Print error and exit, if necessary.
  97. if err != nil {
  98. handleError(ExitServerError, err)
  99. }
  100. if resp != nil && pFn != nil {
  101. pFn(resp, c.GlobalString("output"))
  102. }
  103. }
  104. // Just like handlePrint but also passed the context of the command
  105. func handleContextualPrint(c *cli.Context, fn handlerFunc, pFn contextualPrintFunc) {
  106. resp, err := rawhandle(c, fn)
  107. if err != nil {
  108. handleError(ExitServerError, err)
  109. }
  110. if resp != nil && pFn != nil {
  111. pFn(c, resp, c.GlobalString("output"))
  112. }
  113. }
  114. // handleDir handles a request that wants to do operations on a single dir.
  115. // Dir cannot be printed out, so we set NIL print function here.
  116. func handleDir(c *cli.Context, fn handlerFunc) {
  117. handlePrint(c, fn, nil)
  118. }
  119. // handleKey handles a request that wants to do operations on a single key.
  120. func handleKey(c *cli.Context, fn handlerFunc) {
  121. handlePrint(c, fn, printKey)
  122. }
  123. func handleAll(c *cli.Context, fn handlerFunc) {
  124. handlePrint(c, fn, printAll)
  125. }