From 0810ed134310c402ae0bb900d50d47ded2c43c9e Mon Sep 17 00:00:00 2001 From: miaolz123 Date: Mon, 28 Sep 2020 19:41:34 +0800 Subject: [PATCH] feat: add flag package --- flag/flag.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 9 +++++++++ log/log.go | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 flag/flag.go diff --git a/flag/flag.go b/flag/flag.go new file mode 100644 index 0000000..ec1cdc6 --- /dev/null +++ b/flag/flag.go @@ -0,0 +1,44 @@ +package flag + +import ( + "os" + "path/filepath" + + "github.com/urfave/cli/v2" +) + +var ( + app = cli.NewApp() + actions []cli.ActionFunc +) + +func init() { + app.Name = filepath.Base(os.Args[0]) + app.HideHelp = true + app.Action = action +} + +func action(ctx *cli.Context) error { + if ctx.Bool("help") { + cli.ShowAppHelpAndExit(ctx, 0) + } + + for _, fn := range actions { + if err := fn(ctx); err != nil { + return err + } + } + return nil +} + +func Add(flag cli.Flag, fn cli.ActionFunc) { + app.Flags = append(app.Flags, flag) + if fn != nil { + actions = append(actions, fn) + } +} + +func Run() error { + app.Flags = append(app.Flags, cli.HelpFlag) + return app.Run(os.Args) +} diff --git a/go.mod b/go.mod index 252b68d..28035c1 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/shopspring/decimal v1.2.0 github.com/stretchr/testify v1.6.1 github.com/tidwall/gjson v1.6.1 + github.com/urfave/cli/v2 v2.2.0 golang.org/x/net v0.0.0-20200923182212-328152dc79b1 xorm.io/xorm v1.0.5 ) diff --git a/go.sum b/go.sum index 924568d..1930854 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,7 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s= gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.3.3 h1:CWUqKXe0s8A2z6qCgkP4Kru7wC11YoAnoupUKFDnH08= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= @@ -10,6 +11,8 @@ github.com/bsm/redislock v0.6.0 h1:eAzUAcN8EN1cbJAwSRTUvORfaEe9o8GFwNkNXMecegY= github.com/bsm/redislock v0.6.0/go.mod h1:3Kgu+cXw0JrkZ5pmY/JbcFpixGZ5M9v9G2PGWYqku+k= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -97,8 +100,12 @@ 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/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +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/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= +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 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -113,6 +120,8 @@ github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc= github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU= github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0= go.opentelemetry.io/otel v0.11.0 h1:IN2tzQa9Gc4ZVKnTaMbPVcHjvzOdg5n9QfnmlqiET7E= go.opentelemetry.io/otel v0.11.0/go.mod h1:G8UCk+KooF2HLkgo8RHX9epABH/aRGYET7gQOqBVdB0= diff --git a/log/log.go b/log/log.go index 3fe5b77..ddb593c 100644 --- a/log/log.go +++ b/log/log.go @@ -5,9 +5,11 @@ import ( "os" "path/filepath" + "github.com/gxxgle/go-utils/flag" "github.com/gxxgle/go-utils/path" "github.com/phuslu/log" + "github.com/urfave/cli/v2" ) var ( @@ -19,6 +21,44 @@ func init() { log.DefaultLogger.Caller = 1 } +func InitFromFlag() { + flag.Add(&cli.StringFlag{ + Name: "log_level", + Usage: "set log level. (debug, info, error)", + EnvVars: []string{"LOG_LEVEL"}, + Value: "info", + }, func(ctx *cli.Context) error { + lvl := log.ParseLevel(ctx.String("log_level")) + if lvl >= log.TraceLevel && lvl <= log.PanicLevel { + log.DefaultLogger.SetLevel(lvl) + } + return nil + }) + + flag.Add(&cli.StringFlag{ + Name: "log_type", + Usage: "set log type. (json, console, color_console)", + EnvVars: []string{"LOG_TYPE"}, + Value: "json", + }, func(ctx *cli.Context) error { + switch ctx.String("log_type") { + case "console": + Console() + case "color_console": + ColorConsole() + } + return nil + }) +} + +func Console() { + log.DefaultLogger.Writer = &log.ConsoleWriter{ + ColorOutput: false, + QuoteString: false, + EndWithMessage: false, + } +} + func ColorConsole() { log.DefaultLogger.Writer = &log.ConsoleWriter{ ColorOutput: true,