key add/passwd: deduplicate options setup and remove globals

The current pattern of using a global options variable is problematic.
This commit is contained in:
Michael Eischer 2024-05-18 18:25:09 +02:00
parent 55cb8d174a
commit d4b0d21199
2 changed files with 19 additions and 20 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
) )
var cmdKeyAdd = &cobra.Command{ var cmdKeyAdd = &cobra.Command{
@ -23,26 +24,28 @@ EXIT STATUS
Exit status is 0 if the command is successful, and non-zero if there was any error. Exit status is 0 if the command is successful, and non-zero if there was any error.
`, `,
DisableAutoGenTag: true, DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runKeyAdd(cmd.Context(), globalOptions, keyAddOpts, args)
},
} }
type KeyAddOptions struct { type KeyAddOptions struct {
NewPasswordFile string NewPasswordFile string
Username string Username string
Hostname string Hostname string
} }
var keyAddOpts KeyAddOptions func (opts *KeyAddOptions) Add(flags *pflag.FlagSet) {
flags.StringVarP(&opts.NewPasswordFile, "new-password-file", "", "", "`file` from which to read the new password")
flags.StringVarP(&opts.Username, "user", "", "", "the username for new key")
flags.StringVarP(&opts.Hostname, "host", "", "", "the hostname for new key")
}
func init() { func init() {
cmdKey.AddCommand(cmdKeyAdd) cmdKey.AddCommand(cmdKeyAdd)
flags := cmdKeyAdd.Flags() var keyAddOpts KeyAddOptions
flags.StringVarP(&keyAddOpts.NewPasswordFile, "new-password-file", "", "", "`file` from which to read the new password") keyAddOpts.Add(cmdKeyAdd.Flags())
flags.StringVarP(&keyAddOpts.Username, "user", "", "", "the username for new key") cmdKeyAdd.RunE = func(cmd *cobra.Command, args []string) error {
flags.StringVarP(&keyAddOpts.Hostname, "host", "", "", "the hostname for new key") return runKeyAdd(cmd.Context(), globalOptions, keyAddOpts, args)
}
} }
func runKeyAdd(ctx context.Context, gopts GlobalOptions, opts KeyAddOptions, args []string) error { func runKeyAdd(ctx context.Context, gopts GlobalOptions, opts KeyAddOptions, args []string) error {

View file

@ -22,24 +22,20 @@ EXIT STATUS
Exit status is 0 if the command is successful, and non-zero if there was any error. Exit status is 0 if the command is successful, and non-zero if there was any error.
`, `,
DisableAutoGenTag: true, DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runKeyPasswd(cmd.Context(), globalOptions, keyPasswdOpts, args)
},
} }
type KeyPasswdOptions struct { type KeyPasswdOptions struct {
KeyAddOptions KeyAddOptions
} }
var keyPasswdOpts KeyPasswdOptions
func init() { func init() {
cmdKey.AddCommand(cmdKeyPasswd) cmdKey.AddCommand(cmdKeyPasswd)
flags := cmdKeyPasswd.Flags() var keyPasswdOpts KeyPasswdOptions
flags.StringVarP(&keyPasswdOpts.NewPasswordFile, "new-password-file", "", "", "`file` from which to read the new password") keyPasswdOpts.KeyAddOptions.Add(cmdKeyPasswd.Flags())
flags.StringVarP(&keyPasswdOpts.Username, "user", "", "", "the username for new key") cmdKeyPasswd.RunE = func(cmd *cobra.Command, args []string) error {
flags.StringVarP(&keyPasswdOpts.Hostname, "host", "", "", "the hostname for new key") return runKeyPasswd(cmd.Context(), globalOptions, keyPasswdOpts, args)
}
} }
func runKeyPasswd(ctx context.Context, gopts GlobalOptions, opts KeyPasswdOptions, args []string) error { func runKeyPasswd(ctx context.Context, gopts GlobalOptions, opts KeyPasswdOptions, args []string) error {