You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 lines
2.9 KiB

  1. // Copyright 2018 Google Inc. All rights reserved.
  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. // Command-line tool to view .marbl files. This tool reads all headers from provided .marbl
  15. // file and prints them to stdout. Bodies of request/response are not printed to stdout,
  16. // instead they are saved into individual files in form of "marbl_ID_TYPE" where
  17. // ID is the ID of request or response and TYPE is "request" or "response".
  18. //
  19. // Command line arguments:
  20. // --file Path to the .marbl file to view.
  21. // --out Optional, folder where this tool will save request/response bodies.
  22. // uses current folder by default.
  23. package main
  24. import (
  25. "flag"
  26. "fmt"
  27. "io"
  28. "log"
  29. "os"
  30. "github.com/google/martian/marbl"
  31. )
  32. var (
  33. file = flag.String("file", "", ".marbl file to show contents of")
  34. out = flag.String("out", "", "folder to write request/response bodies to. Folder must exist.")
  35. )
  36. func main() {
  37. flag.Parse()
  38. if *file == "" {
  39. fmt.Println("--file flag is required")
  40. return
  41. }
  42. file, err := os.Open(*file)
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. reader := marbl.NewReader(file)
  47. // Iterate through all frames in .marbl file.
  48. for {
  49. frame, err := reader.ReadFrame()
  50. if frame == nil && err == io.EOF {
  51. break
  52. }
  53. if err != nil {
  54. log.Fatalf("reader.ReadFrame(): got %v, want no error or io.EOF\n", err)
  55. break
  56. }
  57. // Print current frame to stdout.
  58. if frame.FrameType() == marbl.HeaderFrame {
  59. fmt.Print("Header ")
  60. } else {
  61. fmt.Print("Data ")
  62. }
  63. fmt.Println(frame.String())
  64. // If frame is Data then we write it into separate
  65. // file that can be inspected later.
  66. if frame.FrameType() == marbl.DataFrame {
  67. df := frame.(marbl.Data)
  68. var t string
  69. if df.MessageType == marbl.Request {
  70. t = "request"
  71. } else if df.MessageType == marbl.Response {
  72. t = "response"
  73. } else {
  74. t = fmt.Sprintf("unknown_%d", df.MessageType)
  75. }
  76. fout := fmt.Sprintf("marbl_%s_%s", df.ID, t)
  77. if *out != "" {
  78. fout = *out + "/" + fout
  79. }
  80. fmt.Printf("Appending data to file %s\n", fout)
  81. // Append data to the file. Note that body can be split
  82. // into multiple frames so we have to append and not overwrite.
  83. f, err := os.OpenFile(fout, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
  84. if err != nil {
  85. log.Fatal(err)
  86. }
  87. if _, err := f.Write(df.Data); err != nil {
  88. log.Fatal(err)
  89. }
  90. if err := f.Close(); err != nil {
  91. log.Fatal(err)
  92. }
  93. }
  94. }
  95. }