From 3401ace66666f64f3993237c4cfb44fa0241b07b Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Sat, 2 Feb 2019 18:29:30 +0530 Subject: [PATCH 1/4] Suppress `logging before flag.Parse` from glog Replace `goflag.Parse()` call with `goflag.CommandLine.Parse([]string{})` This serves multiple purposes 1. We don't actually parse the os.Args (which Parse() method internally does). This ensures that the command line arguments are parsed only by RootCmd.Execute() call (which is the parent command) 2. We suppress the `Error: Logging before flag.Parse...` warning messages. https://github.com/golang/glog/blob/master/glog.go#L679 causes the warning message and we suppress this warning by calling the Parse method on the underlying flagset. Signed-off-by: Ibrahim Jarif --- dgraph/cmd/root.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dgraph/cmd/root.go b/dgraph/cmd/root.go index bdb132c35f4..7afba0a062d 100644 --- a/dgraph/cmd/root.go +++ b/dgraph/cmd/root.go @@ -56,7 +56,11 @@ cluster. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { initCmds() - goflag.Parse() + + // Convinces goflags that we have called Parse() to avoid noisy logs. + // https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212 + goflag.CommandLine.Parse([]string{}) + if err := RootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) From 26665cffd4948e831a1f8e5e0e5daac55c7af2d8 Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Sat, 2 Feb 2019 18:57:13 +0530 Subject: [PATCH 2/4] Don't check for error returned by CommandLine.Parse([]string{}) Signed-off-by: Ibrahim Jarif --- dgraph/cmd/root.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dgraph/cmd/root.go b/dgraph/cmd/root.go index 7afba0a062d..52230976064 100644 --- a/dgraph/cmd/root.go +++ b/dgraph/cmd/root.go @@ -59,7 +59,8 @@ func Execute() { // Convinces goflags that we have called Parse() to avoid noisy logs. // https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212 - goflag.CommandLine.Parse([]string{}) + // We don't need to check the error here. + _ = goflag.CommandLine.Parse([]string{}) if err := RootCmd.Execute(); err != nil { fmt.Println(err) From cbb3c0c1a1b69b620c2997faaa685fc8cefedcd2 Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Mon, 4 Feb 2019 23:53:27 +0530 Subject: [PATCH 3/4] Reword comment and add error checking --- dgraph/cmd/root.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dgraph/cmd/root.go b/dgraph/cmd/root.go index 52230976064..afef7d78e5a 100644 --- a/dgraph/cmd/root.go +++ b/dgraph/cmd/root.go @@ -57,10 +57,9 @@ cluster. func Execute() { initCmds() - // Convinces goflags that we have called Parse() to avoid noisy logs. + // Convinces goflags that Parse() has been called to avoid noisy logs. // https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212 - // We don't need to check the error here. - _ = goflag.CommandLine.Parse([]string{}) + x.Check(goflag.CommandLine.Parse([]string{})) if err := RootCmd.Execute(); err != nil { fmt.Println(err) From 80e5237be45478d981093847bd29720e0f708c55 Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Tue, 5 Feb 2019 00:21:45 +0530 Subject: [PATCH 4/4] Wrap rootCmd.Execute in x.Check() --- dgraph/cmd/root.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/dgraph/cmd/root.go b/dgraph/cmd/root.go index afef7d78e5a..ebec7cc7dbc 100644 --- a/dgraph/cmd/root.go +++ b/dgraph/cmd/root.go @@ -18,8 +18,6 @@ package cmd import ( goflag "flag" - "fmt" - "os" "strings" "github.com/dgraph-io/dgraph/dgraph/cmd/alpha" @@ -61,10 +59,7 @@ func Execute() { // https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212 x.Check(goflag.CommandLine.Parse([]string{})) - if err := RootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) - } + x.Check(RootCmd.Execute()) } var rootConf = viper.New()