Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.
()
| 127 | // Execute adds all child commands to the root command and sets flags appropriately. |
| 128 | // This is called by main.main(). It only needs to happen once to the rootCmd. |
| 129 | func Execute() { |
| 130 | interrupt.Global.Start() |
| 131 | |
| 132 | // disable klog |
| 133 | disableKlog() |
| 134 | |
| 135 | // create a new factory |
| 136 | f := factory.DefaultFactory() |
| 137 | |
| 138 | // build the root command |
| 139 | rootCmd := BuildRoot(f, false) |
| 140 | |
| 141 | // set version for --version flag |
| 142 | rootCmd.Version = upgrade.GetVersion() |
| 143 | |
| 144 | // before hooks |
| 145 | pluginErr := hook.ExecuteHooks(nil, nil, "root", "root.beforeExecute", "command:before:execute") |
| 146 | if pluginErr != nil { |
| 147 | f.GetLog().Fatalf("%+v", pluginErr) |
| 148 | } |
| 149 | |
| 150 | // execute command |
| 151 | err := rootCmd.Execute() |
| 152 | |
| 153 | // after hooks |
| 154 | pluginErr = hook.ExecuteHooks(nil, map[string]interface{}{"error": err}, "root.afterExecute", "command:after:execute") |
| 155 | if err != nil { |
| 156 | // Check if return code error |
| 157 | retCode, ok := errors.Cause(err).(*exit.ReturnCodeError) |
| 158 | if ok { |
| 159 | os.Exit(retCode.ExitCode) |
| 160 | } |
| 161 | |
| 162 | // error hooks |
| 163 | pluginErr := hook.ExecuteHooks(nil, map[string]interface{}{"error": err}, "root.errorExecution", "command:error") |
| 164 | if pluginErr != nil { |
| 165 | f.GetLog().Fatalf("%+v", pluginErr) |
| 166 | } |
| 167 | |
| 168 | if globalFlags.Debug { |
| 169 | f.GetLog().Fatalf("%+v", err) |
| 170 | } else { |
| 171 | f.GetLog().Fatal(err) |
| 172 | } |
| 173 | } else if pluginErr != nil { |
| 174 | f.GetLog().Fatalf("%+v", pluginErr) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // BuildRoot creates a new root command from the |
| 179 | func BuildRoot(f factory.Factory, excludePlugins bool) *cobra.Command { |
no test coverage detected