Debian Codegraph @

cmd/jl/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "github.com/mattn/go-isatty"
7 "github.com/mightyguava/jl"
8 "os"
9 )
10
11 func main() {
12 if err := run(); err != nil {
13 fmt.Fprintf(os.Stderr, "%v\n", err)
14 os.Exit(1)
15 }
16 }
17
18 func run() error {
19 flag.Usage = func() {
20 fmt.Printf(`Usage of %s:
21
22 %s [filename]
23
24 If [filename] is omitted, it reads from standard input.
25
26 `, os.Args[0], os.Args[0])
27 flag.PrintDefaults()
28 }
29 formatFlag := flag.String("format", "compact", `Formatter for logs. The options are "compact" and "logfmt"`)
30 color := flag.String("color", "auto", `Sets the color mode. The options are "auto", "yes", and "no". "auto" disables color if stdout is not a tty`)
31 truncate := flag.Bool("truncate", true, "Whether to truncate strings in the compact formatter")
32 flag.Parse()
33
34 disableColor := false
35 switch *color {
36 case "auto":
37 if !isatty.IsTerminal(os.Stdout.Fd()) {
38 disableColor = true
39 }
40 case "yes":
41 disableColor = false
42 case "no":
43 disableColor = true
44 default:
45 return fmt.Errorf("invalid -color=%s", *color)
46 }
47
48 var printer jl.EntryPrinter
49 switch *formatFlag {
50 case "logfmt":
51 lp := jl.NewLogfmtPrinter(os.Stdout)
52 lp.DisableColor = disableColor
53 printer = lp
54 case "compact":
55 cp := jl.NewCompactPrinter(os.Stdout)
56 cp.DisableColor = disableColor
57 cp.DisableTruncate = !*truncate
58 printer = cp
59 }
60
61 fileArg := flag.Arg(0)
62 inFile := os.Stdin
63 if fileArg != "" {
64 f, err := os.Open(fileArg)
65 if err != nil {
66 return err
67 }
68 defer f.Close()
69 inFile = f
70 }
71 return jl.NewParser(inFile, printer).Consume()
72 }