Skip to content

Commit

Permalink
feat(lfpp): add file and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
transacid committed Dec 6, 2024
1 parent 0d0e6b1 commit 18fc774
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

lfpp
dist/
26 changes: 25 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"io/fs"
"os"
"strings"

Expand All @@ -19,7 +21,29 @@ var jFlag = flag.Bool("j", false, "output json")

func main() {
flag.Parse()
scanner := bufio.NewScanner(os.Stdin)
var input *os.File
fi, _ := os.Stdin.Stat()
if (fi.Mode() & os.ModeCharDevice) == 0 {
fmt.Println("stdin ist da")
input = os.Stdin
} else if len(flag.Args()) >= 1 {
arg := flag.Args()
f, err := os.Open(arg[0])
if errors.Is(err, fs.ErrNotExist) {
fmt.Printf("file '%s' does not exist.\n", arg[0])
os.Exit(1)
} else {
input = f
}
} else {
fmt.Println("Usage:")
fmt.Println("lfpp is a tool to pretty print logfmt from stdin or from a file")
fmt.Println("lfpp [-cj] [file]")
flag.PrintDefaults()
os.Exit(1)
}

scanner := bufio.NewScanner(input)
for scanner.Scan() {
if *jFlag {
fmt.Println(encodeJson(parseInputIntoMap(scanner.Text())))
Expand Down

0 comments on commit 18fc774

Please sign in to comment.