summaryrefslogtreecommitdiffstats
path: root/cmd/mattermost/commands/webhook.go
diff options
context:
space:
mode:
authorBen Echols <lologarithm@gmail.com>2018-10-04 12:16:25 -0600
committerJesús Espino <jespinog@gmail.com>2018-10-04 20:16:25 +0200
commit939ee15013bcb0eeed27f57cf1d7b7192d387976 (patch)
tree5a11ac24d73625f6f32c6bbe8b9208ab79cfdd2f /cmd/mattermost/commands/webhook.go
parent108fabc67de253b83060c6335cd4aa3c10a0f97c (diff)
downloadchat-939ee15013bcb0eeed27f57cf1d7b7192d387976.tar.gz
chat-939ee15013bcb0eeed27f57cf1d7b7192d387976.tar.bz2
chat-939ee15013bcb0eeed27f57cf1d7b7192d387976.zip
MM-12371 Add webhook list command (#9528)
* Added start of the webhook command * start of unit tests * created a simple hook for unit test * added outgoing as well * have it all working * Add license headers to the files * Addressing code review, fixed print reverted sql change
Diffstat (limited to 'cmd/mattermost/commands/webhook.go')
-rw-r--r--cmd/mattermost/commands/webhook.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/cmd/mattermost/commands/webhook.go b/cmd/mattermost/commands/webhook.go
new file mode 100644
index 000000000..d2a60bed8
--- /dev/null
+++ b/cmd/mattermost/commands/webhook.go
@@ -0,0 +1,84 @@
+// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package commands
+
+import (
+ "fmt"
+
+ "github.com/mattermost/mattermost-server/model"
+ "github.com/spf13/cobra"
+)
+
+var WebhookCmd = &cobra.Command{
+ Use: "webhook",
+ Short: "Management of webhooks",
+}
+
+var WebhookListCmd = &cobra.Command{
+ Use: "list",
+ Short: "List webhooks",
+ Long: "list all webhooks",
+ Example: " webhook list myteam",
+ RunE: listWebhookCmdF,
+}
+
+func listWebhookCmdF(command *cobra.Command, args []string) error {
+ app, err := InitDBCommandContextCobra(command)
+ if err != nil {
+ return err
+ }
+ defer app.Shutdown()
+
+ var teams []*model.Team
+ if len(args) < 1 {
+ var getErr error
+ // If no team is specified, list all teams
+ teams, getErr = app.GetAllTeams()
+ if getErr != nil {
+ return getErr
+ }
+ } else {
+ teams = getTeamsFromTeamArgs(app, args)
+ }
+
+ for i, team := range teams {
+ if team == nil {
+ CommandPrintErrorln("Unable to find team '" + args[i] + "'")
+ continue
+ }
+
+ // Fetch all hooks with a very large limit so we get them all.
+ incomingResult := app.Srv.Store.Webhook().GetIncomingByTeam(team.Id, 0, 100000000)
+ outgoingResult := app.Srv.Store.Webhook().GetOutgoingByTeam(team.Id, 0, 100000000)
+
+ if result := <-incomingResult; result.Err == nil {
+ CommandPrettyPrintln(fmt.Sprintf("Incoming webhooks for %s (%s):", team.DisplayName, team.Name))
+ hooks := result.Data.([]*model.IncomingWebhook)
+ for _, hook := range hooks {
+ CommandPrettyPrintln("\t" + hook.DisplayName + " (" + hook.Id + ")")
+ }
+ } else {
+ CommandPrintErrorln("Unable to list incoming webhooks for '" + args[i] + "'")
+ }
+
+ if result := <-outgoingResult; result.Err == nil {
+ hooks := result.Data.([]*model.OutgoingWebhook)
+ CommandPrettyPrintln(fmt.Sprintf("Outgoing webhooks for %s (%s):", team.DisplayName, team.Name))
+ for _, hook := range hooks {
+ CommandPrettyPrintln("\t" + hook.DisplayName + " (" + hook.Id + ")")
+ }
+ } else {
+ CommandPrintErrorln("Unable to list outgoing webhooks for '" + args[i] + "'")
+ }
+ }
+ return nil
+}
+
+func init() {
+ WebhookCmd.AddCommand(
+ WebhookListCmd,
+ )
+
+ RootCmd.AddCommand(WebhookCmd)
+}