-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
defs.go
68 lines (58 loc) · 1.93 KB
/
defs.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
package gitlab
import (
"context"
"errors"
"net/http"
"time"
)
var (
ErrNilValue = errors.New("nil value")
ErrInvalidValue = errors.New("invalid value")
ErrFieldRequired = errors.New("required field")
ErrFieldInvalidValue = errors.New("invalid value for field")
ErrBackendNotConfigured = errors.New("backend not configured")
)
type contextKey string
const (
DefaultConfigFieldAccessTokenMaxTTL = 7 * 24 * time.Hour
DefaultConfigFieldAccessTokenRotate = DefaultAutoRotateBeforeMinTTL
DefaultRoleFieldAccessTokenMaxTTL = 24 * time.Hour
DefaultAccessTokenMinTTL = 24 * time.Hour
DefaultAccessTokenMaxPossibleTTL = 365 * 24 * time.Hour
DefaultAutoRotateBeforeMinTTL = 24 * time.Hour
DefaultAutoRotateBeforeMaxTTL = 730 * time.Hour
ctxKeyHttpClient = contextKey("vpsg-ctx-key-http-client")
ctxKeyGitlabClient = contextKey("vpsg-ctx-key-gitlab-client")
ctxKeyTimeNow = contextKey("vpsg-ctx-key-time-now")
DefaultConfigName = "default"
)
func WithStaticTime(ctx context.Context, t time.Time) context.Context {
return context.WithValue(ctx, ctxKeyTimeNow, t)
}
func TimeFromContext(ctx context.Context) time.Time {
t, ok := ctx.Value(ctxKeyTimeNow).(time.Time)
if !ok {
return time.Now()
}
return t
}
func HttpClientNewContext(ctx context.Context, httpClient *http.Client) context.Context {
return context.WithValue(ctx, ctxKeyHttpClient, httpClient)
}
func HttpClientFromContext(ctx context.Context) (*http.Client, bool) {
u, ok := ctx.Value(ctxKeyHttpClient).(*http.Client)
if !ok {
u = nil
}
return u, ok
}
func GitlabClientNewContext(ctx context.Context, client Client) context.Context {
return context.WithValue(ctx, ctxKeyGitlabClient, client)
}
func GitlabClientFromContext(ctx context.Context) (Client, bool) {
u, ok := ctx.Value(ctxKeyGitlabClient).(Client)
if !ok {
u = nil
}
return u, ok
}