format.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "fmt"
  18. "os"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  20. "github.com/coreos/etcd/client"
  21. )
  22. // printKey writes the etcd response to STDOUT in the given format.
  23. func printKey(resp *etcd.Response, format string) {
  24. // printKey is only for keys, error on directories
  25. if resp.Node.Dir == true {
  26. fmt.Fprintln(os.Stderr, fmt.Sprintf("Cannot print key [%s: Is a directory]", resp.Node.Key))
  27. os.Exit(1)
  28. }
  29. printKeyOnly(resp, format)
  30. }
  31. // printAll prints the etcd response in the given format in its best efforts.
  32. func printAll(resp *etcd.Response, format string) {
  33. if resp.Node.Dir == true {
  34. return
  35. }
  36. printKeyOnly(resp, format)
  37. }
  38. // printKeyOnly only supports to print key correctly.
  39. func printKeyOnly(resp *etcd.Response, format string) {
  40. // Format the result.
  41. switch format {
  42. case "simple":
  43. fmt.Println(resp.Node.Value)
  44. case "extended":
  45. // Extended prints in a rfc2822 style format
  46. fmt.Println("Key:", resp.Node.Key)
  47. fmt.Println("Created-Index:", resp.Node.CreatedIndex)
  48. fmt.Println("Modified-Index:", resp.Node.ModifiedIndex)
  49. if resp.PrevNode != nil {
  50. fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
  51. }
  52. fmt.Println("TTL:", resp.Node.TTL)
  53. fmt.Println("Etcd-Index:", resp.EtcdIndex)
  54. fmt.Println("Raft-Index:", resp.RaftIndex)
  55. fmt.Println("Raft-Term:", resp.RaftTerm)
  56. fmt.Println("")
  57. fmt.Println(resp.Node.Value)
  58. case "json":
  59. b, err := json.Marshal(resp)
  60. if err != nil {
  61. panic(err)
  62. }
  63. fmt.Println(string(b))
  64. default:
  65. fmt.Fprintln(os.Stderr, "Unsupported output format:", format)
  66. }
  67. }
  68. // printResponseKey only supports to print key correctly.
  69. func printResponseKey(resp *client.Response, format string) {
  70. // Format the result.
  71. switch format {
  72. case "simple":
  73. if resp.Action != "delete" {
  74. fmt.Println(resp.Node.Value)
  75. } else {
  76. fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
  77. }
  78. case "extended":
  79. // Extended prints in a rfc2822 style format
  80. fmt.Println("Key:", resp.Node.Key)
  81. fmt.Println("Created-Index:", resp.Node.CreatedIndex)
  82. fmt.Println("Modified-Index:", resp.Node.ModifiedIndex)
  83. if resp.PrevNode != nil {
  84. fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
  85. }
  86. fmt.Println("TTL:", resp.Node.TTL)
  87. fmt.Println("Index:", resp.Index)
  88. if resp.Action != "delete" {
  89. fmt.Println("")
  90. fmt.Println(resp.Node.Value)
  91. }
  92. case "json":
  93. b, err := json.Marshal(resp)
  94. if err != nil {
  95. panic(err)
  96. }
  97. fmt.Println(string(b))
  98. default:
  99. fmt.Fprintln(os.Stderr, "Unsupported output format:", format)
  100. }
  101. }