From 33b474b224936e25fa3f57aa238220cd3573b0a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Sat, 7 Jul 2018 02:45:14 +0200 Subject: [PATCH] Create a globalOptions structure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This works just like the command-specific options. Also moves the "Before:" handler into a separate method. Does not change behavior. Signed-off-by: Miloslav Trmač --- cmd/skopeo/main.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/skopeo/main.go b/cmd/skopeo/main.go index 6a2fd560..852e8cdf 100644 --- a/cmd/skopeo/main.go +++ b/cmd/skopeo/main.go @@ -15,8 +15,13 @@ import ( // and will be populated by the Makefile var gitCommit = "" +type globalOptions struct { +} + // createApp returns a cli.App to be run or tested. func createApp() *cli.App { + opts := globalOptions{} + app := cli.NewApp() app.EnableBashCompletion = true app.Name = "skopeo" @@ -65,15 +70,7 @@ func createApp() *cli.App { Usage: "timeout for the command execution", }, } - app.Before = func(c *cli.Context) error { - if c.GlobalBool("debug") { - logrus.SetLevel(logrus.DebugLevel) - } - if c.GlobalIsSet("tls-verify") { - logrus.Warn("'--tls-verify' is deprecated, please set this on the specific subcommand") - } - return nil - } + app.Before = opts.before app.Commands = []cli.Command{ copyCmd(), inspectCmd(), @@ -87,6 +84,17 @@ func createApp() *cli.App { return app } +// before is run by the cli package for any command, before running the command-specific handler. +func (opts *globalOptions) before(c *cli.Context) error { + if c.GlobalBool("debug") { + logrus.SetLevel(logrus.DebugLevel) + } + if c.GlobalIsSet("tls-verify") { + logrus.Warn("'--tls-verify' is deprecated, please set this on the specific subcommand") + } + return nil +} + func main() { if reexec.Init() { return