-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Automatically split STDIN on null characters on push
Signed-off-by: eternal-flame-AD <[email protected]>
- Loading branch information
1 parent
1998489
commit 2264f1f
Showing
6 changed files
with
164 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package command | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/gotify/cli/v2/utils" | ||
) | ||
|
||
func readMessage(args []string, r io.Reader, output chan<- string, split *rune) { | ||
msgArgs := strings.Join(args, " ") | ||
|
||
if msgArgs != "" { | ||
if utils.ProbeStdin(r) { | ||
utils.Exit1With("message is set via arguments and stdin, use only one of them") | ||
} | ||
|
||
output <- msgArgs | ||
close(output) | ||
return | ||
} | ||
|
||
var buf strings.Builder | ||
for { | ||
var tmp [256]byte | ||
n, err := r.Read(tmp[:]) | ||
if err != nil { | ||
if err.Error() == "EOF" { | ||
break | ||
} | ||
utils.Exit1With(err) | ||
} | ||
tmpStr := string(tmp[:n]) | ||
if split != nil { | ||
// split the message on the null character | ||
parts := strings.Split(tmpStr, string(*split)) | ||
if len(parts) == 1 { | ||
buf.WriteString(parts[0]) | ||
continue | ||
} | ||
|
||
previous := buf.String() | ||
// fuse previous with parts[0], send parts[1] .. parts[n-2] and set parts[n-1] as new previous | ||
firstMsg := previous + parts[0] | ||
output <- firstMsg | ||
for _, part := range parts[1 : len(parts)-1] { | ||
output <- part | ||
} | ||
buf.Reset() | ||
buf.WriteString(parts[len(parts)-1]) | ||
} else { | ||
buf.WriteString(tmpStr) | ||
} | ||
} | ||
|
||
if buf.Len() > 0 { | ||
output <- buf.String() | ||
} | ||
|
||
close(output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package command | ||
|
||
import ( | ||
"slices" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func readChanAll[T any](c chan T) []T { | ||
var res []T | ||
for s := range c { | ||
res = append(res, s) | ||
} | ||
return res | ||
} | ||
|
||
func TestReadMessage(t *testing.T) { | ||
var split rune = '\x00' | ||
|
||
// Test case 1: message set via arguments | ||
output := make(chan string) | ||
go readMessage([]string{"Hello", "World"}, nil, output, nil) | ||
|
||
if res := readChanAll(output); !(slices.Equal(res, []string{"Hello World"})) { | ||
t.Errorf("Expected %v, but got %v", []string{"Hello World"}, res) | ||
} | ||
|
||
// Test case 2: message set via arguments should not split on 'split' character | ||
output = make(chan string) | ||
go readMessage([]string{"Hello\x00World"}, nil, output, &split) | ||
|
||
if res := readChanAll(output); !(slices.Equal(res, []string{"Hello\x00World"})) { | ||
t.Errorf("Expected %v, but got %v", []string{"Hello\x00World"}, res) | ||
} | ||
|
||
// Test case 3: message set via stdin | ||
output = make(chan string) | ||
go readMessage([]string{}, strings.NewReader("Hello\x00World"), output, &split) | ||
|
||
if res := readChanAll(output); !(slices.Equal(res, []string{"Hello", "World"})) { | ||
t.Errorf("Expected %v, but got %v", []string{"Hello", "World"}, res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
package utils | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"io/ioutil" | ||
) | ||
|
||
func ReadFrom(file *os.File) string { | ||
fi, err := os.Stdin.Stat() | ||
if err != nil { | ||
return "" | ||
func ProbeStdin(file io.Reader) bool { | ||
if file == nil { | ||
return false | ||
} | ||
if fi.Mode()&os.ModeNamedPipe == 0 && !fi.Mode().IsRegular() { | ||
return "" | ||
if file, ok := file.(*os.File); ok { | ||
fi, err := file.Stat() | ||
if err != nil { | ||
return false | ||
} | ||
if fi.Mode()&os.ModeNamedPipe == 0 && !fi.Mode().IsRegular() { | ||
return false | ||
} | ||
} | ||
|
||
bytes, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
return "" | ||
} | ||
return string(bytes) | ||
return true | ||
} |