Skip to content

Commit

Permalink
Suppress logging before flag.Parse from glog (#2970)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jarifibrahim authored and martinmr committed Feb 4, 2019
1 parent 2b943ca commit 78aa45d
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions dgraph/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package cmd

import (
goflag "flag"
"fmt"
"os"
"strings"

"github.com/dgraph-io/dgraph/dgraph/cmd/alpha"
Expand Down Expand Up @@ -56,11 +54,12 @@ cluster.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
initCmds()
goflag.Parse()
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}

// Convinces goflags that Parse() has been called to avoid noisy logs.
// https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212
x.Check(goflag.CommandLine.Parse([]string{}))

x.Check(RootCmd.Execute())
}

var rootConf = viper.New()
Expand Down

0 comments on commit 78aa45d

Please sign in to comment.