format.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2015 The etcd Authors
  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. "go.etcd.io/etcd/client"
  20. )
  21. // printResponseKey only supports to print key correctly.
  22. func printResponseKey(resp *client.Response, format string) {
  23. // Format the result.
  24. switch format {
  25. case "simple":
  26. if resp.Action != "delete" {
  27. fmt.Println(resp.Node.Value)
  28. } else {
  29. fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
  30. }
  31. case "extended":
  32. // Extended prints in a rfc2822 style format
  33. fmt.Println("Key:", resp.Node.Key)
  34. fmt.Println("Created-Index:", resp.Node.CreatedIndex)
  35. fmt.Println("Modified-Index:", resp.Node.ModifiedIndex)
  36. if resp.PrevNode != nil {
  37. fmt.Println("PrevNode.Value:", resp.PrevNode.Value)
  38. }
  39. fmt.Println("TTL:", resp.Node.TTL)
  40. fmt.Println("Index:", resp.Index)
  41. if resp.Action != "delete" {
  42. fmt.Println("")
  43. fmt.Println(resp.Node.Value)
  44. }
  45. case "json":
  46. b, err := json.Marshal(resp)
  47. if err != nil {
  48. panic(err)
  49. }
  50. fmt.Println(string(b))
  51. default:
  52. fmt.Fprintln(os.Stderr, "Unsupported output format:", format)
  53. }
  54. }