2015-09-11 03:41:58 +00:00
|
|
|
package context
|
|
|
|
|
2017-08-11 22:31:16 +00:00
|
|
|
import "context"
|
|
|
|
|
|
|
|
type versionKey struct{}
|
|
|
|
|
|
|
|
func (versionKey) String() string { return "version" }
|
|
|
|
|
2015-09-11 03:41:58 +00: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-11 22:31:16 +00:00
|
|
|
func WithVersion(ctx context.Context, version string) context.Context {
|
|
|
|
ctx = context.WithValue(ctx, versionKey{}, version)
|
2015-09-11 03:41:58 +00:00
|
|
|
// push a new logger onto the stack
|
2017-08-11 22:31:16 +00:00
|
|
|
return WithLogger(ctx, GetLogger(ctx, versionKey{}))
|
2015-09-11 03:41:58 +00: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-11 22:31:16 +00:00
|
|
|
func GetVersion(ctx context.Context) string {
|
|
|
|
return GetStringValue(ctx, versionKey{})
|
2015-09-11 03:41:58 +00:00
|
|
|
}
|