summaryrefslogtreecommitdiff
path: root/cmd/btrfs-rec/inspect_spewitems.go
blob: c3a1e6b84705a28f6334ee9768c2039c7e931df6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package main

import (
	"os"

	"github.com/datawire/dlib/dlog"
	"github.com/datawire/ocibuild/pkg/cliutil"
	"github.com/davecgh/go-spew/spew"
	"github.com/spf13/cobra"

	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfstree"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfsutil"
	"git.lukeshu.com/btrfs-progs-ng/lib/textui"
)

func init() {
	inspectors.AddCommand(&cobra.Command{
		Use:   "spew-items",
		Short: "Spew all items as parsed",
		Args:  cliutil.WrapPositionalArgs(cobra.NoArgs),
		RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error {
			ctx := cmd.Context()

			spew := spew.NewDefaultConfig()
			spew.DisablePointerAddresses = true

			btrfsutil.WalkAllTrees(ctx, fs, btrfsutil.WalkAllTreesHandler{
				Err: func(err *btrfsutil.WalkError) {
					dlog.Error(ctx, err)
				},
				TreeWalkHandler: btrfstree.TreeWalkHandler{
					Item: func(path btrfstree.Path, item btrfstree.Item) {
						textui.Fprintf(os.Stdout, "%s = ", path)
						spew.Dump(item)
						_, _ = os.Stdout.WriteString("\n")
					},
					BadItem: func(path btrfstree.Path, item btrfstree.Item) {
						textui.Fprintf(os.Stdout, "%s = ", path)
						spew.Dump(item)
						_, _ = os.Stdout.WriteString("\n")
					},
				},
			})
			return nil
		}),
	})
}