always access stdout/stderr via globalOptions

This commit is contained in:
Michael Eischer 2023-05-07 22:21:56 +02:00
parent 4b3a0b4104
commit 7a268e4aba
9 changed files with 16 additions and 21 deletions

View file

@ -24,7 +24,6 @@ func testRunBackupAssumeFailure(t testing.TB, dir string, target []string, opts
term := termstatus.New(gopts.stdout, gopts.stderr, gopts.Quiet) term := termstatus.New(gopts.stdout, gopts.stderr, gopts.Quiet)
wg.Go(func() error { term.Run(ctx); return nil }) wg.Go(func() error { term.Run(ctx); return nil })
gopts.stdout = io.Discard
t.Logf("backing up %v in %v", target, dir) t.Logf("backing up %v in %v", target, dir)
if dir != "" { if dir != "" {
cleanup := rtest.Chdir(t, dir) cleanup := rtest.Chdir(t, dir)
@ -371,9 +370,7 @@ func TestBackupErrors(t *testing.T) {
rtest.OK(t, os.Chmod(inaccessibleFile, 0644)) rtest.OK(t, os.Chmod(inaccessibleFile, 0644))
}() }()
opts := BackupOptions{} opts := BackupOptions{}
gopts := env.gopts err := testRunBackupAssumeFailure(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts)
gopts.stderr = io.Discard
err := testRunBackupAssumeFailure(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, gopts)
rtest.Assert(t, err != nil, "Assumed failure, but no error occurred.") rtest.Assert(t, err != nil, "Assumed failure, but no error occurred.")
rtest.Assert(t, err == ErrInvalidSourceData, "Wrong error returned") rtest.Assert(t, err == ErrInvalidSourceData, "Wrong error returned")
testListSnapshots(t, env.gopts, 1) testListSnapshots(t, env.gopts, 1)

View file

@ -155,7 +155,7 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error {
}) })
} }
_ = tab.Write(gopts.stdout) _ = tab.Write(globalOptions.stdout)
Printf("%d cache dirs in %s\n", len(dirs), cachedir) Printf("%d cache dirs in %s\n", len(dirs), cachedir)
return nil return nil

View file

