handle.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "strings"
  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. // rawhandle wraps the command function handlers and sets up the
  35. // environment but performs no output formatting.
  36. func rawhandle(c *cli.Context, fn handlerFunc) (*etcd.Response, error) {
  37. endpoints, err := getEndpoints(c)
  38. if err != nil {
  39. return nil, err
  40. }
  41. tr, err := getTransport(c)
  42. if err != nil {
  43. return nil, err
  44. }
  45. client := etcd.NewClient(endpoints)
  46. client.SetTransport(tr)
  47. if c.GlobalBool("debug") {
  48. go dumpCURL(client)
  49. }
  50. // Sync cluster.
  51. if !c.GlobalBool("no-sync") {
  52. if ok := client.SyncCluster(); !ok {
  53. handleError(FailedToConnectToHost, errors.New("cannot sync with the cluster using endpoints "+strings.Join(endpoints, ", ")))
  54. }
  55. }
  56. if c.GlobalBool("debug") {
  57. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(client.GetCluster(), ", "))
  58. }
  59. // Execute handler function.
  60. return fn(c, client)
  61. }
  62. // handlePrint wraps the command function handlers to parse global flags
  63. // into a client and to properly format the response objects.
  64. func handlePrint(c *cli.Context, fn handlerFunc, pFn printFunc) {
  65. resp, err := rawhandle(c, fn)
  66. // Print error and exit, if necessary.
  67. if err != nil {
  68. handleError(ErrorFromEtcd, err)
  69. }
  70. if resp != nil && pFn != nil {
  71. pFn(resp, c.GlobalString("output"))
  72. }
  73. }
  74. // Just like handlePrint but also passed the context of the command
  75. func handleContextualPrint(c *cli.Context, fn handlerFunc, pFn contextualPrintFunc) {
  76. resp, err := rawhandle(c, fn)
  77. if err != nil {
  78. handleError(ErrorFromEtcd, err)
  79. }
  80. if resp != nil && pFn != nil {
  81. pFn(c, resp, c.GlobalString("output"))
  82. }
  83. }
  84. // handleDir handles a request that wants to do operations on a single dir.
  85. // Dir cannot be printed out, so we set NIL print function here.
  86. func handleDir(c *cli.Context, fn handlerFunc) {
  87. handlePrint(c, fn, nil)
  88. }
  89. // handleKey handles a request that wants to do operations on a single key.
  90. func handleKey(c *cli.Context, fn handlerFunc) {
  91. handlePrint(c, fn, printKey)
  92. }
  93. func handleAll(c *cli.Context, fn handlerFunc) {
  94. handlePrint(c, fn, printAll)
  95. }
  96. // printKey writes the etcd response to STDOUT in the given format.
  97. func printKey(resp *etcd.Response, format string) {
  98. // printKey is only for keys, error on directories
  99. if resp.Node.Dir == true {
  100. fmt.Fprintln(os.Stderr, fmt.Sprintf("Cannot print key [%s: Is a directory]", resp.Node.Key))
  101. os.Exit(1)
  102. }
  103. printKeyOnly(resp, format)
  104. }
  105. // printAll prints the etcd response in the given format in its best efforts.
  106. func printAll(resp *etcd.Response, format string) {
  107. if resp.Node.Dir == true {
  108. return
  109. }
  110. printKeyOnly(resp, format)
  111. }
  112. // printKeyOnly only supports to print key correctly.
  113. func printKeyOnly(resp *etcd.Response, format string) {
  114. // Format the result.
  115. switch format {
  116. case "simple":
  117. fmt.Println(resp.Node.Value)
  118. case "extended":
  119. // Extended prints in a rfc2822 style format
  120. fmt.Println("Key:", resp.Node.Key)
  121. fmt.Println("Created-Index:", resp.Node.CreatedIndex)
  122. fmt.Println("Modified-Index:", resp.Node.ModifiedIndex)
  123. if resp.PrevNode != nil {
  124. fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
  125. }
  126. fmt.Println("TTL:", resp.Node.TTL)
  127. fmt.Println("Etcd-Index:", resp.EtcdIndex)
  128. fmt.Println("Raft-Index:", resp.RaftIndex)
  129. fmt.Println("Raft-Term:", resp.RaftTerm)
  130. fmt.Println("")
  131. fmt.Println(resp.Node.Value)
  132. case "json":
  133. b, err := json.Marshal(resp)
  134. if err != nil {
  135. panic(err)
  136. }
  137. fmt.Println(string(b))
  138. default:
  139. fmt.Fprintln(os.Stderr, "Unsupported output format:", format)
  140. }
  141. }