main.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "bufio"
  16. "encoding/json"
  17. "fmt"
  18. "io"
  19. "os"
  20. )
  21. func main() {
  22. err := extractRawLog(os.Stdin)
  23. if err != nil {
  24. panic(err)
  25. }
  26. }
  27. // A json log entry contains keys such as "Time", "Action", "Package" and "Output".
  28. // We are only interested in "Output", which is the raw log.
  29. type jsonLog struct {
  30. Output string `json:"output,omitempty"`
  31. }
  32. // jsonToRawLog converts a single line of json formatted log to raw log.
  33. // If there is an error, it returns the original input.
  34. func jsonToRawLog(line string) (string, error) {
  35. var log jsonLog
  36. if err := json.Unmarshal([]byte(line), &log); err != nil {
  37. return line, err
  38. }
  39. return log.Output, nil
  40. }
  41. func extractRawLog(r io.Reader) error {
  42. scan := bufio.NewScanner(r)
  43. for scan.Scan() {
  44. l, _ := jsonToRawLog(scan.Text())
  45. // Print the raw log to stdout.
  46. fmt.Println(l)
  47. }
  48. if err := scan.Err(); err != nil {
  49. return err
  50. }
  51. return nil
  52. }