-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcli.go
73 lines (60 loc) · 2.34 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"os"
"strconv"
"strings"
"github.com/docopt/docopt-go"
"github.com/mattn/go-isatty"
)
var usage = `jl - JSON Logs
'jl' is a development tool for working with structured JSON logging
It will parse loglines from stdin and try to parse them as
structured logging entries. When such a message is detected it
will output the entry in a human readable way. Anything else
is forwarded as is.
Usage:
jl [options] [FILE...]
Options:
-h, --help Show this screen.
--version Show version.
Output Options:
--color Force colorized output
--no-color Don't colorize output
--skip-prefix Skip printing truncated bytes before the JSON
--skip-suffix Skip printing truncated bytes after the JSON
Formatting Options:
--skip-fields Don't output misc json keys as fields
--max-field-length <int>
Any field, exceeding the given length (including
field name) will be ommitted from output. Use 0
to remove the length limit [default: 30]
--include-fields <fields>, -f <fields>
Always include these json keys as fields, no matter
the length (comma separated list)
--exclude-fields <fields>
Always exclude these json keys (comma separated
list)
You can add any option to the JL_OPTS environment variable, ex:
export JL_OPTS="--no-color"
Example:
$ echo '{"level": "info", "msg": "Hello!", "size": 42}' | jl
INFO: Hello! [size=42]
`
var version = "v1.6.0"
func cli() (files []string, color, showPrefix, showSuffix, showFields bool, includeFields string, excludeFields string, maxFieldLength int) {
argv := append(os.Args[1:], strings.Split(os.Getenv("JL_OPTS"), " ")...)
arguments, err := docopt.ParseArgs(usage, argv, "jl "+version)
if err != nil {
panic(err)
}
isTTY := isatty.IsTerminal(os.Stdout.Fd())
color = !arguments["--no-color"].(bool) && (arguments["--color"].(bool) || isTTY)
showPrefix = !arguments["--skip-prefix"].(bool)
showSuffix = !arguments["--skip-suffix"].(bool)
showFields = !arguments["--skip-fields"].(bool)
maxFieldLength, _ = strconv.Atoi(arguments["--max-field-length"].(string))
includeFields, _ = arguments["--include-fields"].(string)
excludeFields, _ = arguments["--exclude-fields"].(string)
files = arguments["FILE"].([]string)
return
}