1package main
2
3import (
4	"os"
5
6	"github.com/restic/restic/internal/errors"
7	"github.com/spf13/pflag"
8)
9
10type secondaryRepoOptions struct {
11	Repo            string
12	RepositoryFile  string
13	password        string
14	PasswordFile    string
15	PasswordCommand string
16	KeyHint         string
17}
18
19func initSecondaryRepoOptions(f *pflag.FlagSet, opts *secondaryRepoOptions, repoPrefix string, repoUsage string) {
20	f.StringVarP(&opts.Repo, "repo2", "", os.Getenv("RESTIC_REPOSITORY2"), repoPrefix+" `repository` "+repoUsage+" (default: $RESTIC_REPOSITORY2)")
21	f.StringVarP(&opts.RepositoryFile, "repository-file2", "", os.Getenv("RESTIC_REPOSITORY_FILE2"), "`file` from which to read the "+repoPrefix+" repository location "+repoUsage+" (default: $RESTIC_REPOSITORY_FILE2)")
22	f.StringVarP(&opts.PasswordFile, "password-file2", "", os.Getenv("RESTIC_PASSWORD_FILE2"), "`file` to read the "+repoPrefix+" repository password from (default: $RESTIC_PASSWORD_FILE2)")
23	f.StringVarP(&opts.KeyHint, "key-hint2", "", os.Getenv("RESTIC_KEY_HINT2"), "key ID of key to try decrypting the "+repoPrefix+" repository first (default: $RESTIC_KEY_HINT2)")
24	f.StringVarP(&opts.PasswordCommand, "password-command2", "", os.Getenv("RESTIC_PASSWORD_COMMAND2"), "shell `command` to obtain the "+repoPrefix+" repository password from (default: $RESTIC_PASSWORD_COMMAND2)")
25}
26
27func fillSecondaryGlobalOpts(opts secondaryRepoOptions, gopts GlobalOptions, repoPrefix string) (GlobalOptions, error) {
28	if opts.Repo == "" && opts.RepositoryFile == "" {
29		return GlobalOptions{}, errors.Fatal("Please specify a " + repoPrefix + " repository location (--repo2 or --repository-file2)")
30	}
31
32	if opts.Repo != "" && opts.RepositoryFile != "" {
33		return GlobalOptions{}, errors.Fatal("Options --repo2 and --repository-file2 are mutually exclusive, please specify only one")
34	}
35
36	var err error
37	dstGopts := gopts
38	dstGopts.Repo = opts.Repo
39	dstGopts.RepositoryFile = opts.RepositoryFile
40	dstGopts.PasswordFile = opts.PasswordFile
41	dstGopts.PasswordCommand = opts.PasswordCommand
42	dstGopts.KeyHint = opts.KeyHint
43	if opts.password != "" {
44		dstGopts.password = opts.password
45	} else {
46		dstGopts.password, err = resolvePassword(dstGopts, "RESTIC_PASSWORD2")
47		if err != nil {
48			return GlobalOptions{}, err
49		}
50	}
51	dstGopts.password, err = ReadPassword(dstGopts, "enter password for "+repoPrefix+" repository: ")
52	if err != nil {
53		return GlobalOptions{}, err
54	}
55	return dstGopts, nil
56}
57