This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
128 lines (105 loc) · 3.28 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
118
119
120
121
122
123
124
125
126
127
128
package chat
import (
"context"
"github.com/keybase/client/go/chat/globals"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
type appTypeSource interface {
GetAppType() libkb.AppType
}
type keyfinderKey int
type identifyNotifierKey int
type chatTrace int
type identifyModeKey int
var kfKey keyfinderKey
var inKey identifyNotifierKey
var chatTraceKey chatTrace
var identModeKey identifyModeKey
type identModeData struct {
mode keybase1.TLFIdentifyBehavior
breaks *[]keybase1.TLFIdentifyFailure
}
func IdentifyModeCtx(ctx context.Context, mode keybase1.TLFIdentifyBehavior,
breaks *[]keybase1.TLFIdentifyFailure) context.Context {
return context.WithValue(ctx, identModeKey, identModeData{mode: mode, breaks: breaks})
}
func IdentifyMode(ctx context.Context) (ib keybase1.TLFIdentifyBehavior, breaks *[]keybase1.TLFIdentifyFailure, ok bool) {
var imd identModeData
val := ctx.Value(identModeKey)
if imd, ok = val.(identModeData); ok {
return imd.mode, imd.breaks, ok
}
return keybase1.TLFIdentifyBehavior_CHAT_CLI, nil, false
}
func CtxKeyFinder(ctx context.Context, g *globals.Context) KeyFinder {
var kf KeyFinder
var ok bool
val := ctx.Value(kfKey)
if kf, ok = val.(KeyFinder); ok {
return kf
}
return NewKeyFinder(g)
}
func CtxIdentifyNotifier(ctx context.Context) *IdentifyNotifier {
var in *IdentifyNotifier
var ok bool
val := ctx.Value(inKey)
if in, ok = val.(*IdentifyNotifier); ok {
return in
}
return nil
}
func CtxTrace(ctx context.Context) (string, bool) {
var trace string
var ok bool
val := ctx.Value(chatTraceKey)
if trace, ok = val.(string); ok {
return trace, true
}
return "", false
}
func CtxAddLogTags(ctx context.Context, env appTypeSource) context.Context {
// Add trace context value
ctx = context.WithValue(ctx, chatTraceKey, libkb.RandStringB64(3))
// Add log tags
tags := make(map[interface{}]string)
tags[chatTraceKey] = "chat-trace"
ctx = logger.NewContextWithLogTags(ctx, tags)
rpcTags := make(map[string]interface{})
rpcTags["user-agent"] = libkb.UserAgent
rpcTags["platform"] = libkb.GetPlatformString()
rpcTags["apptype"] = env.GetAppType()
ctx = rpc.AddRpcTagsToContext(ctx, rpcTags)
return ctx
}
func Context(ctx context.Context, g *globals.Context, mode keybase1.TLFIdentifyBehavior,
breaks *[]keybase1.TLFIdentifyFailure, notifier *IdentifyNotifier) context.Context {
if breaks == nil {
breaks = new([]keybase1.TLFIdentifyFailure)
}
res := IdentifyModeCtx(ctx, mode, breaks)
val := res.Value(kfKey)
if _, ok := val.(KeyFinder); !ok {
res = context.WithValue(res, kfKey, NewKeyFinder(g))
}
res = context.WithValue(res, inKey, notifier)
res = CtxAddLogTags(res, g.GetEnv())
return res
}
func BackgroundContext(sourceCtx context.Context, g *globals.Context) context.Context {
rctx := context.Background()
in := CtxIdentifyNotifier(sourceCtx)
if ident, breaks, ok := IdentifyMode(sourceCtx); ok {
rctx = Context(rctx, g, ident, breaks, in)
}
// Overwrite trace tag
if tr, ok := sourceCtx.Value(chatTraceKey).(string); ok {
rctx = context.WithValue(rctx, chatTraceKey, tr)
}
rctx = context.WithValue(rctx, kfKey, CtxKeyFinder(sourceCtx, g))
rctx = context.WithValue(rctx, inKey, in)
return rctx
}