Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ install:
- go mod verify

script:
- go install ./...
- go test ./...

before_deploy:
- GIT_TAG=$TRAVIS_TAG make clean build
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ DESCRIPTION:
echo my text | gotify push

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

## Configuration
Expand Down
5 changes: 5 additions & 0 deletions command/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"gopkg.in/urfave/cli.v1"
)


func Push() cli.Command {
return cli.Command{
Name: "push",
Expand All @@ -30,6 +31,7 @@ func Push() cli.Command {
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.BoolFlag{Name: "disable-unescape-backslash", Usage: "Disable evaluating \\n and \\t (if set, \\n and \\t will be seen as a string)"},
},
Action: doPush,
}
Expand All @@ -39,6 +41,9 @@ func doPush(ctx *cli.Context) {
conf, confErr := config.ReadConfig(config.GetLocations())

msgText := readMessage(ctx)
if !ctx.Bool("disable-unescape-backslash") {
msgText = utils.Evaluate(msgText)
}

priority := ctx.Int("priority")
title := ctx.String("title")
Expand Down
34 changes: 34 additions & 0 deletions utils/evaluate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package utils

import (
"bytes"
"io"
)

func Evaluate(s string) string {
res := bytes.NewBuffer([]byte{})
sourceReader := bytes.NewBufferString(s)
for {
r, _, err := sourceReader.ReadRune()
if err == io.EOF {
break
}
if r == '\\' {
nextRune, _, err := sourceReader.ReadRune()
if err == nil {
switch nextRune {
case '\\':
// ignore
case 't':
r = '\t'
case 'n':
r = '\n'
default:
sourceReader.UnreadRune()
}
}
}
res.WriteRune(r)
}
return res.String()
}
29 changes: 29 additions & 0 deletions utils/evaluate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package utils

import "testing"

func TestEvaluate(t *testing.T) {
items := []struct {
str string
expected string
}{
{str: "test\\ntest", expected: "test\ntest"},
{str: "test\ntest", expected: "test\ntest"},
{str: "test\\\\ntest", expected: "test\\ntest"},
{str: "\\n", expected: "\n"},
{str: "\\\\n\\n", expected: "\\n\n"},
{str: "\\n", expected: "\n"},
{str: "\n", expected: "\n"},
{str: "\\n\\thi", expected: "\n\thi"},
{str: "\\n\t\\n\\t\n\n", expected: "\n\t\n\t\n\n"},
{str: "\\\\n\\\\thallo\\\\t\\\\n", expected: "\\n\\thallo\\t\\n"},
}
for _, item := range items {
t.Run(item.str, func(t *testing.T) {
eval := Evaluate(item.str)
if eval != item.expected {
t.Fatalf("str '%s' should be evaluated to '%s' but was '%s'.", item.str, item.expected, eval)
}
})
}
}