Skip to content

Commit

Permalink
D.R.Y. up the input source fetching.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Apr 10, 2024
1 parent 2ab50b8 commit 8a154f0
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions cmd/bfg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"errors"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -29,28 +30,15 @@ func main() {
var sourceBuf, inputBuf io.ByteReader
source := flag.Arg(0)
input := flag.Arg(1)
if source == "" {
fmt.Println("please provide a source file")
sourceBuf, err := inputReader(source, false)
if err != nil {
fmt.Println("error opening program: err:", err)
os.Exit(1)
} else if source == "-" {
sourceBuf = bufio.NewReader(os.Stdin)
} else {
sourceFile, err := os.Open(source)
if err != nil {
fmt.Println("error opening source file: err:", err)
os.Exit(1)
}
sourceBuf = bufio.NewReader(sourceFile)
}
if input == "" {
inputBuf = bufio.NewReader(os.Stdin)
} else {
file, err := os.Open(input)
if err != nil {
fmt.Println("error opening input file: err:", err)
os.Exit(1)
}
inputBuf = bufio.NewReader(file)
inputBuf, err = inputReader(input, true)
if err != nil {
fmt.Println("error opening input: err:", err)
os.Exit(1)
}
outputBuf := bufio.NewWriter(os.Stdout)
defer outputBuf.Flush()
Expand All @@ -66,3 +54,21 @@ func main() {
parser.Execute(program, inputBuf, outputBuf)
}
}

func inputReader(pathStr string, useDefault bool) (buff io.ByteReader, err error) {
if useDefault && pathStr == "" {
pathStr = "-"
}
if pathStr == "" {
return nil, errors.New("no path provided")
} else if pathStr == "-" {
buff = bufio.NewReader(os.Stdin)
} else {
sourceFile, err := os.Open(pathStr)
if err != nil {
return nil, err
}
buff = bufio.NewReader(sourceFile)
}
return
}

0 comments on commit 8a154f0

Please sign in to comment.