Execute adds all child commands to the root command and sets flags appropriately. Returns the process exit code; main is responsible for os.Exit so deferred tty restore runs on every path (os.Exit skips defers).
()
| 57 | // Returns the process exit code; main is responsible for os.Exit so deferred tty |
| 58 | // restore runs on every path (os.Exit skips defers). |
| 59 | func Execute() int { |
| 60 | // Heal an inherited-broken tty (e.g. left in raw mode by a previous tnr |
| 61 | // process that was SIGKILL'd) before snapshotting. Detect-and-heal only |
| 62 | // touches the tty when canonical cooked-mode bits are missing, so users |
| 63 | // with custom termios setups are unaffected. No-op on Windows. |
| 64 | tty.Heal() |
| 65 | // Snapshot tty state on entry so a panic or error-return from any subcommand |
| 66 | // (typically a Bubble Tea TUI that put the terminal in raw mode and didn't |
| 67 | // get a chance to restore it) doesn't strand the user's shell. |
| 68 | restoreTTY := tty.Snapshot() |
| 69 | defer restoreTTY() |
| 70 | defer func() { |
| 71 | if r := recover(); r != nil { |
| 72 | restoreTTY() |
| 73 | panic(r) |
| 74 | } |
| 75 | }() |
| 76 | |
| 77 | c, err := rootCmd.ExecuteC() |
| 78 | if err != nil { |
| 79 | if !isUserError(err) { |
| 80 | sentry.WithScope(func(scope *sentry.Scope) { |
| 81 | scope.SetTag("source", "catch_all") |
| 82 | if c != nil { |
| 83 | scope.SetTag("command", c.Name()) |
| 84 | } |
| 85 | sentry.CaptureException(err) |
| 86 | }) |
| 87 | sentry.Flush(2 * time.Second) |
| 88 | } |
| 89 | PrintError(err) |
| 90 | return 1 |
| 91 | } |
| 92 | return 0 |
| 93 | } |
| 94 | |
| 95 | var userErrorSubstrings = []string{ |
| 96 | // Cobra internals (no hook to wrap these at source) |
no test coverage detected