Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jan 31, 2024
1 parent 8db07e5 commit 70ac3e8
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions cmd/prettylog/prettylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,39 @@ func isInputFromPipe() bool {
return fileInfo.Mode()&os.ModeCharDevice == 0
}

func main() {
func processInput(reader io.Reader) error {
writer := zerolog.NewConsoleWriter()

if isInputFromPipe() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
bytesToWrite := scanner.Bytes()
_, err := writer.Write(bytesToWrite)
if err != nil {
if errors.Is(err, io.EOF) {
break
}

fmt.Printf("%s\n", bytesToWrite)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
bytesToWrite := scanner.Bytes()
_, err := writer.Write(bytesToWrite)
if err != nil {
if errors.Is(err, io.EOF) {
break
}

fmt.Printf("%s\n", bytesToWrite)
}
}

return scanner.Err()
}

func main() {

if isInputFromPipe() {
_ = processInput(os.Stdin)
} else if len(os.Args) > 1 {
for _, filename := range os.Args[1:] {
// Scan each line from filename and write it into writer
r, err := os.Open(filename)
reader, err := os.Open(filename)
if err != nil {
fmt.Printf("%s open: %v", filename, err)
os.Exit(1)
}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
_, err = writer.Write(scanner.Bytes())
if err != nil {
if errors.Is(err, io.EOF) {
break
}
fmt.Printf("%s write: %v", filename, err)
os.Exit(1)
}
}
if err := scanner.Err(); err != nil {

if err := processInput(reader); err != nil {
fmt.Printf("%s scan: %v", filename, err)
os.Exit(1)
}
Expand Down

0 comments on commit 70ac3e8

Please sign in to comment.