github.com/vipernet-xyz/tm@v0.34.24/scripts/wal2json/main.go (about)

     1  /*
     2  	wal2json converts binary WAL file to JSON.
     3  
     4  	Usage:
     5  			wal2json <path-to-wal>
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"fmt"
    12  	"io"
    13  	"os"
    14  
    15  	cs "github.com/vipernet-xyz/tm/consensus"
    16  	tmjson "github.com/vipernet-xyz/tm/libs/json"
    17  )
    18  
    19  func main() {
    20  	if len(os.Args) < 2 {
    21  		fmt.Println("missing one argument: <path-to-wal>")
    22  		os.Exit(1)
    23  	}
    24  
    25  	f, err := os.Open(os.Args[1])
    26  	if err != nil {
    27  		panic(fmt.Errorf("failed to open WAL file: %v", err))
    28  	}
    29  	defer f.Close()
    30  
    31  	dec := cs.NewWALDecoder(f)
    32  	for {
    33  		msg, err := dec.Decode()
    34  		if err == io.EOF {
    35  			break
    36  		} else if err != nil {
    37  			panic(fmt.Errorf("failed to decode msg: %v", err))
    38  		}
    39  
    40  		json, err := tmjson.Marshal(msg)
    41  		if err != nil {
    42  			panic(fmt.Errorf("failed to marshal msg: %v", err))
    43  		}
    44  
    45  		_, err = os.Stdout.Write(json)
    46  		if err == nil {
    47  			_, err = os.Stdout.Write([]byte("\n"))
    48  		}
    49  
    50  		if err == nil {
    51  			if endMsg, ok := msg.Msg.(cs.EndHeightMessage); ok {
    52  				_, err = os.Stdout.Write([]byte(fmt.Sprintf("ENDHEIGHT %d\n", endMsg.Height)))
    53  			}
    54  		}
    55  
    56  		if err != nil {
    57  			fmt.Println("Failed to write message", err)
    58  			os.Exit(1) //nolint:gocritic
    59  		}
    60  
    61  	}
    62  }