generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcommand.go
121 lines (105 loc) · 3.08 KB
/
command.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
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
// See License for license information.
package command
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils"
)
// Handler handles commands
type Command struct {
Context *plugin.Context
Args *model.CommandArgs
ChannelID string
Config *config.Config
MSCalendar mscalendar.MSCalendar
}
func getHelp() string {
help := `
TODO: help text.
`
return utils.CodeBlock(fmt.Sprintf(
help,
))
}
// RegisterFunc is a function that allows the runner to register commands with the mattermost server.
type RegisterFunc func(*model.Command) error
// Register should be called by the plugin to register all necessary commands
func Register(registerFunc RegisterFunc) {
_ = registerFunc(&model.Command{
Trigger: config.CommandTrigger,
DisplayName: "Microsoft Calendar",
Description: "Interact with your outlook calendar.",
AutoComplete: true,
AutoCompleteDesc: "info, connect, viewcal, createcal, testcreateevent, deletecal, subscribe, findMeetings, showcals, availability",
AutoCompleteHint: "(subcommand)",
})
}
// Handle should be called by the plugin when a command invocation is received from the Mattermost server.
func (c *Command) Handle() (string, error) {
cmd, parameters, err := c.isValid()
if err != nil {
return "", err
}
handler := c.help
switch cmd {
case "info":
handler = c.info
case "connect":
handler = c.connect
case "connect_bot":
handler = c.connectBot
case "disconnect":
handler = c.disconnect
case "disconnect_bot":
handler = c.disconnectBot
case "viewcal":
handler = c.viewCalendar
case "createcal":
handler = c.createCalendar
case "createevent":
handler = c.createEvent
case "deletecal":
handler = c.deleteCalendar
case "subscribe":
handler = c.subscribe
case "findmeetings":
handler = c.findMeetings
case "showcals":
handler = c.showCalendars
case "availability":
handler = c.availability
}
out, err := handler(parameters...)
if err != nil {
return "", errors.WithMessagef(err, "Command /%s %s failed", config.CommandTrigger, cmd)
}
return out, nil
}
func (c *Command) isValid() (subcommand string, parameters []string, err error) {
if c.Context == nil || c.Args == nil {
return "", nil, errors.New("Invalid arguments to command.Handler")
}
split := strings.Fields(c.Args.Command)
command := split[0]
if command != "/"+config.CommandTrigger {
return "", nil, errors.Errorf("%q is not a supported command. Please contact your system administrator.", command)
}
parameters = []string{}
subcommand = ""
if len(split) > 1 {
subcommand = split[1]
}
if len(split) > 2 {
parameters = split[2:]
}
return subcommand, parameters, nil
}
func (c *Command) user() *mscalendar.User {
return mscalendar.NewUser(c.Args.UserId)
}