Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/cmd/notify.go
2498 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"strings"
10
"time"
11
12
"github.com/spf13/cobra"
13
14
"github.com/gitpod-io/gitpod/common-go/log"
15
"github.com/gitpod-io/gitpod/supervisor/api"
16
)
17
18
var notifyCmd = &cobra.Command{
19
Use: "notify <message>",
20
Short: "Notifies the user of an external event",
21
Args: cobra.ExactArgs(1),
22
Hidden: true, // this is not official user-facing functionality, but just for debugging
23
Run: func(cmd *cobra.Command, args []string) {
24
client := api.NewNotificationServiceClient(dialSupervisor())
25
26
level := api.NotifyRequest_INFO
27
lv, _ := cmd.Flags().GetString("level")
28
if l, ok := api.NotifyRequest_Level_value[strings.ToUpper(lv)]; ok {
29
level = api.NotifyRequest_Level(l)
30
}
31
32
var (
33
message = args[0]
34
actions, _ = cmd.Flags().GetStringArray("actions")
35
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Minute)
36
)
37
defer cancel()
38
39
response, err := client.Notify(ctx, &api.NotifyRequest{
40
Level: level,
41
Message: message,
42
Actions: actions,
43
})
44
if err != nil {
45
log.WithError(err).Fatal("cannot notify client")
46
}
47
log.WithField("action", response.Action).Info("User answered")
48
},
49
}
50
51
func init() {
52
rootCmd.AddCommand(notifyCmd)
53
54
var levels []string
55
for k := range api.NotifyRequest_Level_value {
56
levels = append(levels, strings.ToLower(k))
57
}
58
notifyCmd.Flags().String("level", "info", "notification severity - must be one of "+strings.Join(levels, ", "))
59
notifyCmd.Flags().StringArray("actions", nil, "actions to offer to the user")
60
}
61
62