summaryrefslogtreecommitdiff
path: root/commands/quit.go
blob: ee5f46c1290863e3c85d0654f1ee369f9a0e77b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package commands

import (
	"errors"
	"fmt"

	"git.sr.ht/~rjarry/aerc/commands/mode"
	"git.sr.ht/~rjarry/aerc/widgets"
	"git.sr.ht/~sircmpwn/getopt"
)

type Quit struct{}

func init() {
	register(Quit{})
}

func (Quit) Aliases() []string {
	return []string{"quit", "exit"}
}

func (Quit) Complete(aerc *widgets.Aerc, args []string) []string {
	return nil
}

type ErrorExit int

func (err ErrorExit) Error() string {
	return "exit"
}

func (Quit) Execute(aerc *widgets.Aerc, args []string) error {
	force := false
	opts, optind, err := getopt.Getopts(args, "f")
	if err != nil {
		return err
	}
	for _, opt := range opts {
		switch opt.Option {
		case 'f':
			force = true
		}
	}
	if len(args) != optind {
		return errors.New("Usage: quit [-f]")
	}
	if force || mode.QuitAllowed() {
		return ErrorExit(1)
	}
	return fmt.Errorf("A task is not done yet. Use -f to force an exit.")
}