Source file src/internal/trace/testdata/cmd/gotraceraw/main.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"io"
    11  	"log"
    12  	"os"
    13  
    14  	"internal/trace/raw"
    15  	"internal/trace/version"
    16  )
    17  
    18  func init() {
    19  	flag.Usage = func() {
    20  		fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [mode]\n", os.Args[0])
    21  		fmt.Fprintf(flag.CommandLine.Output(), "\n")
    22  		fmt.Fprintf(flag.CommandLine.Output(), "Supported modes:")
    23  		fmt.Fprintf(flag.CommandLine.Output(), "\n")
    24  		fmt.Fprintf(flag.CommandLine.Output(), "* text2bytes - converts a text format trace to bytes\n")
    25  		fmt.Fprintf(flag.CommandLine.Output(), "* bytes2text - converts a byte format trace to text\n")
    26  		fmt.Fprintf(flag.CommandLine.Output(), "\n")
    27  		flag.PrintDefaults()
    28  	}
    29  	log.SetFlags(0)
    30  }
    31  
    32  func main() {
    33  	flag.Parse()
    34  	if narg := flag.NArg(); narg != 1 {
    35  		log.Fatal("expected exactly one positional argument: the mode to operate in; see -h output")
    36  	}
    37  
    38  	r := os.Stdin
    39  	w := os.Stdout
    40  
    41  	var tr traceReader
    42  	var tw traceWriter
    43  	var err error
    44  
    45  	switch flag.Arg(0) {
    46  	case "text2bytes":
    47  		tr, err = raw.NewTextReader(r)
    48  		if err != nil {
    49  			log.Fatal(err)
    50  		}
    51  		tw, err = raw.NewWriter(w, tr.Version())
    52  		if err != nil {
    53  			log.Fatal(err)
    54  		}
    55  	case "bytes2text":
    56  		tr, err = raw.NewReader(r)
    57  		if err != nil {
    58  			log.Fatal(err)
    59  		}
    60  		tw, err = raw.NewTextWriter(w, tr.Version())
    61  		if err != nil {
    62  			log.Fatal(err)
    63  		}
    64  	}
    65  	for {
    66  		ev, err := tr.ReadEvent()
    67  		if err == io.EOF {
    68  			break
    69  		}
    70  		if err != nil {
    71  			log.Fatal(err)
    72  		}
    73  		if err := tw.WriteEvent(ev); err != nil {
    74  			log.Fatal(err)
    75  		}
    76  	}
    77  }
    78  
    79  type traceReader interface {
    80  	Version() version.Version
    81  	ReadEvent() (raw.Event, error)
    82  }
    83  
    84  type traceWriter interface {
    85  	WriteEvent(raw.Event) error
    86  }
    87  

View as plain text