standardize shorten variable name for GlobalOptions to gopts

This commit is contained in:
Michael Eischer
2025-09-18 22:19:38 +02:00
parent 96af35555a
commit 013c565c29
5 changed files with 47 additions and 47 deletions

View File

@@ -12,20 +12,20 @@ import (
"github.com/restic/restic/internal/ui/progress" "github.com/restic/restic/internal/ui/progress"
) )
func testRunInit(t testing.TB, opts GlobalOptions) { func testRunInit(t testing.TB, gopts GlobalOptions) {
repository.TestUseLowSecurityKDFParameters(t) repository.TestUseLowSecurityKDFParameters(t)
restic.TestDisableCheckPolynomial(t) restic.TestDisableCheckPolynomial(t)
restic.TestSetLockTimeout(t, 0) 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) return runInit(ctx, InitOptions{}, gopts, nil, gopts.term)
}) })
rtest.OK(t, err) 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 // 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")} { 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))
} }
} }

View File

@@ -10,9 +10,9 @@ import (
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
) )
func testRunList(t testing.TB, opts GlobalOptions, tpe string) restic.IDs { func testRunList(t testing.TB, gopts GlobalOptions, tpe string) restic.IDs {
buf, err := withCaptureStdout(opts, func(opts GlobalOptions) error { buf, err := withCaptureStdout(gopts, func(gopts GlobalOptions) error {
return withTermStatus(opts, func(ctx context.Context, gopts GlobalOptions) error { return withTermStatus(gopts, func(ctx context.Context, gopts GlobalOptions) error {
return runList(ctx, gopts, []string{tpe}, gopts.term) return runList(ctx, gopts, []string{tpe}, gopts.term)
}) })
}) })
@@ -38,9 +38,9 @@ func parseIDsFromReader(t testing.TB, rd io.Reader) restic.IDs {
return 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() 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) rtest.Assert(t, len(snapshotIDs) == expected, "expected %v snapshot, got %v", expected, snapshotIDs)
return snapshotIDs return snapshotIDs
} }

View File

