Skip to content

Commit 041a4d6

Browse files
authored
feat(#72): print version (#76)
1 parent 120d8df commit 041a4d6

File tree

7 files changed

+67
-16
lines changed

7 files changed

+67
-16
lines changed

Makefile

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ VERSION?=${shell git describe --tags}
55

66
all: lint test build
77

8-
run:
9-
@echo "building ${VERSION}"
10-
go run ./cmd/jlv assets/example.log
11-
.PHONY: build
8+
run: build
9+
./bin/jlv assets/example.log
10+
.PHONY: run
1211

13-
run.stdin:
14-
@echo "building ${VERSION}"
15-
go run ./cmd/jlv < assets/example.log
16-
.PHONY: build
12+
run.version: build
13+
./bin/jlv --version
14+
.PHONY: run.version
15+
16+
run.stdin: build
17+
./bin/jlv < assets/example.log
18+
.PHONY: run.stdin
1719

1820
build:
1921
@echo "building ${VERSION}"

cmd/jlv/main.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"flag"
56
"fmt"
67
"os"
@@ -15,12 +16,23 @@ import (
1516
"github.com/hedhyw/json-log-viewer/internal/pkg/source/readerinput"
1617
)
1718

19+
// version will be set on build.
20+
var version = "development"
21+
1822
const configFileName = ".jlv.jsonc"
1923

2024
func main() {
2125
configPath := flag.String("config", "", "Path to the config")
26+
printVersion := flag.Bool("version", false, "Print version")
2227
flag.Parse()
2328

29+
if *printVersion {
30+
// nolint: forbidigo // Version command.
31+
print("github.com/hedhyw/json-log-viewer@" + version + "\n")
32+
33+
return
34+
}
35+
2436
cfg, err := readConfig(*configPath)
2537
if err != nil {
2638
fatalf("Error reading config: %s\n", err)
@@ -30,21 +42,37 @@ func main() {
3042

3143
switch flag.NArg() {
3244
case 0:
33-
sourceInput = readerinput.New(os.Stdin, cfg.StdinReadTimeout)
45+
sourceInput, err = getStdinSource(cfg)
46+
if err != nil {
47+
fatalf("Stdin: %s\n", err)
48+
}
3449
case 1:
3550
sourceInput = fileinput.New(flag.Arg(0))
3651
default:
3752
fatalf("Invalid arguments, usage: %s file.log\n", os.Args[0])
3853
}
3954

40-
appModel := app.NewModel(sourceInput, cfg)
41-
program := tea.NewProgram(appModel, tea.WithAltScreen())
55+
appModel := app.NewModel(sourceInput, cfg, version)
56+
program := tea.NewProgram(appModel, tea.WithInputTTY(), tea.WithAltScreen())
4257

4358
if _, err := program.Run(); err != nil {
4459
fatalf("Error running program: %s\n", err)
4560
}
4661
}
4762

63+
func getStdinSource(cfg *config.Config) (source.Input, error) {
64+
stat, err := os.Stdin.Stat()
65+
if err != nil {
66+
return nil, fmt.Errorf("stat: %w", err)
67+
}
68+
69+
if stat.Mode()&os.ModeNamedPipe == 0 {
70+
return readerinput.New(bytes.NewReader(nil), cfg.StdinReadTimeout), nil
71+
}
72+
73+
return readerinput.New(os.Stdin, cfg.StdinReadTimeout), nil
74+
}
75+
4876
func fatalf(message string, args ...any) {
4977
fmt.Fprintf(os.Stderr, message, args...)
5078
os.Exit(1)

internal/app/app.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ type Application struct {
1717
FooterStyle lipgloss.Style
1818

1919
LastWindowSize tea.WindowSizeMsg
20+
21+
Version string
2022
}
2123

22-
func newApplication(sourceInput source.Input, config *config.Config) Application {
24+
func newApplication(
25+
sourceInput source.Input,
26+
config *config.Config,
27+
version string,
28+
) Application {
2329
const (
2430
initialWidth = 70
2531
initialHeight = 20
@@ -36,11 +42,17 @@ func newApplication(sourceInput source.Input, config *config.Config) Application
3642
Width: initialWidth,
3743
Height: initialHeight,
3844
},
45+
46+
Version: version,
3947
}
4048
}
4149

4250
// NewModel initializes a new application model. It accept the path
4351
// to the file with logs.
44-
func NewModel(sourceInput source.Input, config *config.Config) tea.Model {
45-
return newStateInitial(newApplication(sourceInput, config))
52+
func NewModel(
53+
sourceInput source.Input,
54+
config *config.Config,
55+
version string,
56+
) tea.Model {
57+
return newStateInitial(newApplication(sourceInput, config, version))
4658
}

internal/app/app_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import (
1414
"github.com/hedhyw/json-log-viewer/internal/pkg/tests"
1515
)
1616

17+
const testVersion = "v0.0.1"
18+
1719
func newTestModel(tb testing.TB, content []byte) tea.Model {
1820
tb.Helper()
1921

2022
testFile := tests.RequireCreateFile(tb, content)
2123

22-
model := app.NewModel(fileinput.New(testFile), config.GetDefaultConfig())
24+
model := app.NewModel(fileinput.New(testFile), config.GetDefaultConfig(), testVersion)
2325
model = handleUpdate(model, model.Init()())
2426

2527
return model

internal/app/stateinitial_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestStateInitial(t *testing.T) {
2222
model := app.NewModel(
2323
readerinput.New(bytes.NewReader([]byte{}), time.Millisecond),
2424
config.GetDefaultConfig(),
25+
testVersion,
2526
)
2627

2728
_, ok := model.(app.StateInitialModel)

internal/app/stateloaded.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (s StateLoadedModel) viewTable() string {
6767
}
6868

6969
func (s StateLoadedModel) viewHelp() string {
70-
return "\n" + s.help.View(s.keys)
70+
return "\n" + s.Version + " " + s.help.View(s.keys)
7171
}
7272

7373
// Update handles events. It implements tea.Model.

internal/app/stateloaded_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func TestStateLoaded(t *testing.T) {
5757
_, ok = model.(app.StateErrorModel)
5858
assert.Truef(t, ok, "%s", model)
5959
})
60+
61+
t.Run("version_printed", func(t *testing.T) {
62+
t.Parallel()
63+
64+
assert.Contains(t, model.View(), testVersion)
65+
})
6066
}
6167

6268
func TestStateLoadedQuit(t *testing.T) {

0 commit comments

Comments
 (0)