mount: convert to termstatus

This commit is contained in:
Michael Eischer
2025-09-13 23:48:33 +02:00
parent 0dcd9bee88
commit 2e91e81c83
2 changed files with 17 additions and 9 deletions

View File

@@ -16,6 +16,7 @@ import (
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui/termstatus"
"github.com/restic/restic/internal/fuse" "github.com/restic/restic/internal/fuse"
@@ -81,7 +82,9 @@ Exit status is 12 if the password is incorrect.
DisableAutoGenTag: true, DisableAutoGenTag: true,
GroupID: cmdGroupDefault, GroupID: cmdGroupDefault,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runMount(cmd.Context(), opts, globalOptions, args) term, cancel := setupTermstatus()
defer cancel()
return runMount(cmd.Context(), opts, globalOptions, args, term)
}, },
} }
@@ -112,7 +115,9 @@ func (opts *MountOptions) AddFlags(f *pflag.FlagSet) {
_ = f.MarkDeprecated("snapshot-template", "use --time-template") _ = f.MarkDeprecated("snapshot-template", "use --time-template")
} }
func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args []string) error { func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args []string, term *termstatus.Terminal) error {
printer := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term)
if opts.TimeTemplate == "" { if opts.TimeTemplate == "" {
return errors.Fatal("time template string cannot be empty") return errors.Fatal("time template string cannot be empty")
} }
@@ -130,7 +135,7 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args
// Check the existence of the mount point at the earliest stage to // Check the existence of the mount point at the earliest stage to
// prevent unnecessary computations while opening the repository. // prevent unnecessary computations while opening the repository.
if _, err := os.Stat(mountpoint); errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(mountpoint); errors.Is(err, os.ErrNotExist) {
Verbosef("Mountpoint %s doesn't exist\n", mountpoint) printer.P("Mountpoint %s doesn't exist", mountpoint)
return err return err
} }
@@ -143,7 +148,7 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args
} }
defer unlock() defer unlock()
bar := newIndexProgress(gopts.Quiet, gopts.JSON) bar := newIndexTerminalProgress(printer)
err = repo.LoadIndex(ctx, bar) err = repo.LoadIndex(ctx, bar)
if err != nil { if err != nil {
return err return err
@@ -183,9 +188,9 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args
} }
root := fuse.NewRoot(repo, cfg) root := fuse.NewRoot(repo, cfg)
Printf("Now serving the repository at %s\n", mountpoint) printer.S("Now serving the repository at %s", mountpoint)
Printf("Use another terminal or tool to browse the contents of this folder.\n") printer.S("Use another terminal or tool to browse the contents of this folder.")
Printf("When finished, quit with Ctrl-c here or umount the mountpoint.\n") printer.S("When finished, quit with Ctrl-c here or umount the mountpoint.")
debug.Log("serving mount at %v", mountpoint) debug.Log("serving mount at %v", mountpoint)
@@ -201,7 +206,7 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args
debug.Log("running umount cleanup handler for mount at %v", mountpoint) debug.Log("running umount cleanup handler for mount at %v", mountpoint)
err := systemFuse.Unmount(mountpoint) err := systemFuse.Unmount(mountpoint)
if err != nil { if err != nil {
Warnf("unable to umount (maybe already umounted or still in use?): %v\n", err) printer.E("unable to umount (maybe already umounted or still in use?): %v", err)
} }
return ErrOK return ErrOK

View File

@@ -16,6 +16,7 @@ import (
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/ui/termstatus"
) )
const ( const (
@@ -61,7 +62,9 @@ func testRunMount(t testing.TB, gopts GlobalOptions, dir string, wg *sync.WaitGr
opts := MountOptions{ opts := MountOptions{
TimeTemplate: time.RFC3339, TimeTemplate: time.RFC3339,
} }
rtest.OK(t, runMount(context.TODO(), opts, gopts, []string{dir})) rtest.OK(t, withTermStatus(gopts, func(ctx context.Context, term *termstatus.Terminal) error {
return runMount(context.TODO(), opts, gopts, []string{dir}, term)
}))
} }
func testRunUmount(t testing.TB, dir string) { func testRunUmount(t testing.TB, dir string) {