-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
79 lines (63 loc) · 2.03 KB
/
main.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
package main
import (
"fmt"
"os"
"time"
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
ld "github.com/launchdarkly/go-server-sdk/v7"
)
func showBanner() {
fmt.Print("\n ██ \n" +
" ██ \n" +
" ████████ \n" +
" ███████ \n" +
"██ LAUNCHDARKLY █\n" +
" ███████ \n" +
" ████████ \n" +
" ██ \n" +
" ██ \n")
}
func showMessage(s string) { fmt.Printf("*** %s\n\n", s) }
func main() {
var sdkKey = os.Getenv("LAUNCHDARKLY_SDK_KEY")
if sdkKey == "" {
showMessage("LaunchDarkly SDK key is required: set the LAUNCHDARKLY_SDK_KEY environment variable and try again.")
os.Exit(1)
}
ldClient, _ := ld.MakeClient(sdkKey, 5*time.Second)
if ldClient.Initialized() {
showMessage("SDK successfully initialized!")
} else {
showMessage("SDK failed to initialize")
os.Exit(1)
}
// Set up the evaluation context. This context should appear on your LaunchDarkly contexts dashboard
// soon after you run the demo.
context := ldcontext.NewBuilder("example-user-key").
Name("Sandy").
Build()
// Set featureFlagKey to the feature flag key you want to evaluate.
var featureFlagKey = "sample-feature"
if os.Getenv("LAUNCHDARKLY_FLAG_KEY") != "" {
featureFlagKey = os.Getenv("LAUNCHDARKLY_FLAG_KEY")
}
flagValue, err := ldClient.BoolVariation(featureFlagKey, context, false)
if err != nil {
showMessage("error: " + err.Error())
}
showMessage(fmt.Sprintf("The '%s' feature flag evaluates to %t.", featureFlagKey, flagValue))
if flagValue {
showBanner()
}
if os.Getenv("CI") != "" {
os.Exit(0)
}
updateCh := ldClient.GetFlagTracker().AddFlagValueChangeListener(featureFlagKey, context, ldvalue.Null())
for event := range updateCh {
showMessage(fmt.Sprintf("The '%s' feature flag evaluates to %t.", featureFlagKey, event.NewValue.BoolValue()))
if event.NewValue.BoolValue() {
showBanner()
}
}
}