11package main
22
33import (
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+
1822const configFileName = ".jlv.jsonc"
1923
2024func 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+
4876func fatalf (message string , args ... any ) {
4977 fmt .Fprintf (os .Stderr , message , args ... )
5078 os .Exit (1 )
0 commit comments