@@ -16,8 +16,8 @@ import (
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
) )
func testRunRestore(t testing.TB, opts GlobalOptions, dir string, snapshotID string) { func testRunRestore(t testing.TB, gopts GlobalOptions, dir string, snapshotID string) {
testRunRestoreExcludes(t, opts, dir, snapshotID, nil) testRunRestoreExcludes(t, gopts, dir, snapshotID, nil)
} }
func testRunRestoreExcludes(t testing.TB, gopts GlobalOptions, dir string, snapshotID string, excludes []string) { func testRunRestoreExcludes(t testing.TB, gopts GlobalOptions, dir string, snapshotID string, excludes []string) {

View File

@@ -243,16 +243,16 @@ func readPassword(in io.Reader) (password string, err error) {
// ReadPassword reads the password from a password file, the environment // ReadPassword reads the password from a password file, the environment
// variable RESTIC_PASSWORD or prompts the user. If the context is canceled, // variable RESTIC_PASSWORD or prompts the user. If the context is canceled,
// the function leaks the password reading goroutine. // the function leaks the password reading goroutine.
func ReadPassword(ctx context.Context, opts GlobalOptions, prompt string, printer progress.Printer) (string, error) { func ReadPassword(ctx context.Context, gopts GlobalOptions, prompt string, printer progress.Printer) (string, error) {
if opts.InsecureNoPassword { if gopts.InsecureNoPassword {
if opts.password != "" { 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 "", errors.Fatal("--insecure-no-password must not be specified together with providing a password via a cli option or environment variable")
} }
return "", nil return "", nil
} }
if opts.password != "" { if gopts.password != "" {
return opts.password, nil return gopts.password, nil
} }
var ( var (
@@ -260,7 +260,7 @@ func ReadPassword(ctx context.Context, opts GlobalOptions, prompt string, printe
err error err error
) )
if opts.term.InputIsTerminal() { if gopts.term.InputIsTerminal() {
password, err = terminal.ReadPassword(ctx, os.Stdin, os.Stderr, prompt) password, err = terminal.ReadPassword(ctx, os.Stdin, os.Stderr, prompt)
} else { } else {
printer.PT("reading repository password from stdin") printer.PT("reading repository password from stdin")
@@ -300,20 +300,20 @@ func ReadPasswordTwice(ctx context.Context, gopts GlobalOptions, prompt1, prompt
return pw1, nil return pw1, nil
} }
func ReadRepo(opts GlobalOptions) (string, error) { func ReadRepo(gopts GlobalOptions) (string, error) {
if opts.Repo == "" && opts.RepositoryFile == "" { if gopts.Repo == "" && gopts.RepositoryFile == "" {
return "", errors.Fatal("Please specify repository location (-r or --repository-file)") return "", errors.Fatal("Please specify repository location (-r or --repository-file)")
} }
repo := opts.Repo repo := gopts.Repo
if opts.RepositoryFile != "" { if gopts.RepositoryFile != "" {
if repo != "" { if repo != "" {
return "", errors.Fatal("Options -r and --repository-file are mutually exclusive, please specify only one") 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) { 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 { if err != nil {
return "", err return "", err
@@ -328,47 +328,47 @@ func ReadRepo(opts GlobalOptions) (string, error) {
const maxKeys = 20 const maxKeys = 20
// OpenRepository reads the password and opens the repository. // OpenRepository reads the password and opens the repository.
func OpenRepository(ctx context.Context, opts GlobalOptions, printer progress.Printer) (*repository.Repository, error) { func OpenRepository(ctx context.Context, gopts GlobalOptions, printer progress.Printer) (*repository.Repository, error) {
repo, err := ReadRepo(opts) repo, err := ReadRepo(gopts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
be, err := open(ctx, repo, opts, opts.extended, printer) be, err := open(ctx, repo, gopts, gopts.extended, printer)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s, err := repository.New(be, repository.Options{ s, err := repository.New(be, repository.Options{
Compression: opts.Compression, Compression: gopts.Compression,
PackSize: opts.PackSize * 1024 * 1024, PackSize: gopts.PackSize * 1024 * 1024,
NoExtraVerify: opts.NoExtraVerify, NoExtraVerify: gopts.NoExtraVerify,
}) })
if err != nil { if err != nil {
return nil, errors.Fatalf("%s", err) return nil, errors.Fatalf("%s", err)
} }
passwordTriesLeft := 1 passwordTriesLeft := 1
if opts.term.InputIsTerminal() && opts.password == "" && !opts.InsecureNoPassword { if gopts.term.InputIsTerminal() && gopts.password == "" && !gopts.InsecureNoPassword {
passwordTriesLeft = 3 passwordTriesLeft = 3
} }
for ; passwordTriesLeft > 0; passwordTriesLeft-- { 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 { if ctx.Err() != nil {
return nil, ctx.Err() return nil, ctx.Err()
} }
if err != nil && passwordTriesLeft > 1 { if err != nil && passwordTriesLeft > 1 {
opts.password = "" gopts.password = ""
printer.E("%s. Try again", err) printer.E("%s. Try again", err)
} }
if err != nil { if err != nil {
continue continue
} }
err = s.SearchKey(ctx, opts.password, maxKeys, opts.KeyHint) err = s.SearchKey(ctx, gopts.password, maxKeys, gopts.KeyHint)
if err != nil && passwordTriesLeft > 1 { if err != nil && passwordTriesLeft > 1 {
opts.password = "" gopts.password = ""
printer.E("%s. Try again", err) printer.E("%s. Try again", err)
} }
} }
@@ -385,15 +385,15 @@ func OpenRepository(ctx context.Context, opts GlobalOptions, printer progress.Pr
} }
extra := "" extra := ""
if s.Config().Version >= 2 { 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) printer.PT("repository %v opened (version %v%s)", id, s.Config().Version, extra)
if opts.NoCache { if gopts.NoCache {
return s, nil return s, nil
} }
c, err := cache.New(s.Config().ID, opts.CacheDir) c, err := cache.New(s.Config().ID, gopts.CacheDir)
if err != nil { if err != nil {
printer.E("unable to open cache: %v", err) printer.E("unable to open cache: %v", err)
return s, nil 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 // 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) printer.PT("removing %d old cache dirs from %v", len(oldCacheDirs), c.Base)
for _, item := range oldCacheDirs { for _, item := range oldCacheDirs {
dir := filepath.Join(c.Base, item.Name()) dir := filepath.Join(c.Base, item.Name())

View File

@@ -26,9 +26,9 @@ func TestReadRepo(t *testing.T) {
tempDir := rtest.TempDir(t) tempDir := rtest.TempDir(t)
// test --repo option // test --repo option
var opts GlobalOptions var gopts GlobalOptions
opts.Repo = tempDir gopts.Repo = tempDir
repo, err := ReadRepo(opts) repo, err := ReadRepo(gopts)
rtest.OK(t, err) rtest.OK(t, err)
rtest.Equals(t, tempDir, repo) rtest.Equals(t, tempDir, repo)
@@ -37,15 +37,15 @@ func TestReadRepo(t *testing.T) {
err = os.WriteFile(foo, []byte(tempDir+"\n"), 0666) err = os.WriteFile(foo, []byte(tempDir+"\n"), 0666)
rtest.OK(t, err) rtest.OK(t, err)
var opts2 GlobalOptions var gopts2 GlobalOptions
opts2.RepositoryFile = foo gopts2.RepositoryFile = foo
repo, err = ReadRepo(opts2) repo, err = ReadRepo(gopts2)
rtest.OK(t, err) rtest.OK(t, err)
rtest.Equals(t, tempDir, repo) rtest.Equals(t, tempDir, repo)
var opts3 GlobalOptions var gopts3 GlobalOptions
opts3.RepositoryFile = foo + "-invalid" gopts3.RepositoryFile = foo + "-invalid"
_, err = ReadRepo(opts3) _, err = ReadRepo(gopts3)
if err == nil { if err == nil {
t.Fatal("must not read repository path from invalid file path") t.Fatal("must not read repository path from invalid file path")
} }