Skip to content

Commit

Permalink
add command line options
Browse files Browse the repository at this point in the history
  • Loading branch information
yqiangsap committed Jul 1, 2020
1 parent 3c01150 commit 80eb3a8
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 25 deletions.
11 changes: 0 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -34,16 +33,6 @@ func lookForConfigFile(dir string) string {

// DetermineConfigFilePath return (file path)
func DetermineConfigFilePath() string {
for i, arg := range os.Args {
if arg == "-c" {
log.Println("got -c option")
if i == len(os.Args)-1 {
panic(fmt.Errorf("missing config file for -c option"))
}
return os.Args[i+1]
}
}

dir := ExeDirectory()
r := lookForConfigFile(dir)
if len(r) != 0 {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ require (
github.com/gookit/color v1.2.5
github.com/gookit/goutil v0.2.7
github.com/pkg/errors v0.9.1
github.com/qiangyt/j2log v0.0.0-20200701164106-72dc62926f43 // indirect
gopkg.in/yaml.v2 v2.3.0
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -9,6 +12,8 @@ github.com/gookit/color v1.2.5 h1:s1gzb/fg3HhkSLKyWVUsZcVBUo+R1TwEYTmmxH8gGFg=
github.com/gookit/color v1.2.5/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg=
github.com/gookit/filter v1.1.0 h1:K7RTF0miQpkwLThkcbuDDebtVNGeXoYgG7+dOsoZHkA=
github.com/gookit/filter v1.1.0/go.mod h1:goEI07jAkSf3wAoa7IWi6Ex8qzLHx9R5/Phv3opvKh4=
github.com/gookit/gcli v1.2.1 h1:a/DApyzDT/468l/v8oTGwd1TYDdD6Q+a1aWQ+IZoD24=
github.com/gookit/gcli/v2 v2.2.1 h1:7uzRBODHIOl0vbmG3sPmQ2dn4f+0mxFPaoj2Mb2IFts=
github.com/gookit/gcli/v2 v2.2.1/go.mod h1:E0Xwlbtx42V2bpx/Z9v0t01yTbOxwTEcwVZAesbkC/Q=
github.com/gookit/goutil v0.2.3/go.mod h1:8emMcACka2rFot/L9ZO7r3zjWiitzIhB/CfWXUCW75w=
github.com/gookit/goutil v0.2.7 h1:9cI9yDIBKT+RJHq6o3kRa3snQfozUkJoOwVH1aHGJhA=
Expand All @@ -27,17 +32,27 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qiangyt/j2log v0.0.0-20200701164106-72dc62926f43 h1:Yv1xNPeMRO86alT3xC/WcA5P78OXI6Ecm7QHFMhcCHQ=
github.com/qiangyt/j2log v0.0.0-20200701164106-72dc62926f43/go.mod h1:vXUowHLawrg5B3K9EoCxH14nXL601YM+wPPbY68Js3Y=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA=
github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e h1:D5TXcfTk7xF7hvieo4QErS3qqCB4teTffacDWr7CI+0=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
2 changes: 1 addition & 1 deletion log_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func ProcessRawLine(cfg Config, index int, raw string) {
func ProcessLinesWithLocalFile(cfg Config, localFilePath string) {
f, err := os.Open(localFilePath)
if err != nil {
panic(errors.Wrapf(err, "failed to read file: %s", localFilePath))
panic(errors.Wrap(err, ""))
}
log.Printf("file is opened: %s\n", localFilePath)
defer f.Close()
Expand Down
113 changes: 102 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,118 @@ import (
"fmt"
"log"
"os"

"github.com/gookit/color"
)

const (
// AppVersion ...
AppVersion = "v0.9.0"
)

// PrintVersion ...
func PrintVersion() {
fmt.Println(AppVersion)
}

// PrintConfigTemplate ...
func PrintConfigTemplate() {
fmt.Println(ConfigDefaultYAML)
}

// PrintHelp ...
func PrintHelp() {
color.New(color.Blue, color.OpBold).Println("Convert and view structured (JSON) log")
PrintVersion()
fmt.Println()

color.OpBold.Println("Usage:")
fmt.Println(" jog [option...] <your JSON log file path>")
fmt.Println(" cat <your JSON file path> | jog [option...]")
fmt.Println()

color.OpBold.Println("Options:")
fmt.Printf(" -c, --config <config file path> Specify config YAML file path. The default is .jog.yaml or $HOME/.job.yaml \n")
fmt.Printf(" -t, --template Print a config YAML file template\n")
fmt.Printf(" -h, --help Display this information\n")
fmt.Printf(" -V, --version Display app version information\n")
fmt.Printf(" -d, --debug Print more error detail\n")
fmt.Println()
}

func main() {
logFile := InitLogger()
defer logFile.Close()

cfg := ConfigWithDefaultYamlFile()
var configFilePath string
var logFilePath string
var debug bool
// logFilePath = "./example_logs/logstash.log"

for i := 0; i < len(os.Args); i++ {
if i == 0 {
continue
}

var filePath string
filePath = "./example_logs/logstash.log"
arg := os.Args[i]

if arg[0:1] == "-" {
if arg == "-c" || arg == "--config" {
if i+1 >= len(os.Args) {
color.Red.Println("Missing config file path\n")
PrintHelp()
return
}

if i+1 < len(os.Args) {
configFilePath = os.Args[i+1]
}
i++
} else if arg == "-t" || arg == "--template" {
PrintConfigTemplate()
return
} else if arg == "-h" || arg == "--help" {
PrintHelp()
return
} else if arg == "-V" || arg == "--version" {
PrintVersion()
return
} else if arg == "-d" || arg == "--debug" {
debug = true
} else {
color.Red.Printf("Unknown option: '%s'\n\n", arg)
PrintHelp()
return
}
} else {
logFilePath = arg
}
}

if len(filePath) == 0 && len(os.Args) == 2 {
filePath = os.Args[1]
if !debug {
defer func() {
if p := recover(); p != nil {
color.Red.Printf("%v\n\n", p)
os.Exit(1)
return
}
}()
}

logFile := InitLogger()
defer logFile.Close()

var cfg Config
if len(configFilePath) == 0 {
cfg = ConfigWithDefaultYamlFile()
} else {
cfg = ConfigWithYamlFile(configFilePath)
}

if len(filePath) == 0 {
log.Println("Read log lines from stdin")
if len(logFilePath) == 0 {
log.Println("Read JSON log lines from stdin")
ProcessLinesWithReader(cfg, os.Stdin)
} else {
log.Printf("processing local file: %s\n", filePath)
ProcessLinesWithLocalFile(cfg, filePath)
log.Printf("processing local JSON log file: %s\n", logFilePath)
ProcessLinesWithLocalFile(cfg, logFilePath)
}

fmt.Println()
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func RemoveFile(path string) {
func ReadFile(path string) []byte {
r, err := ioutil.ReadFile(path)
if err != nil {
panic(errors.Wrap(err, "failed to read file: "+path))
panic(errors.Wrap(err, ""))
}
return r
}

0 comments on commit 80eb3a8

Please sign in to comment.