diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-10 22:02:23 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-14 21:31:45 -0600 |
commit | afd2fe91ec604491bd8978b1880c6482e7394240 (patch) | |
tree | 7b8275fe12a2f2d7c57f1e9419b97e693b715f36 /cmd | |
parent | 3949bd3ff240bc6d06dd08f6e3183e72571e0e1d (diff) |
cmd/btrfs-rec: inspect rebuild-mappings list-nodes: Set up logging and stuff
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/btrfs-rec/inspect_rebuildmappings.go | 4 | ||||
-rw-r--r-- | cmd/btrfs-rec/main.go | 59 |
2 files changed, 38 insertions, 25 deletions
diff --git a/cmd/btrfs-rec/inspect_rebuildmappings.go b/cmd/btrfs-rec/inspect_rebuildmappings.go index b215e7a..43c45b1 100644 --- a/cmd/btrfs-rec/inspect_rebuildmappings.go +++ b/cmd/btrfs-rec/inspect_rebuildmappings.go @@ -129,7 +129,7 @@ func init() { "advantage of using previously read data from " + "`btrfs-rec inspect rebuild-nodes scan`.", Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)), - RunE: func(cmd *cobra.Command, args []string) error { + RunE: run(func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() scanResults, err := readJSONFile[rebuildmappings.ScanDevicesResult](ctx, args[0]) @@ -159,7 +159,7 @@ func init() { dlog.Info(ctx, "... done writing") return nil - }, + }), }) inspectors.AddCommand(cmd) diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index 343a41f..15f1964 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -107,7 +107,7 @@ func main() { } } -func runWithRawFS(runE func(*btrfs.FS, *cobra.Command, []string) error) func(*cobra.Command, []string) error { +func run(runE func(*cobra.Command, []string) error) func(*cobra.Command, []string) error { return func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() logger := textui.NewLogger(os.Stderr, globalFlags.logLevel.Level) @@ -126,37 +126,50 @@ func runWithRawFS(runE func(*btrfs.FS, *cobra.Command, []string) error) func(*co err = _err } } + defer func() { maybeSetErr(globalFlags.stopProfiling()) }() - if len(globalFlags.pvs) == 0 { - // We do this here instead of calling argparser.MarkPersistentFlagRequired("pv") so that - // it doesn't interfere with the `help` sub-command. - return cliutil.FlagErrorFunc(cmd, fmt.Errorf("must specify 1 or more physical volumes with --pv")) + cmd.SetContext(ctx) + return runE(cmd, args) + }) + return grp.Wait() + } +} + +func runWithRawFS(runE func(*btrfs.FS, *cobra.Command, []string) error) func(*cobra.Command, []string) error { + return run(func(cmd *cobra.Command, args []string) (err error) { + maybeSetErr := func(_err error) { + if _err != nil && err == nil { + err = _err } - fs, err := btrfsutil.Open(ctx, globalFlags.openFlag, globalFlags.pvs...) + } + + if len(globalFlags.pvs) == 0 { + // We do this here instead of calling argparser.MarkPersistentFlagRequired("pv") so that + // it doesn't interfere with the `help` sub-command. + return cliutil.FlagErrorFunc(cmd, fmt.Errorf("must specify 1 or more physical volumes with --pv")) + } + fs, err := btrfsutil.Open(cmd.Context(), globalFlags.openFlag, globalFlags.pvs...) + if err != nil { + return err + } + defer func() { + maybeSetErr(fs.Close()) + }() + + if globalFlags.mappings != "" { + mappingsJSON, err := readJSONFile[[]btrfsvol.Mapping](cmd.Context(), globalFlags.mappings) if err != nil { return err } - defer func() { - maybeSetErr(fs.Close()) - }() - - if globalFlags.mappings != "" { - mappingsJSON, err := readJSONFile[[]btrfsvol.Mapping](ctx, globalFlags.mappings) - if err != nil { + for _, mapping := range mappingsJSON { + if err := fs.LV.AddMapping(mapping); err != nil { return err } - for _, mapping := range mappingsJSON { - if err := fs.LV.AddMapping(mapping); err != nil { - return err - } - } } + } - cmd.SetContext(ctx) - return runE(fs, cmd, args) - }) - return grp.Wait() - } + return runE(fs, cmd, args) + }) } |