-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext.go
97 lines (77 loc) · 2.64 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
package gitdir
import (
"context"
"github.com/gliderlabs/ssh"
"github.com/go-git/go-billy/v5/memfs"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/belak/go-gitdir/models"
)
type contextKey string
func (c contextKey) String() string {
return "Context key: " + string(c)
}
const (
contextKeyConfig = contextKey("gitdir-config")
contextKeyUser = contextKey("gitdir-user")
contextKeyLogger = contextKey("gitdir-logger")
contextKeyPublicKey = contextKey("gitdir-public-key")
)
// CtxExtract is a convenience wrapper around the other context convenience
// methods to pull out everything you'd want from a request.
func CtxExtract(ctx context.Context) (*zerolog.Logger, *Config, *User) {
return CtxLogger(ctx), CtxConfig(ctx), CtxUser(ctx)
}
// CtxSetConfig puts the given Config into the ssh.Context.
func CtxSetConfig(parent ssh.Context, config *Config) {
parent.SetValue(contextKeyConfig, config)
}
// CtxConfig pulls the current Config out of the context, or a blank Config if
// not set.
func CtxConfig(ctx context.Context) *Config {
if c, ok := ctx.Value(contextKeyConfig).(*Config); ok {
return c
}
// If it doesn't exist, return a new empty config for safety.
return NewConfig(memfs.New())
}
// CtxSetUser puts the given User into the ssh.Context.
func CtxSetUser(parent ssh.Context, user *User) {
parent.SetValue(contextKeyUser, user)
}
// CtxUser pulls the current User out of the context, or AnonymousUser if not
// set.
func CtxUser(ctx context.Context) *User {
if u, ok := ctx.Value(contextKeyUser).(*User); ok {
return u
}
return AnonymousUser
}
// WithLogger takes a parent context and a logger and returns a new context
// with that logger.
func WithLogger(parent context.Context, logger *zerolog.Logger) context.Context {
return context.WithValue(parent, contextKeyLogger, logger)
}
// CtxSetLogger puts the given logger into the ssh.Context.
func CtxSetLogger(parent ssh.Context, logger *zerolog.Logger) {
parent.SetValue(contextKeyLogger, logger)
}
// CtxLogger pulls the logger out of the context, or the default logger if not
// found.
func CtxLogger(ctx context.Context) *zerolog.Logger {
if ctxLog, ok := ctx.Value(contextKeyLogger).(*zerolog.Logger); ok {
return ctxLog
}
return &log.Logger
}
// CtxSetPublicKey puts the given public key into the ssh.Context.
func CtxSetPublicKey(parent ssh.Context, pk *models.PublicKey) {
parent.SetValue(contextKeyPublicKey, pk)
}
// CtxPublicKey pulls the public key out of the context, or nil if not found.
func CtxPublicKey(ctx context.Context) *models.PublicKey {
if pk, ok := ctx.Value(contextKeyPublicKey).(*models.PublicKey); ok {
return pk
}
return nil
}