From bf97435eb34f00368f93ea88d13d1f954a8a01db Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Mon, 16 Jan 2023 21:45:20 +0100 Subject: [PATCH] replace command line parser (#1383) --- go.mod | 4 +--- go.sum | 11 +++++------ internal/core/core.go | 36 +++++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index bb4ba083256..501d1033b55 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( code.cloudfoundry.org/bytefmt v0.0.0 github.com/abema/go-mp4 v0.9.0 + github.com/alecthomas/kong v0.7.1 github.com/aler9/gortsplib/v2 v2.0.5 github.com/asticode/go-astits v1.10.1-0.20220319093903-4abe66a9b757 github.com/fsnotify/fsnotify v1.4.9 @@ -22,13 +23,10 @@ require ( github.com/pion/webrtc/v3 v3.1.47 github.com/stretchr/testify v1.7.1 golang.org/x/crypto v0.0.0-20221010152910-d6f0a8c073c2 - gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v2 v2.4.0 ) require ( - github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect - github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect github.com/asticode/go-astikit v0.20.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect diff --git a/go.sum b/go.sum index 79436e32dec..6c2f8d622af 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,9 @@ github.com/abema/go-mp4 v0.9.0 h1:WFkzn0J8uYTQ2MIWfgCaFHRB3VDkird5JncIjuuKjGI= github.com/abema/go-mp4 v0.9.0/go.mod h1:vPl9t5ZK7K0x68jh12/+ECWBCXoWuIDtNgPtU2f04ws= -github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= -github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E= -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= +github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0= +github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4= +github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U= +github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE= github.com/aler9/gortsplib/v2 v2.0.5 h1:K1dxSVMCRB22tNcpqAjSQBHj6YoIp2ad0FakEJAe+lo= github.com/aler9/gortsplib/v2 v2.0.5/go.mod h1:cGD/4JW43Elv/f4HfNBdH78IO5ZfwZIY52fByVASmsU= github.com/aler9/writerseeker v0.0.0-20220601075008-6f0e685b9c82 h1:9WgSzBLo3a9ToSVV7sRTBYZ1GGOZUpq4+5H3SN0UZq4= @@ -61,6 +61,7 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grafov/m3u8 v0.11.1 h1:igZ7EBIB2IAsPPazKwRKdbhxcoBKO3lO1UY57PZDeNA= github.com/grafov/m3u8 v0.11.1/go.mod h1:nqzOkfBiZJENr52zTVd/Dcl03yzphIMbJqkXGu+u080= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= @@ -237,8 +238,6 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= -gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/core/core.go b/internal/core/core.go index 1d1b743b1a0..4fc6f8b9929 100644 --- a/internal/core/core.go +++ b/internal/core/core.go @@ -8,9 +8,9 @@ import ( "os/signal" "reflect" + "github.com/alecthomas/kong" "github.com/aler9/gortsplib/v2" "github.com/gin-gonic/gin" - "gopkg.in/alecthomas/kingpin.v2" "github.com/aler9/rtsp-simple-server/internal/conf" "github.com/aler9/rtsp-simple-server/internal/confwatcher" @@ -49,17 +49,32 @@ type Core struct { done chan struct{} } +var cli struct { + Version bool `help:"print version"` + Confpath string `arg:"" default:"rtsp-simple-server.yml"` +} + // New allocates a core. func New(args []string) (*Core, bool) { - k := kingpin.New("rtsp-simple-server", "rtsp-simple-server "+version) - - argVersion := k.Flag("version", "print version").Bool() - argConfPath := k.Arg("confpath", "path to a config file. The default is rtsp-simple-server.yml."). - Default("rtsp-simple-server.yml").String() - - kingpin.MustParse(k.Parse(args)) + parser, err := kong.New(&cli, + kong.Description("rtsp-simple-server "+version), + kong.UsageOnError(), + kong.ValueFormatter(func(value *kong.Value) string { + switch value.Name { + case "confpath": + return "path to a config file. The default is rtsp-simple-server.yml." + + default: + return kong.DefaultHelpValueFormatter(value) + } + })) + if err != nil { + panic(err) + } + _, err = parser.Parse(args) + parser.FatalIfErrorf(err) - if *argVersion { + if cli.Version { fmt.Println(version) os.Exit(0) } @@ -76,12 +91,11 @@ func New(args []string) (*Core, bool) { p := &Core{ ctx: ctx, ctxCancel: ctxCancel, - confPath: *argConfPath, + confPath: cli.Confpath, chAPIConfigSet: make(chan *conf.Conf), done: make(chan struct{}), } - var err error p.conf, p.confFound, err = conf.Load(p.confPath) if err != nil { fmt.Printf("ERR: %s\n", err)