-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathpush.go
More file actions
135 lines (115 loc) · 3.63 KB
/
push.go
File metadata and controls
135 lines (115 loc) · 3.63 KB
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
135
package command
import (
"fmt"
"net/url"
"os"
"github.com/gotify/cli/v2/config"
"github.com/gotify/cli/v2/utils"
"github.com/gotify/go-api-client/v2/auth"
"github.com/gotify/go-api-client/v2/client/message"
"github.com/gotify/go-api-client/v2/gotify"
"github.com/gotify/go-api-client/v2/models"
"gopkg.in/urfave/cli.v1"
)
func Push() cli.Command {
return cli.Command{
Name: "push",
Aliases: []string{"p"},
Usage: "Pushes a message",
ArgsUsage: "<message-text>",
Description: "the message can also provided in stdin f.ex:\n echo my text | gotify push",
Flags: []cli.Flag{
cli.IntFlag{Name: "priority,p", Usage: "Set the priority"},
cli.StringFlag{Name: "title,t", Usage: "Set the title (empty for app name)"},
cli.StringFlag{Name: "token", Usage: "Override the app token", EnvVar: "GOTIFY_TOKEN"},
cli.StringFlag{Name: "url", Usage: "Override the Gotify URL"},
cli.BoolFlag{Name: "quiet,q", Usage: "Do not output anything (on success)"},
cli.StringFlag{Name: "contentType", Usage: "The content type of the message. See https://gotify.net/docs/msgextras#client-display"},
cli.StringFlag{Name: "clickUrl", Usage: "An URL to open upon clicking the notification. See https://gotify.net/docs/msgextras#client-notification"},
cli.BoolFlag{Name: "disable-unescape-backslash", Usage: "Disable evaluating \\n and \\t (if set, \\n and \\t will be seen as a string)"},
cli.BoolFlag{Name: "no-split", Usage: "Do not split the message on null character when reading from stdin"},
},
Action: doPush,
}
}
func doPush(ctx *cli.Context) {
conf, confErr := config.ReadConfig(config.GetLocations())
msgText := make(chan string)
null := '\x00'
if ctx.Bool("no-split") {
go readMessage(ctx.Args(), os.Stdin, msgText, nil)
} else {
go readMessage(ctx.Args(), os.Stdin, msgText, &null)
}
priority := ctx.Int("priority")
title := ctx.String("title")
token := ctx.String("token")
quiet := ctx.Bool("quiet")
contentType := ctx.String("contentType")
clickUrl := ctx.String("clickUrl")
if token == "" {
if confErr != nil {
utils.Exit1With("token is not configured, run 'gotify init'")
return
}
token = conf.Token
}
stringURL := ctx.String("url")
if stringURL == "" {
if confErr != nil {
utils.Exit1With("url is not configured, run 'gotify init'")
return
}
stringURL = conf.URL
}
if !ctx.IsSet("priority") && conf != nil {
priority = conf.DefaultPriority
}
parsedURL, err := url.Parse(stringURL)
if err != nil {
utils.Exit1With("invalid url", stringURL)
return
}
var sent bool
for msgText := range msgText {
if !ctx.Bool("disable-unescape-backslash") {
msgText = utils.Evaluate(msgText)
}
msg := models.MessageExternal{
Message: msgText,
Title: title,
Priority: priority,
}
msg.Extras = map[string]interface{}{}
if contentType != "" {
msg.Extras["client::display"] = map[string]interface{}{
"contentType": contentType,
}
}
if clickUrl != "" {
msg.Extras["client::notification"] = map[string]interface{}{
"click": map[string]string{
"url": clickUrl,
},
}
}
pushMessage(parsedURL, token, msg, quiet)
sent = true
}
if !sent {
utils.Exit1With("no message sent! a message must be set, either as argument or via stdin")
}
}
func pushMessage(parsedURL *url.URL, token string, msg models.MessageExternal, quiet bool) {
client := gotify.NewClient(parsedURL, utils.CreateHTTPClient())
params := message.NewCreateMessageParams()
params.Body = &msg
_, err := client.Message.CreateMessage(params, auth.TokenAuth(token))
if err == nil {
if !quiet {
fmt.Println("message created")
}
} else {
utils.Exit1With(err)
}
}