golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/trace/cmd/gotraceraw/main.go (about)

     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  // Code generated by "gen.bash" from internal/trace/v2; DO NOT EDIT.
     6  
     7  //go:build go1.21
     8  
     9  package main
    10  
    11  import (
    12  	"flag"
    13  	"fmt"
    14  	"io"
    15  	"log"
    16  	"os"
    17  
    18  	"golang.org/x/exp/trace/internal/raw"
    19  	"golang.org/x/exp/trace/internal/version"
    20  )
    21  
    22  func init() {
    23  	flag.Usage = func() {
    24  		fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [mode]\n", os.Args[0])
    25  		fmt.Fprintf(flag.CommandLine.Output(), "\n")
    26  		fmt.Fprintf(flag.CommandLine.Output(), "Supported modes:")
    27  		fmt.Fprintf(flag.CommandLine.Output(), "\n")
    28  		fmt.Fprintf(flag.CommandLine.Output(), "* text2bytes - converts a text format trace to bytes\n")
    29  		fmt.Fprintf(flag.CommandLine.Output(), "* bytes2text - converts a byte format trace to text\n")
    30  		fmt.Fprintf(flag.CommandLine.Output(), "\n")
    31  		flag.PrintDefaults()
    32  	}
    33  	log.SetFlags(0)
    34  }
    35  
    36  func main() {
    37  	flag.Parse()
    38  	if narg := flag.NArg(); narg != 1 {
    39  		log.Fatal("expected exactly one positional argument: the mode to operate in; see -h output")
    40  	}
    41  
    42  	r := os.Stdin
    43  	w := os.Stdout
    44  
    45  	var tr traceReader
    46  	var tw traceWriter
    47  	var err error
    48  
    49  	switch flag.Arg(0) {
    50  	case "text2bytes":
    51  		tr, err = raw.NewTextReader(r)
    52  		if err != nil {
    53  			log.Fatal(err)
    54  		}
    55  		tw, err = raw.NewWriter(w, tr.Version())
    56  		if err != nil {
    57  			log.Fatal(err)
    58  		}
    59  	case "bytes2text":
    60  		tr, err = raw.NewReader(r)
    61  		if err != nil {
    62  			log.Fatal(err)
    63  		}
    64  		tw, err = raw.NewTextWriter(w, tr.Version())
    65  		if err != nil {
    66  			log.Fatal(err)
    67  		}
    68  	}
    69  	for {
    70  		ev, err := tr.ReadEvent()
    71  		if err == io.EOF {
    72  			break
    73  		}
    74  		if err != nil {
    75  			log.Fatal(err)
    76  		}
    77  		if err := tw.WriteEvent(ev); err != nil {
    78  			log.Fatal(err)
    79  		}
    80  	}
    81  }
    82  
    83  type traceReader interface {
    84  	Version() version.Version
    85  	ReadEvent() (raw.Event, error)
    86  }
    87  
    88  type traceWriter interface {
    89  	WriteEvent(raw.Event) error
    90  }