AppContext returns the context for a command. Should only be called once per command, near the start. This will ensure the namespace is picked up and set the timeout, if one is defined.
(cliContext *cli.Context)
| 35 | // This will ensure the namespace is picked up and set the timeout, if one is |
| 36 | // defined. |
| 37 | func AppContext(cliContext *cli.Context) (context.Context, context.CancelFunc) { |
| 38 | var ( |
| 39 | ctx = cliContext.Context |
| 40 | timeout = cliContext.Duration("timeout") |
| 41 | namespace = cliContext.String("namespace") |
| 42 | cancel context.CancelFunc |
| 43 | ) |
| 44 | ctx = namespaces.WithNamespace(ctx, namespace) |
| 45 | if timeout > 0 { |
| 46 | ctx, cancel = context.WithTimeout(ctx, timeout) |
| 47 | } else { |
| 48 | ctx, cancel = context.WithCancel(ctx) |
| 49 | } |
| 50 | if tm, err := epoch.SourceDateEpoch(); err != nil { |
| 51 | log.L.WithError(err).Warn("Failed to read SOURCE_DATE_EPOCH") |
| 52 | } else if tm != nil { |
| 53 | log.L.Debugf("Using SOURCE_DATE_EPOCH: %v", tm) |
| 54 | ctx = epoch.WithSourceDateEpoch(ctx, tm) |
| 55 | } |
| 56 | return ctx, cancel |
| 57 | } |
| 58 | |
| 59 | // NewClient returns a new containerd client |
| 60 | func NewClient(cliContext *cli.Context, opts ...containerd.Opt) (*containerd.Client, context.Context, context.CancelFunc, error) { |
no test coverage detected
searching dependent graphs…