@ -167,20 +167,20 @@ func runDebugDump(ctx context.Context, gopts GlobalOptions, args []string) error
switch tpe { switch tpe {
case "indexes": case "indexes":
return dumpIndexes(ctx, repo, gopts.stdout) return dumpIndexes(ctx, repo, globalOptions.stdout)
case "snapshots": case "snapshots":
return debugPrintSnapshots(ctx, repo, gopts.stdout) return debugPrintSnapshots(ctx, repo, globalOptions.stdout)
case "packs": case "packs":
return printPacks(ctx, repo, gopts.stdout) return printPacks(ctx, repo, globalOptions.stdout)
case "all": case "all":
Printf("snapshots:\n") Printf("snapshots:\n")
err := debugPrintSnapshots(ctx, repo, gopts.stdout) err := debugPrintSnapshots(ctx, repo, globalOptions.stdout)
if err != nil { if err != nil {
return err return err
} }
Printf("\nindexes:\n") Printf("\nindexes:\n")
err = dumpIndexes(ctx, repo, gopts.stdout) err = dumpIndexes(ctx, repo, globalOptions.stdout)
if err != nil { if err != nil {
return err return err
} }

View file

@ -381,7 +381,7 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
} }
if gopts.JSON { if gopts.JSON {
enc := json.NewEncoder(gopts.stdout) enc := json.NewEncoder(globalOptions.stdout)
c.printChange = func(change *Change) { c.printChange = func(change *Change) {
err := enc.Encode(change) err := enc.Encode(change)
if err != nil { if err != nil {
@ -415,7 +415,7 @@ func runDiff(ctx context.Context, opts DiffOptions, gopts GlobalOptions, args []
updateBlobs(repo, stats.BlobsAfter.Sub(both).Sub(stats.BlobsCommon), &stats.Added) updateBlobs(repo, stats.BlobsAfter.Sub(both).Sub(stats.BlobsCommon), &stats.Added)
if gopts.JSON { if gopts.JSON {
err := json.NewEncoder(gopts.stdout).Encode(stats) err := json.NewEncoder(globalOptions.stdout).Encode(stats)
if err != nil { if err != nil {
Warnf("JSON encode failed: %v\n", err) Warnf("JSON encode failed: %v\n", err)
} }

View file

@ -16,8 +16,6 @@ import (
func testRunDiffOutput(gopts GlobalOptions, firstSnapshotID string, secondSnapshotID string) (string, error) { func testRunDiffOutput(gopts GlobalOptions, firstSnapshotID string, secondSnapshotID string) (string, error) {
buf, err := withCaptureStdout(func() error { buf, err := withCaptureStdout(func() error {
gopts.stdout = globalOptions.stdout
opts := DiffOptions{ opts := DiffOptions{
ShowMetadata: false, ShowMetadata: false,
} }

View file

@ -193,7 +193,7 @@ func runForget(ctx context.Context, opts ForgetOptions, gopts GlobalOptions, arg
for k, snapshotGroup := range snapshotGroups { for k, snapshotGroup := range snapshotGroups {
if gopts.Verbose >= 1 && !gopts.JSON { if gopts.Verbose >= 1 && !gopts.JSON {
err = PrintSnapshotGroupHeader(gopts.stdout, k) err = PrintSnapshotGroupHeader(globalOptions.stdout, k)
if err != nil { if err != nil {
return err return err
} }
@ -250,7 +250,7 @@ func runForget(ctx context.Context, opts ForgetOptions, gopts GlobalOptions, arg
} }
if gopts.JSON && len(jsonGroups) > 0 { if gopts.JSON && len(jsonGroups) > 0 {
err = printJSONForget(gopts.stdout, jsonGroups) err = printJSONForget(globalOptions.stdout, jsonGroups)
if err != nil { if err != nil {
return err return err
} }

View file

@ -123,7 +123,7 @@ func runInit(ctx context.Context, opts InitOptions, gopts GlobalOptions, args []
ID: s.Config().ID, ID: s.Config().ID,
Repository: location.StripPassword(gopts.Repo), Repository: location.StripPassword(gopts.Repo),
} }
return json.NewEncoder(gopts.stdout).Encode(status) return json.NewEncoder(globalOptions.stdout).Encode(status)
} }
return nil return nil

View file

@ -181,7 +181,7 @@ func runLs(ctx context.Context, opts LsOptions, gopts GlobalOptions, args []stri
) )
if gopts.JSON { if gopts.JSON {
enc := json.NewEncoder(gopts.stdout) enc := json.NewEncoder(globalOptions.stdout)
printSnapshot = func(sn *restic.Snapshot) { printSnapshot = func(sn *restic.Snapshot) {
err := enc.Encode(lsSnapshot{ err := enc.Encode(lsSnapshot{

View file

@ -94,7 +94,7 @@ func runSnapshots(ctx context.Context, opts SnapshotOptions, gopts GlobalOptions
} }
if gopts.JSON { if gopts.JSON {
err := printSnapshotGroupJSON(gopts.stdout, snapshotGroups, grouped) err := printSnapshotGroupJSON(globalOptions.stdout, snapshotGroups, grouped)
if err != nil { if err != nil {
Warnf("error printing snapshots: %v\n", err) Warnf("error printing snapshots: %v\n", err)
} }
@ -103,13 +103,13 @@ func runSnapshots(ctx context.Context, opts SnapshotOptions, gopts GlobalOptions
for k, list := range snapshotGroups { for k, list := range snapshotGroups {
if grouped { if grouped {
err := PrintSnapshotGroupHeader(gopts.stdout, k) err := PrintSnapshotGroupHeader(globalOptions.stdout, k)
if err != nil { if err != nil {
Warnf("error printing snapshots: %v\n", err) Warnf("error printing snapshots: %v\n", err)
return nil return nil
} }
} }
PrintSnapshots(gopts.stdout, list, nil, opts.Compact) PrintSnapshots(globalOptions.stdout, list, nil, opts.Compact)
} }
return nil return nil