ls: convert to termstatus

This commit is contained in:
Michael Eischer
2025-09-13 23:53:33 +02:00
parent 2e91e81c83
commit d3e26f2868
2 changed files with 19 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ import (
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui/termstatus"
"github.com/restic/restic/internal/walker" "github.com/restic/restic/internal/walker"
) )
@@ -59,7 +60,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 runLs(cmd.Context(), opts, globalOptions, args) term, cancel := setupTermstatus()
defer cancel()
return runLs(cmd.Context(), opts, globalOptions, args, term)
}, },
} }
opts.AddFlags(cmd.Flags()) opts.AddFlags(cmd.Flags())
@@ -270,15 +273,19 @@ type textLsPrinter struct {
dirs []string dirs []string
ListLong bool ListLong bool
HumanReadable bool HumanReadable bool
termPrinter interface {
P(msg string, args ...interface{})
S(msg string, args ...interface{})
}
} }
func (p *textLsPrinter) Snapshot(sn *restic.Snapshot) error { func (p *textLsPrinter) Snapshot(sn *restic.Snapshot) error {
Verbosef("%v filtered by %v:\n", sn, p.dirs) p.termPrinter.P("%v filtered by %v:", sn, p.dirs)
return nil return nil
} }
func (p *textLsPrinter) Node(path string, node *restic.Node, isPrefixDirectory bool) error { func (p *textLsPrinter) Node(path string, node *restic.Node, isPrefixDirectory bool) error {
if !isPrefixDirectory { if !isPrefixDirectory {
Printf("%s\n", formatNode(path, node, p.ListLong, p.HumanReadable)) p.termPrinter.S("%s", formatNode(path, node, p.ListLong, p.HumanReadable))
} }
return nil return nil
} }
@@ -296,7 +303,9 @@ type toSortOutput struct {
node *restic.Node node *restic.Node
} }
func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []string) error { func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []string, term *termstatus.Terminal) error {
termPrinter := newTerminalProgressPrinter(gopts.JSON, gopts.verbosity, term)
if len(args) == 0 { if len(args) == 0 {
return errors.Fatal("no snapshot ID specified, specify snapshot ID or use special ID 'latest'") return errors.Fatal("no snapshot ID specified, specify snapshot ID or use special ID 'latest'")
} }
@@ -366,7 +375,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri
return err return err
} }
bar := newIndexProgress(gopts.Quiet, gopts.JSON) bar := newIndexTerminalProgress(termPrinter)
if err = repo.LoadIndex(ctx, bar); err != nil { if err = repo.LoadIndex(ctx, bar); err != nil {
return err return err
} }
@@ -386,6 +395,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri
dirs: dirs, dirs: dirs,
ListLong: opts.ListLong, ListLong: opts.ListLong,
HumanReadable: opts.HumanReadable, HumanReadable: opts.HumanReadable,
termPrinter: termPrinter,
} }
} }
if opts.Sort != SortModeName || opts.Reverse { if opts.Sort != SortModeName || opts.Reverse {

View File

@@ -10,12 +10,15 @@ import (
"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"
) )
func testRunLsWithOpts(t testing.TB, gopts GlobalOptions, opts LsOptions, args []string) []byte { func testRunLsWithOpts(t testing.TB, gopts GlobalOptions, opts LsOptions, args []string) []byte {
buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error { buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
gopts.Quiet = true gopts.Quiet = true
return runLs(context.TODO(), opts, gopts, args) return withTermStatus(gopts, func(ctx context.Context, term *termstatus.Terminal) error {
return runLs(context.TODO(), opts, gopts, args, term)
})
}) })
rtest.OK(t, err) rtest.OK(t, err)
return buf.Bytes() return buf.Bytes()