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/fs"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui/termstatus"
"github.com/restic/restic/internal/walker"
)
@@ -59,7 +60,9 @@ Exit status is 12 if the password is incorrect.
DisableAutoGenTag: true,
GroupID: cmdGroupDefault,
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())
@@ -270,15 +273,19 @@ type textLsPrinter struct {
dirs []string
ListLong bool
HumanReadable bool
termPrinter interface {
P(msg string, args ...interface{})
S(msg string, args ...interface{})
}
}
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
}
func (p *textLsPrinter) Node(path string, node *restic.Node, isPrefixDirectory bool) error {
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
}
@@ -296,7 +303,9 @@ type toSortOutput struct {
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 {
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
}
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
bar := newIndexTerminalProgress(termPrinter)
if err = repo.LoadIndex(ctx, bar); err != nil {
return err
}
@@ -386,6 +395,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri
dirs: dirs,
ListLong: opts.ListLong,
HumanReadable: opts.HumanReadable,
termPrinter: termPrinter,
}
}
if opts.Sort != SortModeName || opts.Reverse {

View File

@@ -10,12 +10,15 @@ import (
"github.com/restic/restic/internal/restic"
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 {
buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
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)
return buf.Bytes()