forked from distribution/distribution
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WithVersion to context and other cleanup
By adding WithVersion to the context package, we can simplify context setup in the application. This avoids some odd bugs where instantiation order can lead to missing instance.id or version from log messages. Signed-off-by: Stephen J Day <[email protected]>
- Loading branch information
Showing
6 changed files
with
62 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package context | ||
|
||
// WithVersion stores the application version in the context. The new context | ||
// gets a logger to ensure log messages are marked with the application | ||
// version. | ||
func WithVersion(ctx Context, version string) Context { | ||
ctx = WithValue(ctx, "version", version) | ||
// push a new logger onto the stack | ||
return WithLogger(ctx, GetLogger(ctx, "version")) | ||
} | ||
|
||
// GetVersion returns the application version from the context. An empty | ||
// string may returned if the version was not set on the context. | ||
func GetVersion(ctx Context) string { | ||
return GetStringValue(ctx, "version") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package context | ||
|
||
import "testing" | ||
|
||
func TestVersionContext(t *testing.T) { | ||
ctx := Background() | ||
|
||
if GetVersion(ctx) != "" { | ||
t.Fatalf("context should not yet have a version") | ||
} | ||
|
||
expected := "2.1-whatever" | ||
ctx = WithVersion(ctx, expected) | ||
version := GetVersion(ctx) | ||
|
||
if version != expected { | ||
t.Fatalf("version was not set: %q != %q", version, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters