aboutsummaryrefslogtreecommitdiffstats
path: root/commands/account/recover.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-10-03 22:12:10 +0200
committerRobin Jarry <robin@jarry.cc>2023-10-28 19:24:49 +0200
commite54486ee40c9cedde9d4dae3e89d8b5f932188ee (patch)
tree254f032ef94920c28fbb9be8f918e61082a2015d /commands/account/recover.go
parent9a4518476d8c8f28340c6b44cd808e6d58fbeb98 (diff)
downloadaerc-e54486ee40c9cedde9d4dae3e89d8b5f932188ee.tar.gz
commands: parse arguments with go-opt
Use the argument parsing framework introduced earlier to unify the parsing of (almost) all command options. Remove custom parsing code and to avoid extraneous types, add fields with `opt` tags on command structs that have options and arguments. Commands that take no argument do not need anything. Since the command objects now carry data, create a new temporary instance of them before passing them to opt.ArgsToStruct when executing a command. A few of the commands use specific semantics for parsing (:choose), or are delegating argument parsing to another function (:sort, :search, :filter). For these commands, simply add a dummy "-" passthrough argument. Since all commands still have the argument list (after split) nothing needs to be changed in this area. There should be no functional change besides the Usage strings and reported errors which are now generated automatically. Signed-off-by: Robin Jarry <robin@jarry.cc> Reviewed-by: Koni Marti <koni.marti@gmail.com> Tested-by: Moritz Poldrack <moritz@poldrack.dev> Tested-by: Inwit <inwit@sindominio.net>
Diffstat (limited to 'commands/account/recover.go')
-rw-r--r--commands/account/recover.go56
1 files changed, 14 insertions, 42 deletions
diff --git a/commands/account/recover.go b/commands/account/recover.go
index e0d1c6eb..dae3d807 100644
--- a/commands/account/recover.go
+++ b/commands/account/recover.go
@@ -10,10 +10,14 @@ import (
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/config"
- "git.sr.ht/~sircmpwn/getopt"
)
-type Recover struct{}
+type Recover struct {
+ Force bool `opt:"-f"`
+ Edit bool `opt:"-e"`
+ NoEdit bool `opt:"-E"`
+ File string `opt:"file"`
+}
func init() {
register(Recover{})
@@ -40,31 +44,14 @@ func (r Recover) Complete(args []string) []string {
}
func (r Recover) Execute(args []string) error {
- // Complete() expects to be passed only the arguments, not including the command name
- if len(Recover{}.Complete(args[1:])) == 0 {
- return errors.New("No messages to recover.")
- }
-
- force := false
- editHeaders := config.Compose.EditHeaders
-
- opts, optind, err := getopt.Getopts(args, r.Options())
+ file, err := os.Open(r.File)
if err != nil {
return err
}
- for _, opt := range opts {
- switch opt.Option {
- case 'f':
- force = true
- case 'e':
- editHeaders = true
- case 'E':
- editHeaders = false
- }
- }
-
- if len(args) <= optind {
- return errors.New("Usage: recover [-f] [-E|-e] <file>")
+ defer file.Close()
+ data, err := io.ReadAll(file)
+ if err != nil {
+ return err
}
acct := app.SelectedAccount()
@@ -72,22 +59,7 @@ func (r Recover) Execute(args []string) error {
return errors.New("No account selected")
}
- readData := func() ([]byte, error) {
- recoverFile, err := os.Open(args[optind])
- if err != nil {
- return nil, err
- }
- defer recoverFile.Close()
- data, err := io.ReadAll(recoverFile)
- if err != nil {
- return nil, err
- }
- return data, nil
- }
- data, err := readData()
- if err != nil {
- return err
- }
+ editHeaders := (config.Compose.EditHeaders || r.Edit) && !r.NoEdit
composer, err := app.NewComposer(acct,
acct.AccountConfig(), acct.Worker(), editHeaders,
@@ -98,8 +70,8 @@ func (r Recover) Execute(args []string) error {
composer.Tab = app.NewTab(composer, "Recovered")
// remove file if force flag is set
- if force {
- err = os.Remove(args[optind])
+ if r.Force {
+ err = os.Remove(r.File)
if err != nil {
return err
}