~yerinalexey/public-inbox

rex: Return possible error when reading from file v3 SUPERSEDED

Garrit Franke: 1
 Return possible error when reading from file

 1 files changed, 11 insertions(+), 13 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~yerinalexey/public-inbox/patches/19507/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH rex v3] Return possible error when reading from file Export this patch

---
 main.go | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/main.go b/main.go
index a7e4786..8713f14 100644
--- a/main.go
+++ b/main.go
@@ -75,7 +75,7 @@ func regexWorker(in chan string, tests string, out *tview.TextView) {
	}
}

func fromStdin() string {
func readStdin() string {
	var data string
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
@@ -86,18 +86,11 @@ func fromStdin() string {
	return data
}

func fromFile(fname string) string {
func readFile(fname string) (string, error) {
	// TODO: This will block before file loads, should be in a separate
	// goroutine
	var data string

	if raw, err := ioutil.ReadFile(fname); err == nil {
		data = string(raw)
	} else {
		fatalf("Failed to read %s: %v\n", fname, err)
	}

	return data
	raw, err := ioutil.ReadFile(fname)
	return string(raw), err
}

func main() {
@@ -126,9 +119,14 @@ func main() {

	// Determine type of input
	if file == "" {
		data = fromStdin()
		data = readStdin()
	} else {
		data = fromFile(file)
		var err error
		data, err = readFile(file)

		if err != nil {
			fatalf("Failed to read %s: %v\n", file, err)
		}
	}

	if data == "" {
-- 
2.24.3 (Apple Git-128)
This one looks good. One thing is this is not an atomic change anymore.
I'll split it out and push later.
Superseded by 4cc8c39427de25ffe3cd8c1bcc8d6e44e5c61de1