diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-06 10:42:51 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-14 21:31:45 -0600 |
commit | 058f8c36f6bf9ae2600bf7da33d680ced41eae9a (patch) | |
tree | c31b472fb024f5e046c5ed51cbeade831e0454fa | |
parent | f19d908439b3537cc6ae7a21eb926763e90d44ee (diff) |
cmd/btrfs-rec: Have each subcommand call runWithRawFS itself
-rw-r--r-- | cmd/btrfs-rec/inspect_dumptrees.go | 14 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_lsfiles.go | 14 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_lstrees.go | 23 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_mount.go | 19 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_rebuildmappings.go | 30 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_rebuildtrees.go | 12 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_scandevices.go | 12 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_spewitems.go | 14 | ||||
-rw-r--r-- | cmd/btrfs-rec/main.go | 64 |
9 files changed, 83 insertions, 119 deletions
diff --git a/cmd/btrfs-rec/inspect_dumptrees.go b/cmd/btrfs-rec/inspect_dumptrees.go index efdc380..fd152d3 100644 --- a/cmd/btrfs-rec/inspect_dumptrees.go +++ b/cmd/btrfs-rec/inspect_dumptrees.go @@ -16,18 +16,16 @@ import ( ) func init() { - inspectors = append(inspectors, subcommand{ - Command: cobra.Command{ - Use: "dump-trees", - Short: "A clone of `btrfs inspect-internal dump-tree`", - Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - }, - RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error { + inspectors.AddCommand(&cobra.Command{ + Use: "dump-trees", + Short: "A clone of `btrfs inspect-internal dump-tree`", + Args: cliutil.WrapPositionalArgs(cobra.NoArgs), + RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error { const version = "6.1.3" out := os.Stdout textui.Fprintf(out, "btrfs-progs v%v\n", version) dumptrees.DumpTrees(cmd.Context(), out, fs) return nil - }, + }), }) } diff --git a/cmd/btrfs-rec/inspect_lsfiles.go b/cmd/btrfs-rec/inspect_lsfiles.go index 4f985ff..a2b46ab 100644 --- a/cmd/btrfs-rec/inspect_lsfiles.go +++ b/cmd/btrfs-rec/inspect_lsfiles.go @@ -26,13 +26,11 @@ import ( ) func init() { - inspectors = append(inspectors, subcommand{ - Command: cobra.Command{ - Use: "ls-files", - Short: "A listing of all files in the filesystem", - Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - }, - RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) (err error) { + inspectors.AddCommand(&cobra.Command{ + Use: "ls-files", + Short: "A listing of all files in the filesystem", + Args: cliutil.WrapPositionalArgs(cobra.NoArgs), + RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, _ []string) (err error) { out := bufio.NewWriter(os.Stdout) defer func() { if _err := out.Flush(); _err != nil && err == nil { @@ -53,7 +51,7 @@ func init() { }) return nil - }, + }), }) } diff --git a/cmd/btrfs-rec/inspect_lstrees.go b/cmd/btrfs-rec/inspect_lstrees.go index 0c82fe0..be72860 100644 --- a/cmd/btrfs-rec/inspect_lstrees.go +++ b/cmd/btrfs-rec/inspect_lstrees.go @@ -27,13 +27,11 @@ import ( func init() { var scandevicesFilename string - cmd := subcommand{ - Command: cobra.Command{ - Use: "ls-trees", - Short: "A brief view what types of items are in each tree", - Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - }, - RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error { + cmd := &cobra.Command{ + Use: "ls-trees", + Short: "A brief view what types of items are in each tree", + Args: cliutil.WrapPositionalArgs(cobra.NoArgs), + RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error { ctx := cmd.Context() nodeList, err := readNodeList(ctx, scandevicesFilename) if err != nil { @@ -114,11 +112,10 @@ func init() { } return nil - }, + }), } - cmd.Command.Flags().StringVar(&scandevicesFilename, "scandevices", "", "Output of 'scandevices' to use for a lost+found tree") - if err := cmd.Command.MarkFlagFilename("scandevices"); err != nil { - panic(err) - } - inspectors = append(inspectors, cmd) + cmd.Flags().StringVar(&scandevicesFilename, "scandevices", "", "Output of 'scandevices' to use for a lost+found tree") + noError(cmd.MarkFlagFilename("scandevices")) + + inspectors.AddCommand(cmd) } diff --git a/cmd/btrfs-rec/inspect_mount.go b/cmd/btrfs-rec/inspect_mount.go index f3fda34..4582f9f 100644 --- a/cmd/btrfs-rec/inspect_mount.go +++ b/cmd/btrfs-rec/inspect_mount.go @@ -14,17 +14,16 @@ import ( func init() { var skipFileSums bool - cmd := subcommand{ - Command: cobra.Command{ - Use: "mount MOUNTPOINT", - Short: "Mount the filesystem read-only", - Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)), - }, - RunE: func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { + cmd := &cobra.Command{ + Use: "mount MOUNTPOINT", + Short: "Mount the filesystem read-only", + Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)), + RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { return mount.MountRO(cmd.Context(), fs, args[0], skipFileSums) - }, + }), } - cmd.Command.Flags().BoolVar(&skipFileSums, "skip-filesums", false, + cmd.Flags().BoolVar(&skipFileSums, "skip-filesums", false, "ignore checksum failures on file contents; allow such files to be read") - inspectors = append(inspectors, cmd) + + inspectors.AddCommand(cmd) } diff --git a/cmd/btrfs-rec/inspect_rebuildmappings.go b/cmd/btrfs-rec/inspect_rebuildmappings.go index 005fd5d..81660b0 100644 --- a/cmd/btrfs-rec/inspect_rebuildmappings.go +++ b/cmd/btrfs-rec/inspect_rebuildmappings.go @@ -17,21 +17,19 @@ import ( ) func init() { - inspectors = append(inspectors, subcommand{ - Command: cobra.Command{ - Use: "rebuild-mappings SCAN_RESULT.json", - Short: "Rebuild broken chunk/dev-extent/blockgroup trees", - Long: "" + - "The rebuilt information is printed as JSON on stdout, and can\n" + - "be loaded by the --mappings flag.\n" + - "\n" + - "This is very similar to `btrfs rescue chunk-recover`, but (1)\n" + - "does a better job, (2) is less buggy, and (3) doesn't actually\n" + - "write the info back to the filesystem; instead writing it\n" + - "out-of-band to stdout.", - Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)), - }, - RunE: func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { + inspectors.AddCommand(&cobra.Command{ + Use: "rebuild-mappings SCAN_RESULT.json", + Short: "Rebuild broken chunk/dev-extent/blockgroup trees", + Long: "" + + "The rebuilt information is printed as JSON on stdout, and can\n" + + "be loaded by the --mappings flag.\n" + + "\n" + + "This is very similar to `btrfs rescue chunk-recover`, but (1)\n" + + "does a better job, (2) is less buggy, and (3) doesn't actually\n" + + "write the info back to the filesystem; instead writing it\n" + + "out-of-band to stdout.", + Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)), + RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { ctx := cmd.Context() dlog.Infof(ctx, "Reading %q...", args[0]) @@ -56,6 +54,6 @@ func init() { dlog.Info(ctx, "... done writing") return nil - }, + }), }) } diff --git a/cmd/btrfs-rec/inspect_rebuildtrees.go b/cmd/btrfs-rec/inspect_rebuildtrees.go index 5d782cf..8c6fc92 100644 --- a/cmd/btrfs-rec/inspect_rebuildtrees.go +++ b/cmd/btrfs-rec/inspect_rebuildtrees.go @@ -21,12 +21,10 @@ import ( ) func init() { - inspectors = append(inspectors, subcommand{ - Command: cobra.Command{ - Use: "rebuild-nodes NODESCAN.json", - Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)), - }, - RunE: func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { + inspectors.AddCommand(&cobra.Command{ + Use: "rebuild-nodes NODESCAN.json", + Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)), + RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { ctx := cmd.Context() // This is wrapped in a func in order to *ensure* that `nodeList` goes out of scope once @@ -66,6 +64,6 @@ func init() { dlog.Info(ctx, "... done writing") return rebuildErr - }, + }), }) } diff --git a/cmd/btrfs-rec/inspect_scandevices.go b/cmd/btrfs-rec/inspect_scandevices.go index f93d99d..4172c7c 100644 --- a/cmd/btrfs-rec/inspect_scandevices.go +++ b/cmd/btrfs-rec/inspect_scandevices.go @@ -21,12 +21,10 @@ import ( ) func init() { - inspectors = append(inspectors, subcommand{ - Command: cobra.Command{ - Use: "scandevices", - Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - }, - RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) (err error) { + inspectors.AddCommand(&cobra.Command{ + Use: "scandevices", + Args: cliutil.WrapPositionalArgs(cobra.NoArgs), + RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, _ []string) (err error) { ctx := cmd.Context() results, err := rebuildmappings.ScanDevices(ctx, fs) @@ -45,7 +43,7 @@ func init() { dlog.Info(ctx, "... done writing") return nil - }, + }), }) } diff --git a/cmd/btrfs-rec/inspect_spewitems.go b/cmd/btrfs-rec/inspect_spewitems.go index 4abb2b0..d8a65ae 100644 --- a/cmd/btrfs-rec/inspect_spewitems.go +++ b/cmd/btrfs-rec/inspect_spewitems.go @@ -19,13 +19,11 @@ import ( ) 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 { + 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() @@ -51,6 +49,6 @@ func init() { }, }) return nil - }, + }), }) } diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index 372dee6..5da8f52 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -21,12 +21,27 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) -type subcommand struct { - cobra.Command - RunE func(*btrfs.FS, *cobra.Command, []string) error -} +var ( + inspectors = &cobra.Command{ + Use: "inspect {[flags]|SUBCOMMAND}", + Short: "Inspect (but don't modify) a broken btrfs filesystem", -var inspectors, repairers []subcommand + Args: cliutil.WrapPositionalArgs(cliutil.OnlySubcommands), + RunE: cliutil.RunSubcommands, + } + repairers = &cobra.Command{ + Use: "repair {[flags]|SUBCOMMAND}", + Short: "Repair a broken btrfs filesystem", + + Args: cliutil.WrapPositionalArgs(cliutil.OnlySubcommands), + RunE: cliutil.RunSubcommands, + + PersistentPreRunE: func(_ *cobra.Command, _ []string) error { + globalFlags.openFlag = os.O_RDWR + return nil + }, + } +) var globalFlags struct { logLevel textui.LogLevelFlag @@ -82,43 +97,8 @@ func main() { // Sub-commands - argparserInspect := &cobra.Command{ - Use: "inspect {[flags]|SUBCOMMAND}", - Short: "Inspect (but don't modify) a broken btrfs filesystem", - - Args: cliutil.WrapPositionalArgs(cliutil.OnlySubcommands), - RunE: cliutil.RunSubcommands, - } - argparser.AddCommand(argparserInspect) - - argparserRepair := &cobra.Command{ - Use: "repair {[flags]|SUBCOMMAND}", - Short: "Repair a broken btrfs filesystem", - - Args: cliutil.WrapPositionalArgs(cliutil.OnlySubcommands), - RunE: cliutil.RunSubcommands, - - PersistentPreRunE: func(_ *cobra.Command, _ []string) error { - globalFlags.openFlag = os.O_RDWR - return nil - }, - } - argparser.AddCommand(argparserRepair) - - for _, cmdgrp := range []struct { - parent *cobra.Command - children []subcommand - }{ - {argparserInspect, inspectors}, - {argparserRepair, repairers}, - } { - for _, child := range cmdgrp.children { - cmd := child.Command - runE := child.RunE - cmd.RunE = runWithRawFS(runE) - cmdgrp.parent.AddCommand(&cmd) - } - } + argparser.AddCommand(inspectors) + argparser.AddCommand(repairers) // Run |