Skip to content

Commit 7c585b2

Browse files
authored
feat: support columns customization config (#32)
1 parent 4bce99b commit 7c585b2

File tree

22 files changed

+817
-139
lines changed

22 files changed

+817
-139
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Set up Go
1515
uses: actions/setup-go@v4
1616
with:
17-
go-version: '1.20.6'
17+
go-version: '1.21.3'
1818
id: go
1919

2020
- name: Check out code into the Go module directory

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Set up Go
1616
uses: actions/setup-go@v4
1717
with:
18-
go-version: '1.20.4'
18+
go-version: '1.21.3'
1919

2020
- name: Fetch vendor
2121
run: make vendor

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ bin
1717

1818
# Dependency directories (remove the comment below to include it)
1919
vendor/
20+
21+
# Config
22+
.jlv.jsonc

.golangci.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
"rowserrcheck",
3434
"depguard",
3535
"ireturn",
36-
"gomoddirectives"
36+
"gomoddirectives",
37+
"tagalign",
38+
"testifylint"
3739
]
3840
},
3941
"linters-settings": {

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GOLANG_CI_LINT_VER:=v1.53.3
1+
GOLANG_CI_LINT_VER:=v1.55.2
22
OUT_BIN?=${PWD}/bin/jlv
33
COVER_PACKAGES=./...
44
VERSION?=${shell git describe --tags}

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The application is designed to help in visualization, navigation, and analyzing
3333
- [Package](#package)
3434
- [Standalone Binary](#standalone-binary)
3535
- [Source](#source)
36+
- [Customization](#customization)
3637
- [Resources](#resources)
3738
- [Contribution](#contribution)
3839
- [License](#license)
@@ -89,6 +90,69 @@ chmod +x /usr/local/bin/jlv
8990
# jlv application.log
9091
```
9192

93+
## Customization
94+
95+
The application will look for the config `.jlv.jsonc` in the working directory or in the home directory:
96+
- `$PWD/.jlv.jsonc`;
97+
- `$HOME/.jlv.jsonc`.
98+
99+
The Json path supports the described in [yalp/jsonpath](https://github.com/yalp/jsonpath#jsonpath-quick-intro) syntax.
100+
101+
Example configuration:
102+
```json
103+
{
104+
// Comments are allowed.
105+
"fields": [
106+
{
107+
"title": "Time", // Max length is 32.
108+
// Kind affects rendering. There are:
109+
// * time;
110+
// * level;
111+
// * message;
112+
// * any.
113+
"kind": "time",
114+
"ref": [
115+
// The application will display the first matched value.
116+
"$.timestamp",
117+
"$.time",
118+
"$.t",
119+
"$.ts"
120+
],
121+
"width": 30
122+
},
123+
{
124+
"title": "Level",
125+
"kind": "level",
126+
"ref": [
127+
"$.level",
128+
"$.lvl",
129+
"$.l"
130+
],
131+
"width": 10
132+
},
133+
{
134+
"title": "Message",
135+
"kind": "message",
136+
"ref": [
137+
"$.message",
138+
"$.msg",
139+
"$.error",
140+
"$.err"
141+
],
142+
"width": 0 // The width will be calculated automatically.
143+
},
144+
{
145+
"title": "Custom",
146+
"kind": "any",
147+
"ref": [
148+
"$.custom"
149+
],
150+
"width": 0
151+
}
152+
]
153+
}
154+
```
155+
92156
## Resources
93157

94158
Alternatives:

cmd/jlv/main.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"path"
67

78
tea "github.com/charmbracelet/bubbletea"
89

910
"github.com/hedhyw/json-log-viewer/internal/app"
11+
"github.com/hedhyw/json-log-viewer/internal/pkg/config"
1012
)
1113

14+
const configFileName = ".jlv.jsonc"
15+
1216
func main() {
1317
if len(os.Args) != 2 {
1418
fatalf("Invalid arguments, usage: %s file.log\n", os.Args[0])
1519
}
1620

17-
appModel := app.NewModel(os.Args[1])
21+
cfg, err := readConfig()
22+
if err != nil {
23+
fatalf("Error reading config: %s\n", err)
24+
}
25+
26+
appModel := app.NewModel(os.Args[1], cfg)
1827
program := tea.NewProgram(appModel, tea.WithAltScreen())
1928

2029
if _, err := program.Run(); err != nil {
@@ -26,3 +35,21 @@ func fatalf(message string, args ...any) {
2635
fmt.Fprintf(os.Stderr, message, args...)
2736
os.Exit(1)
2837
}
38+
39+
// readConfig tries to read config from working directory or home directory.
40+
// If configs are not found, then it returns a default configuration.
41+
func readConfig() (*config.Config, error) {
42+
paths := []string{}
43+
44+
workDir, err := os.Getwd()
45+
if err == nil {
46+
paths = append(paths, path.Join(workDir, configFileName))
47+
}
48+
49+
homeDir, err := os.UserHomeDir()
50+
if err != nil {
51+
paths = append(paths, path.Join(homeDir, configFileName))
52+
}
53+
54+
return config.Read(paths...)
55+
}

go.mod

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/hedhyw/json-log-viewer
22

3-
go 1.20
3+
go 1.21
44

55
replace github.com/antonmedv/fx => github.com/hedhyw/fx v0.0.1
66

@@ -11,17 +11,23 @@ require (
1111
github.com/charmbracelet/bubbles v0.16.1
1212
github.com/charmbracelet/bubbletea v0.24.2
1313
github.com/charmbracelet/lipgloss v0.7.1
14+
github.com/go-playground/validator/v10 v10.16.0
15+
github.com/hedhyw/jsoncjson v1.1.0
1416
github.com/muesli/reflow v0.3.0
1517
github.com/stretchr/testify v1.8.4
16-
github.com/valyala/fastjson v1.6.4
18+
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0
1719
)
1820

1921
require (
2022
github.com/atotto/clipboard v0.1.4 // indirect
2123
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
2224
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
2325
github.com/davecgh/go-spew v1.1.1 // indirect
26+
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
27+
github.com/go-playground/locales v0.14.1 // indirect
28+
github.com/go-playground/universal-translator v0.18.1 // indirect
2429
github.com/kr/pretty v0.3.0 // indirect
30+
github.com/leodido/go-urn v1.2.4 // indirect
2531
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
2632
github.com/mattn/go-isatty v0.0.19 // indirect
2733
github.com/mattn/go-localereader v0.0.1 // indirect
@@ -33,6 +39,8 @@ require (
3339
github.com/muesli/termenv v0.15.2 // indirect
3440
github.com/pmezard/go-difflib v1.0.0 // indirect
3541
github.com/rivo/uniseg v0.4.4 // indirect
42+
golang.org/x/crypto v0.7.0 // indirect
43+
golang.org/x/net v0.8.0 // indirect
3644
golang.org/x/sync v0.3.0 // indirect
3745
golang.org/x/sys v0.10.0 // indirect
3846
golang.org/x/term v0.10.0 // indirect

go.sum

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@ github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNW
99
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
1010
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
1111
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
12+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1213
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1314
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
15+
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
16+
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
17+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
18+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
19+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
20+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
21+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
22+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
23+
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
24+
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
1425
github.com/hedhyw/bubbles v0.0.2 h1:OqPNGSunmk2B8wE4RcZ4oQvhWkCZNYt8EY8UXuuXJuw=
1526
github.com/hedhyw/bubbles v0.0.2/go.mod h1:XUdibuVUiMfcfKTRla58bmY3TWsdjgF+Rp8pvimQLck=
1627
github.com/hedhyw/fx v0.0.1 h1:h1jJaDnJ6qewSKiD7yooAGZjQwS+yazFcoRgtWV0Rq8=
1728
github.com/hedhyw/fx v0.0.1/go.mod h1:mT/W/Ln5xzLNEh+wGWAsPITPpQV5w6ne7klykEUS78w=
29+
github.com/hedhyw/jsoncjson v1.1.0 h1:uw/aqmbSXAQNJHDPLb+DpwlPNzMREGIsrs+TIwPk+f0=
30+
github.com/hedhyw/jsoncjson v1.1.0/go.mod h1:++nXlbEXzRMcqkoDLvH5I/z5qBkacAWSZDt1u6osUPc=
1831
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1932
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
2033
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
@@ -23,6 +36,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
2336
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2437
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2538
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
39+
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
40+
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
2641
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
2742
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
2843
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
@@ -53,10 +68,20 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
5368
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
5469
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
5570
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
71+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
72+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
73+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
74+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
75+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
76+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
5677
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
5778
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
58-
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
59-
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
79+
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 h1:6fRhSjgLCkTD3JnJxvaJ4Sj+TYblw757bqYgZaOq5ZY=
80+
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0/go.mod h1:/LWChgwKmvncFJFHJ7Gvn9wZArjbV5/FppcK2fKk/tI=
81+
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
82+
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
83+
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
84+
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
6085
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
6186
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
6287
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -72,5 +97,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
7297
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
7398
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
7499
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
100+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
75101
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
76102
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/app/app.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import (
66
tea "github.com/charmbracelet/bubbletea"
77
"github.com/charmbracelet/lipgloss"
88

9+
"github.com/hedhyw/json-log-viewer/internal/pkg/config"
910
"github.com/hedhyw/json-log-viewer/internal/pkg/source"
1011
)
1112

1213
// Model of the application.
1314
type Model struct {
15+
config *config.Config
16+
1417
baseStyle lipgloss.Style
1518
footerStyle lipgloss.Style
1619

@@ -32,16 +35,18 @@ type Model struct {
3235

3336
// NewModel initializes a new application model. It accept the path
3437
// to the file with logs.
35-
func NewModel(path string) Model {
38+
func NewModel(path string, cfg *config.Config) Model {
3639
tableLogs := table.New(
37-
table.WithColumns(getColumns(100)),
40+
table.WithColumns(getColumns(100, cfg)),
3841
table.WithFocused(true),
3942
table.WithHeight(7),
4043
)
4144

4245
tableLogs.SetStyles(getTableStyles())
4346

4447
return Model{
48+
config: cfg,
49+
4550
baseStyle: getBaseStyle(),
4651
footerStyle: getFooterStyle(),
4752

@@ -62,7 +67,7 @@ func NewModel(path string) Model {
6267

6368
// Init implements team.Model interface.
6469
func (m Model) Init() tea.Cmd {
65-
return source.LoadLogsFromFile(m.fileLogPath)
70+
return source.LoadLogsFromFile(m.fileLogPath, m.config)
6671
}
6772

6873
// Update implements team.Model interface.

0 commit comments

Comments
 (0)