-
Notifications
You must be signed in to change notification settings - Fork 63
/
context.go
41 lines (33 loc) · 1.05 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package iostreams
import (
"context"
)
var (
// G is an alias for FromContext.
//
// We may want to define this locally to a package to get package tagged
// iostreams.
G = FromContext
// L is the system IO stream.
IO = System()
)
// contextKey is used to retrieve the logger from the context.
type contextKey struct{}
// WithIOStreams returns a new context with the provided logger. Use in
// combination with logger.WithField(s) for great effect.
func WithIOStreams(ctx context.Context, iostreams *IOStreams) context.Context {
return context.WithValue(ctx, contextKey{}, iostreams)
}
// FromContext returns the logger kraftkit in the context, or an inert logger
// that will not log anything.
func FromContext(ctx context.Context) *IOStreams {
l := ctx.Value(contextKey{})
if l == nil {
return IO
}
return l.(*IOStreams)
}