From 013c565c299f4239cb3710b9afbfeafe2418e823 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Thu, 18 Sep 2025 22:19:38 +0200 Subject: [PATCH] standardize shorten variable name for GlobalOptions to gopts --- cmd/restic/cmd_init_integration_test.go | 8 ++-- cmd/restic/cmd_list_integration_test.go | 10 ++-- cmd/restic/cmd_restore_integration_test.go | 4 +- cmd/restic/global.go | 54 +++++++++++----------- cmd/restic/global_test.go | 18 ++++---- 5 files changed, 47 insertions(+), 47 deletions(-) diff --git a/cmd/restic/cmd_init_integration_test.go b/cmd/restic/cmd_init_integration_test.go index 878049ea6..2fbe88fc4 100644 --- a/cmd/restic/cmd_init_integration_test.go +++ b/cmd/restic/cmd_init_integration_test.go @@ -12,20 +12,20 @@ import ( "github.com/restic/restic/internal/ui/progress" ) -func testRunInit(t testing.TB, opts GlobalOptions) { +func testRunInit(t testing.TB, gopts GlobalOptions) { repository.TestUseLowSecurityKDFParameters(t) restic.TestDisableCheckPolynomial(t) restic.TestSetLockTimeout(t, 0) - err := withTermStatus(opts, func(ctx context.Context, gopts GlobalOptions) error { + err := withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error { return runInit(ctx, InitOptions{}, gopts, nil, gopts.term) }) rtest.OK(t, err) - t.Logf("repository initialized at %v", opts.Repo) + t.Logf("repository initialized at %v", gopts.Repo) // create temporary junk files to verify that restic does not trip over them for _, path := range []string{"index", "snapshots", "keys", "locks", filepath.Join("data", "00")} { - rtest.OK(t, os.WriteFile(filepath.Join(opts.Repo, path, "tmp12345"), []byte("junk file"), 0o600)) + rtest.OK(t, os.WriteFile(filepath.Join(gopts.Repo, path, "tmp12345"), []byte("junk file"), 0o600)) } } diff --git a/cmd/restic/cmd_list_integration_test.go b/cmd/restic/cmd_list_integration_test.go index 5b1409e9c..58b240c5c 100644 --- a/cmd/restic/cmd_list_integration_test.go +++ b/cmd/restic/cmd_list_integration_test.go @@ -10,9 +10,9 @@ import ( rtest "github.com/restic/restic/internal/test" ) -func testRunList(t testing.TB, opts GlobalOptions, tpe string) restic.IDs { - buf, err := withCaptureStdout(opts, func(opts GlobalOptions) error { - return withTermStatus(opts, func(ctx context.Context, gopts GlobalOptions) error { +func testRunList(t testing.TB, gopts GlobalOptions, tpe string) restic.IDs { + buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error { + return withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error { return runList(ctx, gopts, []string{tpe}, gopts.term) }) }) @@ -38,9 +38,9 @@ func parseIDsFromReader(t testing.TB, rd io.Reader) restic.IDs { return IDs } -func testListSnapshots(t testing.TB, opts GlobalOptions, expected int) restic.IDs { +func testListSnapshots(t testing.TB, gopts GlobalOptions, expected int) restic.IDs { t.Helper() - snapshotIDs := testRunList(t, opts, "snapshots") + snapshotIDs := testRunList(t, gopts, "snapshots") rtest.Assert(t, len(snapshotIDs) == expected, "expected %v snapshot, got %v", expected, snapshotIDs) return snapshotIDs } diff --git a/cmd/restic/cmd_restore_integration_test.go b/cmd/restic/cmd_restore_integration_test.go index 9182f06aa..3c2403685 100644 --- a/cmd/restic/cmd_restore_integration_test.go +++ b/cmd/restic/cmd_restore_integration_test.go @@ -16,8 +16,8 @@ import ( rtest "github.com/restic/restic/internal/test" ) -func testRunRestore(t testing.TB, opts GlobalOptions, dir string, snapshotID string) { - testRunRestoreExcludes(t, opts, dir, snapshotID, nil) +func testRunRestore(t testing.TB, gopts GlobalOptions, dir string, snapshotID string) { + testRunRestoreExcludes(t, gopts, dir, snapshotID, nil) } func testRunRestoreExcludes(t testing.TB, gopts GlobalOptions, dir string, snapshotID string, excludes []string) { diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 9872f0070..b31c37515 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -243,16 +243,16 @@ func readPassword(in io.Reader) (password string, err error) { // ReadPassword reads the password from a password file, the environment // variable RESTIC_PASSWORD or prompts the user. If the context is canceled, // the function leaks the password reading goroutine. -func ReadPassword(ctx context.Context, opts GlobalOptions, prompt string, printer progress.Printer) (string, error) { - if opts.InsecureNoPassword { - if opts.password != "" { +func ReadPassword(ctx context.Context, gopts GlobalOptions, prompt string, printer progress.Printer) (string, error) { + if gopts.InsecureNoPassword { + if gopts.password != "" { return "", errors.Fatal("--insecure-no-password must not be specified together with providing a password via a cli option or environment variable") } return "", nil } - if opts.password != "" { - return opts.password, nil + if gopts.password != "" { + return gopts.password, nil } var ( @@ -260,7 +260,7 @@ func ReadPassword(ctx context.Context, opts GlobalOptions, prompt string, printe err error ) - if opts.term.InputIsTerminal() { + if gopts.term.InputIsTerminal() { password, err = terminal.ReadPassword(ctx, os.Stdin, os.Stderr, prompt) } else { printer.PT("reading repository password from stdin") @@ -300,20 +300,20 @@ func ReadPasswordTwice(ctx context.Context, gopts GlobalOptions, prompt1, prompt return pw1, nil } -func ReadRepo(opts GlobalOptions) (string, error) { - if opts.Repo == "" && opts.RepositoryFile == "" { +func ReadRepo(gopts GlobalOptions) (string, error) { + if gopts.Repo == "" && gopts.RepositoryFile == "" { return "", errors.Fatal("Please specify repository location (-r or --repository-file)") } - repo := opts.Repo - if opts.RepositoryFile != "" { + repo := gopts.Repo + if gopts.RepositoryFile != "" { if repo != "" { return "", errors.Fatal("Options -r and --repository-file are mutually exclusive, please specify only one") } - s, err := textfile.Read(opts.RepositoryFile) + s, err := textfile.Read(gopts.RepositoryFile) if errors.Is(err, os.ErrNotExist) { - return "", errors.Fatalf("%s does not exist", opts.RepositoryFile) + return "", errors.Fatalf("%s does not exist", gopts.RepositoryFile) } if err != nil { return "", err @@ -328,47 +328,47 @@ func ReadRepo(opts GlobalOptions) (string, error) { const maxKeys = 20 // OpenRepository reads the password and opens the repository. -func OpenRepository(ctx context.Context, opts GlobalOptions, printer progress.Printer) (*repository.Repository, error) { - repo, err := ReadRepo(opts) +func OpenRepository(ctx context.Context, gopts GlobalOptions, printer progress.Printer) (*repository.Repository, error) { + repo, err := ReadRepo(gopts) if err != nil { return nil, err } - be, err := open(ctx, repo, opts, opts.extended, printer) + be, err := open(ctx, repo, gopts, gopts.extended, printer) if err != nil { return nil, err } s, err := repository.New(be, repository.Options{ - Compression: opts.Compression, - PackSize: opts.PackSize * 1024 * 1024, - NoExtraVerify: opts.NoExtraVerify, + Compression: gopts.Compression, + PackSize: gopts.PackSize * 1024 * 1024, + NoExtraVerify: gopts.NoExtraVerify, }) if err != nil { return nil, errors.Fatalf("%s", err) } passwordTriesLeft := 1 - if opts.term.InputIsTerminal() && opts.password == "" && !opts.InsecureNoPassword { + if gopts.term.InputIsTerminal() && gopts.password == "" && !gopts.InsecureNoPassword { passwordTriesLeft = 3 } for ; passwordTriesLeft > 0; passwordTriesLeft-- { - opts.password, err = ReadPassword(ctx, opts, "enter password for repository: ", printer) + gopts.password, err = ReadPassword(ctx, gopts, "enter password for repository: ", printer) if ctx.Err() != nil { return nil, ctx.Err() } if err != nil && passwordTriesLeft > 1 { - opts.password = "" + gopts.password = "" printer.E("%s. Try again", err) } if err != nil { continue } - err = s.SearchKey(ctx, opts.password, maxKeys, opts.KeyHint) + err = s.SearchKey(ctx, gopts.password, maxKeys, gopts.KeyHint) if err != nil && passwordTriesLeft > 1 { - opts.password = "" + gopts.password = "" printer.E("%s. Try again", err) } } @@ -385,15 +385,15 @@ func OpenRepository(ctx context.Context, opts GlobalOptions, printer progress.Pr } extra := "" if s.Config().Version >= 2 { - extra = ", compression level " + opts.Compression.String() + extra = ", compression level " + gopts.Compression.String() } printer.PT("repository %v opened (version %v%s)", id, s.Config().Version, extra) - if opts.NoCache { + if gopts.NoCache { return s, nil } - c, err := cache.New(s.Config().ID, opts.CacheDir) + c, err := cache.New(s.Config().ID, gopts.CacheDir) if err != nil { printer.E("unable to open cache: %v", err) return s, nil @@ -417,7 +417,7 @@ func OpenRepository(ctx context.Context, opts GlobalOptions, printer progress.Pr } // cleanup old cache dirs if instructed to do so - if opts.CleanupCache { + if gopts.CleanupCache { printer.PT("removing %d old cache dirs from %v", len(oldCacheDirs), c.Base) for _, item := range oldCacheDirs { dir := filepath.Join(c.Base, item.Name()) diff --git a/cmd/restic/global_test.go b/cmd/restic/global_test.go index 57bf19862..173a7a2a8 100644 --- a/cmd/restic/global_test.go +++ b/cmd/restic/global_test.go @@ -26,9 +26,9 @@ func TestReadRepo(t *testing.T) { tempDir := rtest.TempDir(t) // test --repo option - var opts GlobalOptions - opts.Repo = tempDir - repo, err := ReadRepo(opts) + var gopts GlobalOptions + gopts.Repo = tempDir + repo, err := ReadRepo(gopts) rtest.OK(t, err) rtest.Equals(t, tempDir, repo) @@ -37,15 +37,15 @@ func TestReadRepo(t *testing.T) { err = os.WriteFile(foo, []byte(tempDir+"\n"), 0666) rtest.OK(t, err) - var opts2 GlobalOptions - opts2.RepositoryFile = foo - repo, err = ReadRepo(opts2) + var gopts2 GlobalOptions + gopts2.RepositoryFile = foo + repo, err = ReadRepo(gopts2) rtest.OK(t, err) rtest.Equals(t, tempDir, repo) - var opts3 GlobalOptions - opts3.RepositoryFile = foo + "-invalid" - _, err = ReadRepo(opts3) + var gopts3 GlobalOptions + gopts3.RepositoryFile = foo + "-invalid" + _, err = ReadRepo(gopts3) if err == nil { t.Fatal("must not read repository path from invalid file path") }