-
Notifications
You must be signed in to change notification settings - Fork 13
/
context.go
117 lines (94 loc) · 2.5 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// SPDX-License-Identifier: Apache-2.0
package pipeline
import (
"context"
)
// contextKey defines the key type for
// storing pipeline types in a context.
type contextKey int
const (
// buildKey defines the key type for
// storing a Build type in a context.
buildKey contextKey = iota
// secretKey defines the key type for
// storing a Secret type in a context.
secretKey
// stageKey defines the key type for
// storing a Stage type in a context.
stageKey
// containerKey defines the key type for
// storing a Step type in a context.
containerKey
)
// BuildFromContext retrieves the Build type from the context.
func BuildFromContext(c context.Context) *Build {
// get build value from context
v := c.Value(buildKey)
if v == nil {
return nil
}
// cast build value to expected Build type
b, ok := v.(*Build)
if !ok {
return nil
}
return b
}
// BuildWithContext inserts the Build type to the context.
func BuildWithContext(c context.Context, b *Build) context.Context {
return context.WithValue(c, buildKey, b)
}
// SecretFromContext retrieves the Secret type from the context.
func SecretFromContext(c context.Context) *Secret {
// get secret value from context
v := c.Value(secretKey)
if v == nil {
return nil
}
// cast secret value to expected Secret type
s, ok := v.(*Secret)
if !ok {
return nil
}
return s
}
// SecretWithContext inserts the Secret type to the context.
func SecretWithContext(c context.Context, s *Secret) context.Context {
return context.WithValue(c, secretKey, s)
}
// StageFromContext retrieves the Stage type from the context.
func StageFromContext(c context.Context) *Stage {
// get stage value from context
v := c.Value(stageKey)
if v == nil {
return nil
}
// cast stage value to expected Stage type
s, ok := v.(*Stage)
if !ok {
return nil
}
return s
}
// StageWithContext inserts the Stage type to the context.
func StageWithContext(c context.Context, s *Stage) context.Context {
return context.WithValue(c, stageKey, s)
}
// ContainerFromContext retrieves the container type from the context.
func ContainerFromContext(c context.Context) *Container {
// get container value from context
v := c.Value(containerKey)
if v == nil {
return nil
}
// cast step value to expected Container type
s, ok := v.(*Container)
if !ok {
return nil
}
return s
}
// ContainerWithContext inserts the Container type to the context.
func ContainerWithContext(c context.Context, s *Container) context.Context {
return context.WithValue(c, containerKey, s)
}