From d9833a87ab15f98026c1579d0e91424d257f0cac Mon Sep 17 00:00:00 2001 From: Malte Poll <1780588+malt3@users.noreply.github.com> Date: Wed, 20 Dec 2023 12:03:47 +0100 Subject: [PATCH] parsesnp: add slog --- tools/parsesnp.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/tools/parsesnp.go b/tools/parsesnp.go index 122b2c48d..9688a849e 100644 --- a/tools/parsesnp.go +++ b/tools/parsesnp.go @@ -5,7 +5,7 @@ import ( "encoding/hex" "fmt" "io" - "log" + "log/slog" "os" "github.com/google/go-sev-guest/abi" @@ -45,13 +45,26 @@ type Report struct { } func main() { + if err := run(); err != nil { + os.Exit(1) + } +} + +func run() (retErr error) { + logger := slog.Default() + defer func() { + if retErr != nil { + logger.Error(retErr.Error()) + } + }() + data, err := io.ReadAll(os.Stdin) if err != nil { - log.Fatalf("failed to read input: %v", err) + return fmt.Errorf("failed to read input: %w", err) } data, err = hex.DecodeString(string(data)) if err != nil { - log.Fatalf("failed to decode input: %v", err) + return fmt.Errorf("failed to decode input: %w", err) } var r Report @@ -79,11 +92,11 @@ func main() { signerInfo, err := abi.ParseSignerInfo(binary.LittleEndian.Uint32(data[0x48:0x4C])) if err != nil { - log.Printf("failed to parse signer info: %v", err) + fmt.Printf("failed to parse signer info: %v", err) } r.SignerInfo = abi.ComposeSignerInfo(signerInfo) if err := mbz(data, 0x4C, 0x50); err != nil { - log.Printf("%v", err) + fmt.Printf("%v", err) } fmt.Printf("SignerInfo: %v\n", r.SignerInfo) r.ReportData = clone(data[0x50:0x90]) @@ -139,6 +152,7 @@ func main() { } r.Signature = clone(data[signatureOffset:abi.ReportSize]) fmt.Printf("Signature: %x\n", r.Signature) + return nil } func clone(b []byte) []byte {