Skip to content

Commit 6494691

Browse files
committed
Evaluate backslash n in message content
1 parent a26c35e commit 6494691

4 files changed

Lines changed: 75 additions & 6 deletions

File tree

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,13 @@ DESCRIPTION:
105105
echo my text | gotify push
106106

107107
OPTIONS:
108-
--priority value, -p value Set the priority (default: 0)
109-
--title value, -t value Set the title (empty for app name)
110-
--token value Override the app token
111-
--url value Override the Gotify URL
112-
--quiet, -q Do not output anything (on success)
113-
--contentType value The content type of the message. See https://gotify.net/docs/msgextras#client-display
108+
--priority value, -p value Set the priority (default: 0)
109+
--title value, -t value Set the title (empty for app name)
110+
--token value Override the app token
111+
--url value Override the Gotify URL
112+
--quiet, -q Do not output anything (on success)
113+
--contentType value The content type of the message. See https://gotify.net/docs/msgextras#client-display
114+
--disable-unescape-backslash Disable evaluating \n and \t (if set, \n and \t will be seen as a string)
114115
```
115116

116117
## Configuration

command/push.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"gopkg.in/urfave/cli.v1"
1717
)
1818

19+
1920
func Push() cli.Command {
2021
return cli.Command{
2122
Name: "push",
@@ -30,6 +31,7 @@ func Push() cli.Command {
3031
cli.StringFlag{Name: "url", Usage: "Override the Gotify URL"},
3132
cli.BoolFlag{Name: "quiet,q", Usage: "Do not output anything (on success)"},
3233
cli.StringFlag{Name: "contentType", Usage: "The content type of the message. See https://gotify.net/docs/msgextras#client-display"},
34+
cli.BoolFlag{Name: "disable-unescape-backslash", Usage: "Disable evaluating \\n and \\t (if set, \\n and \\t will be seen as a string)"},
3335
},
3436
Action: doPush,
3537
}
@@ -39,6 +41,9 @@ func doPush(ctx *cli.Context) {
3941
conf, confErr := config.ReadConfig(config.GetLocations())
4042

4143
msgText := readMessage(ctx)
44+
if !ctx.Bool("disable-unescape-backslash") {
45+
msgText = utils.Evaluate(msgText)
46+
}
4247

4348
priority := ctx.Int("priority")
4449
title := ctx.String("title")

utils/evaluate.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package utils
2+
3+
import (
4+
"bytes"
5+
"io"
6+
)
7+
8+
func Evaluate(s string) string {
9+
res := bytes.NewBuffer([]byte{})
10+
sourceReader := bytes.NewBufferString(s)
11+
for {
12+
r, _, err := sourceReader.ReadRune()
13+
if err == io.EOF {
14+
break
15+
}
16+
if r == '\\' {
17+
nextRune, _, err := sourceReader.ReadRune()
18+
if err == nil {
19+
switch nextRune {
20+
case '\\':
21+
// ignore
22+
case 't':
23+
r = '\t'
24+
case 'n':
25+
r = '\n'
26+
default:
27+
sourceReader.UnreadRune()
28+
}
29+
}
30+
}
31+
res.WriteRune(r)
32+
}
33+
return res.String()
34+
}

utils/evaluate_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package utils
2+
3+
import "testing"
4+
5+
func TestEvaluate(t *testing.T) {
6+
items := []struct {
7+
str string
8+
expected string
9+
}{
10+
{str: "test\\ntest", expected: "test\ntest"},
11+
{str: "test\ntest", expected: "test\ntest"},
12+
{str: "test\\\\ntest", expected: "test\\ntest"},
13+
{str: "\\n", expected: "\n"},
14+
{str: "\\\\n\\n", expected: "\\n\n"},
15+
{str: "\\n", expected: "\n"},
16+
{str: "\n", expected: "\n"},
17+
{str: "\\n\\thi", expected: "\n\thi"},
18+
{str: "\\n\t\\n\\t\n\n", expected: "\n\t\n\t\n\n"},
19+
{str: "\\\\n\\\\thallo\\\\t\\\\n", expected: "\\n\\thallo\\t\\n"},
20+
}
21+
for _, item := range items {
22+
t.Run(item.str, func(t *testing.T) {
23+
eval := Evaluate(item.str)
24+
if eval != item.expected {
25+
t.Fatalf("str '%s' should be evaluated to '%s' but was '%s'.", item.str, item.expected, eval)
26+
}
27+
})
28+
}
29+
}

0 commit comments

Comments
 (0)