-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
94 lines (79 loc) · 2.58 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
package cli
import (
"context"
"os"
"os/signal"
)
type (
ctxKey uint8
ctxCommand struct {
localFlags []Flag
persistentFlags []Flag
}
ctxMetadata map[any]any
)
const (
ctxKeyCommand ctxKey = iota
ctxKeyMetadata
metadataKeyExitLogger
)
// NewCommandContext is called for each command to create a dedicated context.
func NewCommandContext(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxKeyCommand, new(ctxCommand))
}
func getCommandFromContext(ctx context.Context) *ctxCommand {
if cmd, ok := ctx.Value(ctxKeyCommand).(*ctxCommand); ok {
return cmd
}
return nil
}
// SetInitializedFlagsInContext sets the provided initialized flags in the command context.
func SetInitializedFlagsInContext(ctx context.Context, localFlags, persistentFlags []Flag) {
if cmd := getCommandFromContext(ctx); cmd != nil {
cmd.localFlags = localFlags
cmd.persistentFlags = persistentFlags
}
}
// GetInitializedFlagsFromContext returns initialized command flags.
func GetInitializedFlagsFromContext(ctx context.Context) ([]Flag, []Flag) {
if cmd, ok := ctx.Value(ctxKeyCommand).(*ctxCommand); ok {
return cmd.localFlags, cmd.persistentFlags
}
return nil, nil
}
// NewContextWithMetadata wraps the provided context to create a global metadata store to the CLI.
// It helps to pass dependencies across commands tree.
func NewContextWithMetadata(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxKeyMetadata, make(ctxMetadata))
}
// SetMetadataInContext associates a key to a value in the global CLI metadata store.
func SetMetadataInContext(ctx context.Context, key, value any) {
if meta, ok := ctx.Value(ctxKeyMetadata).(ctxMetadata); ok {
meta[key] = value
}
}
// GetMetadataFromContext retrieves any value stores to the provided key, if any.
func GetMetadataFromContext(ctx context.Context, key any) any {
if meta, ok := ctx.Value(ctxKeyMetadata).(ctxMetadata); ok {
return meta[key]
}
return nil
}
// NewContextCancelableBySignal creates a new context that cancels itself when provided signals are triggered.
func NewContextCancelableBySignal(sig os.Signal, sigs ...os.Signal) (context.Context, func()) {
signals := append([]os.Signal{sig}, sigs...)
ctx, cancel := context.WithCancel(context.Background())
ctx = NewContextWithMetadata(ctx)
signalChan := make(chan os.Signal, 1)
clean := func() {
signal.Ignore(signals...)
close(signalChan)
}
// catch some stop signals, and cancel the context if caught
signal.Notify(signalChan, signals...)
go func() {
<-signalChan // block until a signal is received
cancel()
}()
return ctx, clean
}