| 65 | } |
| 66 | |
| 67 | func initCommandContext(cmd *cobra.Command) error { |
| 68 | finishCommandContext() |
| 69 | |
| 70 | ctx := cmd.Context() |
| 71 | if ctx == nil { |
| 72 | ctx = context.Background() |
| 73 | } |
| 74 | |
| 75 | timeout, err := commandTimeout(cmd) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | if timeout < 0 { |
| 80 | return invalidArgumentsErrorWithDetails("`--timeout` must be greater than or equal to 0", flagErrorDetails("timeout")) |
| 81 | } |
| 82 | if timeout > 0 { |
| 83 | ctx, commandContextCancel = context.WithTimeout(ctx, timeout) |
| 84 | cmd.SetContext(ctx) |
| 85 | } |
| 86 | commandContext = ctx |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | func commandTimeout(cmd *cobra.Command) (time.Duration, error) { |
| 91 | for _, flags := range []interface { |