From 80eb3a8442d0f398cd93e964064588bd20f3f252 Mon Sep 17 00:00:00 2001 From: Yiting Qiang Date: Thu, 2 Jul 2020 02:06:08 +0800 Subject: [PATCH] add command line options --- config.go | 11 ----- go.mod | 1 - go.sum | 15 +++++++ log_event.go | 2 +- main.go | 113 ++++++++++++++++++++++++++++++++++++++++++++++----- util.go | 2 +- 6 files changed, 119 insertions(+), 25 deletions(-) diff --git a/config.go b/config.go index 2579380..3ed04de 100644 --- a/config.go +++ b/config.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "os" "path/filepath" @@ -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 { diff --git a/go.mod b/go.mod index 10d6eb1..a7d13ad 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 5078555..018da70 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/log_event.go b/log_event.go index e098503..47f2935 100644 --- a/log_event.go +++ b/log_event.go @@ -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() diff --git a/main.go b/main.go index 40208f9..6078905 100644 --- a/main.go +++ b/main.go @@ -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...] ") + fmt.Println(" cat | jog [option...]") + fmt.Println() + + color.OpBold.Println("Options:") + fmt.Printf(" -c, --config 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() diff --git a/util.go b/util.go index 0f3b8b4..01b2438 100644 --- a/util.go +++ b/util.go @@ -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 }