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