()
| 85 | } |
| 86 | |
| 87 | func initCmds() { |
| 88 | RootCmd.PersistentFlags().String("cwd", "", |
| 89 | "Change working directory to the path specified. The parent must exist.") |
| 90 | RootCmd.PersistentFlags().String("profile_mode", "", |
| 91 | "Enable profiling mode, one of [cpu, mem, mutex, block]") |
| 92 | RootCmd.PersistentFlags().Int("block_rate", 0, |
| 93 | "Block profiling rate. Must be used along with block profile_mode") |
| 94 | RootCmd.PersistentFlags().String("config", "", |
| 95 | "Configuration file. Takes precedence over default values, but is "+ |
| 96 | "overridden to values set with environment variables and flags.") |
| 97 | RootCmd.PersistentFlags().Bool("bindall", true, |
| 98 | "Use 0.0.0.0 instead of localhost to bind to all addresses on local machine.") |
| 99 | RootCmd.PersistentFlags().Bool("expose_trace", false, |
| 100 | "Allow trace endpoint to be accessible from remote") |
| 101 | x.Check(rootConf.BindPFlags(RootCmd.PersistentFlags())) |
| 102 | |
| 103 | // Add all existing global flag (eg: from glog) to rootCmd's flags |
| 104 | RootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) |
| 105 | |
| 106 | // Always set stderrthreshold=0. Don't let users set it themselves. |
| 107 | x.Check(RootCmd.PersistentFlags().Set("stderrthreshold", "0")) |
| 108 | x.Check(RootCmd.PersistentFlags().MarkDeprecated("stderrthreshold", |
| 109 | "Dgraph always sets this flag to 0. It can't be overwritten.")) |
| 110 | |
| 111 | for _, sc := range subcommands { |
| 112 | RootCmd.AddCommand(sc.Cmd) |
| 113 | sc.Conf = viper.New() |
| 114 | x.Check(sc.Conf.BindPFlags(sc.Cmd.Flags())) |
| 115 | x.Check(sc.Conf.BindPFlags(RootCmd.PersistentFlags())) |
| 116 | sc.Conf.AutomaticEnv() |
| 117 | sc.Conf.SetEnvPrefix(sc.EnvPrefix) |
| 118 | // Options that contain a "." should use "_" in its place when provided as an |
| 119 | // environment variable. |
| 120 | sc.Conf.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) |
| 121 | } |
| 122 | // For bash shell completion |
| 123 | RootCmd.AddCommand(shellCompletionCmd()) |
| 124 | RootCmd.SetHelpTemplate(x.RootTemplate) |
| 125 | |
| 126 | cobra.OnInitialize(func() { |
| 127 | // When run inside docker, the working_dir is created by root even if afterward |
| 128 | // the process is run as another user. Creating a new working directory here |
| 129 | // ensures that it is owned by the user instead, so it can be cleaned up without |
| 130 | // requiring root when using a bind volume (i.e. a mounted host directory). |
| 131 | if cwd := rootConf.GetString("cwd"); cwd != "" { |
| 132 | err := os.Mkdir(cwd, 0750) |
| 133 | if err != nil && !os.IsExist(err) { |
| 134 | x.Fatalf("unable to create directory: %v", err) |
| 135 | } |
| 136 | x.CheckfNoTrace(os.Chdir(cwd)) |
| 137 | } |
| 138 | |
| 139 | for _, sc := range subcommands { |
| 140 | // Set config file is provided for each subcommand, this is done |
| 141 | // for individual subcommand because each subcommand has its own config |
| 142 | // prefix, like `dgraph zero` expects the prefix to be `DGRAPH_ZERO`. |
| 143 | cfg := sc.Conf.GetString("config") |
| 144 | if cfg == "" { |
no test coverage detected