aboutsummaryrefslogtreecommitdiffstats
path: root/commands/compose/switch.go
blob: 0c027a41edc3ca81fffecd0efa491d7c7cce9492 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
package compose

import (
	"errors"

	"git.sr.ht/~rjarry/aerc/app"
)

type AccountSwitcher interface {
	SwitchAccount(*app.AccountView) error
}

type SwitchAccount struct {
	Next    bool   `opt:"-n"`
	Prev    bool   `opt:"-p"`
	Account string `opt:"..." metavar:"<account>" required:"false"`
}

func init() {
	register(SwitchAccount{})
}

func (SwitchAccount) Aliases() []string {
	return []string{"switch-account"}
}

func (SwitchAccount) Complete(args []string) []string {
	return app.AccountNames()
}

func (s SwitchAccount) Execute(args []string) error {
	if !s.Prev && !s.Next && s.Account == "" {
		return errors.New("Usage: switch-account -n | -p | <account-name>")
	}

	switcher, ok := app.SelectedTabContent().(AccountSwitcher)
	if !ok {
		return errors.New("this tab cannot switch accounts")
	}

	var acct *app.AccountView
	var err error

	switch {
	case s.Prev:
		acct, err = app.PrevAccount()
	case s.Next:
		acct, err = app.NextAccount()
	default:
		acct, err = app.Account(s.Account)
	}
	if err != nil {
		return err
	}
	if err = switcher.SwitchAccount(acct); err != nil {
		return err
	}
	acct.UpdateStatus()

	return nil
}