summaryrefslogtreecommitdiff
path: root/cmd/btrfs-rec/inspect_spewitems.go
blob: 3b79e8b577c85d9ae4504153fe411526bdb71acd (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
53
54
55
56
// 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/btrfsprogs/btrfsutil"
	"git.lukeshu.com/btrfs-progs-ng/lib/textui"
)

func init() {
	inspectors = append(inspectors, subcommand{
		Command: cobra.Command{
			Use:   "spew-items",
			Short: "Spew all items as parsed",
			Args:  cliutil.WrapPositionalArgs(cobra.NoArgs),
		},
		RunE: 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.TreePath, item btrfstree.Item) error {
						textui.Fprintf(os.Stdout, "%s = ", path)
						spew.Dump(item)
						_, _ = os.Stdout.WriteString("\n")
						return nil
					},
					BadItem: func(path btrfstree.TreePath, item btrfstree.Item) error {
						textui.Fprintf(os.Stdout, "%s = ", path)
						spew.Dump(item)
						_, _ = os.Stdout.WriteString("\n")
						return nil
					},
				},
			})
			return nil
		},
	})
}