aboutsummaryrefslogtreecommitdiffstats
path: root/commands/patch/patch.go
blob: 0807c1fa399626e2382867f7a1c204082eda1fa4 (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
62
63
64
65
66
67
68
69
70
71
package patch

import (
	"errors"
	"fmt"

	"git.sr.ht/~rjarry/aerc/commands"
	"git.sr.ht/~rjarry/go-opt"
)

var (
	PatchCommands *commands.Commands
	subCommands   *commands.Commands
)

func register(cmd commands.Command) {
	if subCommands == nil {
		subCommands = commands.NewCommands()
	}
	subCommands.Register(cmd)
}

func registerPatch(cmd commands.Command) {
	if PatchCommands == nil {
		PatchCommands = commands.NewCommands()
	}
	PatchCommands.Register(cmd)
}

func init() {
	registerPatch(Patch{})
}

type Patch struct {
	SubCmd commands.Command `opt:"command" action:"ParseSub" complete:"CompleteSubNames"`
	Args   string           `opt:"..." required:"false" complete:"CompleteSubArgs"`
}

func (Patch) Aliases() []string {
	return []string{"patch"}
}

func (p *Patch) ParseSub(arg string) error {
	p.SubCmd = subCommands.ByName(arg)
	if p.SubCmd == nil {
		return fmt.Errorf("%s unknown sub-command", arg)
	}
	return nil
}

func (*Patch) CompleteSubNames(arg string) []string {
	options := subCommands.Names()
	return commands.FilterList(options, arg, nil)
}

func (p *Patch) CompleteSubArgs(arg string) []string {
	if p.SubCmd == nil {
		return nil
	}
	// prepend arbitrary string to arg to work with sub-commands
	options, _ := commands.GetCompletions(p.SubCmd, opt.LexArgs("a "+arg))
	return options
}

func (p Patch) Execute(args []string) error {
	if p.SubCmd == nil {
		return errors.New("no subcommand found")
	}
	a := opt.QuoteArgs(args[1:]...)
	return commands.ExecuteCommand(p.SubCmd, a.String())
}