2015-09-11 05:41:58 +02:00
|
|
|
package context
|
|
|
|
|
2017-08-12 00:31:16 +02:00
|
|
|
import "context"
|
|
|
|
|
|
|
|
type versionKey struct{}
|
|
|
|
|
|
|
|
func (versionKey) String() string { return "version" }
|
|
|
|
|
2015-09-11 05:41:58 +02:00
|
|
|
// WithVersion stores the application version in the context. The new context
|
|
|
|
// gets a logger to ensure log messages are marked with the application
|
|
|
|
// version.
|
2017-08-12 00:31:16 +02:00
|
|
|
func WithVersion(ctx context.Context, version string) context.Context {
|
|
|
|
ctx = context.WithValue(ctx, versionKey{}, version)
|
2015-09-11 05:41:58 +02:00
|
|
|
// push a new logger onto the stack
|
2017-08-12 00:31:16 +02:00
|
|
|
return WithLogger(ctx, GetLogger(ctx, versionKey{}))
|
2015-09-11 05:41:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersion returns the application version from the context. An empty
|
|
|
|
// string may returned if the version was not set on the context.
|
2017-08-12 00:31:16 +02:00
|
|
|
func GetVersion(ctx context.Context) string {
|
|
|
|
return GetStringValue(ctx, versionKey{})
|
2015-09-11 05:41:58 +02:00
|
|
|
}
|