-
Notifications
You must be signed in to change notification settings - Fork 15
/
example.go
56 lines (46 loc) · 1.22 KB
/
example.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
package main
import (
"log"
"os"
harness "github.com/harness/ff-golang-server-sdk/client"
"github.com/harness/ff-golang-server-sdk/evaluation"
)
var (
sdkKey string = getEnvOrDefault("FF_API_KEY", "changeme")
client *harness.CfClient
)
func main() {
log.Println("Harness SDK Getting Started")
// Create a feature flag client
var err error
client, err = harness.NewCfClient(sdkKey)
if err != nil {
log.Fatalf("could not connect to CF servers %s\n", err)
}
defer func() { client.Close() }()
if isEnabled("harnessappdemodarkmode") {
log.Println("Run true code path")
} else {
log.Println("Run false code path")
}
}
func isEnabled(flag string) bool {
// Create a target (different targets can get different results based on rules)
target := evaluation.Target{
Identifier: "HT_1",
Name: "Harness_Target_1",
Attributes: &map[string]interface{}{"email": "demo@harness.io"},
}
resultBool, err := client.BoolVariation(flag, &target, false)
if err != nil {
log.Printf("issue evaluating flag %s, serving default value of false. err: %s", flag, err)
}
return resultBool
}
func getEnvOrDefault(key, defaultStr string) string {
value := os.Getenv(key)
if value == "" {
return defaultStr
}
return value
}