From 79b676aa986cf67d6727261a9c0f96b4121eca31 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 28 Nov 2026 15:33:35 +0100 Subject: [PATCH] cat: always use raw output This avoids quoting of JSON data. --- cmd/restic/cmd_cat.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/cmd/restic/cmd_cat.go b/cmd/restic/cmd_cat.go index f8a33cabd..aa6d66af5 100644 --- a/cmd/restic/cmd_cat.go +++ b/cmd/restic/cmd_cat.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "io" "slices" "strings" @@ -92,16 +93,17 @@ func runCat(ctx context.Context, gopts global.Options, args []string, term ui.Te return err } - printer.S(string(buf)) - return nil + return writeWithNewline(term.OutputRaw(), buf) + case "index": buf, err := repo.LoadUnpacked(ctx, restic.IndexFile, id) if err != nil { return err } - printer.S(string(buf)) - return nil + _, err = term.OutputRaw().Write(buf) + return err + case "snapshot": sn, _, err := data.FindSnapshot(ctx, repo, repo, args[1]) if err != nil { @@ -113,8 +115,8 @@ func runCat(ctx context.Context, gopts global.Options, args []string, term ui.Te return err } - printer.S(string(buf)) - return nil + return writeWithNewline(term.OutputRaw(), buf) + case "key": key, err := repository.LoadKey(ctx, repo, id) if err != nil { @@ -126,16 +128,16 @@ func runCat(ctx context.Context, gopts global.Options, args []string, term ui.Te return err } - printer.S(string(buf)) - return nil + return writeWithNewline(term.OutputRaw(), buf) + case "masterkey": buf, err := json.MarshalIndent(repo.Key(), "", " ") if err != nil { return err } - printer.S(string(buf)) - return nil + return writeWithNewline(term.OutputRaw(), buf) + case "lock": lock, err := repository.LoadLock(ctx, repo, id) if err != nil { @@ -147,8 +149,7 @@ func runCat(ctx context.Context, gopts global.Options, args []string, term ui.Te return err } - printer.S(string(buf)) - return nil + return writeWithNewline(term.OutputRaw(), buf) case "pack": buf, err := repo.LoadRaw(ctx, restic.PackFile, id) @@ -214,3 +215,11 @@ func runCat(ctx context.Context, gopts global.Options, args []string, term ui.Te return errors.Fatal("invalid type") } } + +func writeWithNewline(w io.Writer, buf []byte) error { + if _, err := w.Write(buf); err != nil { + return err + } + _, err := w.Write([]byte{'\n'}) + return err +}