This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
commands.go
134 lines (111 loc) · 3.77 KB
/
commands.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package marvin
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/riking/marvin/util"
)
// CommandArguments contains the post-split arguments arrays. The pre-split
// string is not available, as the arguments can sometimes come in as an array.
type CommandArguments struct {
Source ActionSource
Command string
Arguments []string
OriginalArguments []string
Ctx context.Context
IsEdit bool
IsUndo bool
PreviousResult *CommandResult
ModuleData interface{}
}
// Pop moves the first element of Arguments to Command and returns the new
// value of Command.
func (args *CommandArguments) Pop() string {
str := args.Arguments[0]
args.Arguments = args.Arguments[1:]
return str
}
// PreArgs returns the slice of all arguments that have been Pop()ped.
func (args *CommandArguments) PreArgs() []string {
return args.OriginalArguments[:len(args.OriginalArguments)-len(args.Arguments)]
}
// SetModuleData is useful for editing.
func (args *CommandArguments) SetModuleData(v interface{}) {
args.ModuleData = v
}
type CommandResultCode int
const (
CmdResultOK CommandResultCode = iota
CmdResultFailure
CmdResultError
CmdResultNoSuchCommand
CmdResultPrintUsage
CmdResultPrintHelp
)
const UndoSimple = 2
const UndoCustom = util.TriYes
// A CommandResult is the return of a command. Use the Cmd* constructors to make them.
// If you want to specify where the reply will go, call WithReplyType().
//
// CommandResult objects are to be passed and modified by-value.
//
// A panicking command is converted into an Err-containing CommandResult.
type CommandResult struct {
Args *CommandArguments
Message string
Err error
Code CommandResultCode
ReplyType ReplyType
Sent bool
CanEdit util.TriValue
CanUndo util.TriValue
}
// CmdError includes the Err field for the CmdResultError code.
// An error is something that shouldn't normally happen - access violations go under Failure.
func CmdError(args *CommandArguments, err error, msg string) CommandResult {
return CommandResult{Args: args, Message: msg, Err: errors.Wrap(err, msg), Code: CmdResultError}
}
// CmdFailuref formats a string to create a CmdResultFailure result.
func CmdFailuref(args *CommandArguments, format string, v ...interface{}) CommandResult {
return CommandResult{Args: args, Message: fmt.Sprintf(format, v...), Code: CmdResultFailure}
}
// CmdHelpf formats a string to create a CmdResultPrintHelp result.
func CmdHelpf(args *CommandArguments, format string, v ...interface{}) CommandResult {
return CommandResult{Args: args, Message: fmt.Sprintf(format, v...), Code: CmdResultPrintHelp}
}
// CmdSuccess simply takes a message to give to the user for an OK result.
func CmdSuccess(args *CommandArguments, msg string) CommandResult {
return CommandResult{Args: args, Message: msg, Code: CmdResultOK}
}
// CmdUsage takes the usage string for a CmdResultPrintUsage result.
func CmdUsage(args *CommandArguments, usage string) CommandResult {
return CommandResult{Args: args, Message: usage, Code: CmdResultPrintUsage}
}
func (r CommandResult) WithEdit() CommandResult {
r.CanEdit = util.TriYes
return r
}
func (r CommandResult) WithNoEdit() CommandResult {
r.CanEdit = util.TriNo
return r
}
func (r CommandResult) WithCustomUndo() CommandResult {
r.CanUndo = UndoCustom
return r
}
func (r CommandResult) WithSimpleUndo() CommandResult {
r.CanUndo = UndoSimple
return r
}
func (r CommandResult) WithNoUndo() CommandResult {
r.CanUndo = util.TriNo
return r
}
// WithReplyType explicitly sets where the response should be directed.
//
// The caller can override this if desired, but it will be respected for all
// commands initiated directly by users.
func (r CommandResult) WithReplyType(rt ReplyType) CommandResult {
r.ReplyType = rt
return r
}