-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (117 loc) · 5.14 KB
/
Program.cs
File metadata and controls
137 lines (117 loc) · 5.14 KB
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
129
130
131
132
133
134
135
136
137
using System;
using System.Threading;
using LaunchDarkly.Sdk;
using LaunchDarkly.Sdk.Client;
using LaunchDarkly.Sdk.Client.Interfaces;
namespace DotNetConsoleApp
{
public class Program
{
// Set mobileKey to your LaunchDarkly mobile key.
const string mobileKey = "";
// Set flagKey to the feature flag key you want to evaluate.
const string flagKey = "sample-feature";
static void Main(string[] args)
{
var resolvedMobileKey = GetMobileKey();
var resolvedFlagKey = GetFlagKey();
var isCi = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI"));
var client = LdClient.Init(
Configuration.Default(resolvedMobileKey, ConfigurationBuilder.AutoEnvAttributes.Enabled),
MakeContext(),
TimeSpan.FromSeconds(10)
);
if (client.Initialized)
{
ShowMessage("SDK successfully initialized!");
}
else
{
ShowMessage("SDK failed to initialize. Please check your internet connection and SDK credential for any typo.");
Environment.Exit(1);
}
var flagValue = client.BoolVariation(resolvedFlagKey, false);
ShowMessage(string.Format("The {0} feature flag evaluates to {1}.", resolvedFlagKey, flagValue.ToString().ToLowerInvariant()));
if (flagValue)
{
ShowAsciiArt();
}
if (!isCi)
{
client.FlagTracker.FlagValueChanged += (sender, eventArgs) =>
{
if (eventArgs.Key == resolvedFlagKey)
{
flagValue = client.BoolVariation(resolvedFlagKey, false);
ShowMessage(string.Format("The {0} feature flag evaluates to {1}.", resolvedFlagKey, flagValue.ToString().ToLowerInvariant()));
if (flagValue)
{
ShowAsciiArt();
}
}
};
Thread.Sleep(Timeout.Infinite);
}
// Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
// events to LaunchDarkly before the program exits. If analytics events are not delivered,
// the context properties and flag usage statistics will not appear on your dashboard. In
// a normal long-running application, the SDK would continue running and events would be
// delivered automatically in the background.
client.Dispose();
}
static string GetMobileKey()
{
if (!string.IsNullOrEmpty(mobileKey))
{
return mobileKey;
}
var envKey = Environment.GetEnvironmentVariable("LAUNCHDARKLY_MOBILE_KEY");
if (!string.IsNullOrEmpty(envKey))
{
return envKey;
}
ShowMessage("LaunchDarkly mobile key is required: set the mobileKey variable in Program.cs, or the LAUNCHDARKLY_MOBILE_KEY environment variable and try again.");
Environment.Exit(1);
return ""; // unreachable
}
static string GetFlagKey()
{
var envKey = Environment.GetEnvironmentVariable("LAUNCHDARKLY_FLAG_KEY");
if (!string.IsNullOrEmpty(envKey))
{
return envKey;
}
if (!string.IsNullOrEmpty(flagKey))
{
return flagKey;
}
ShowMessage("LaunchDarkly flag key is required: set the flagKey variable in Program.cs, or the LAUNCHDARKLY_FLAG_KEY environment variable and try again.");
Environment.Exit(1);
return ""; // unreachable
}
// Set up the evaluation context. This context should appear on your
// LaunchDarkly contexts dashboard soon after you run the demo.
static Context MakeContext() =>
Context.Builder("example-user-key")
.Name("Sandy")
.Build();
static void ShowMessage(string s)
{
Console.WriteLine("*** " + s);
Console.WriteLine();
}
static void ShowAsciiArt()
{
Console.WriteLine(" \u2588\u2588 ");
Console.WriteLine(" \u2588\u2588 ");
Console.WriteLine(" \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ");
Console.WriteLine(" \u2588\u2588\u2588\u2588\u2588\u2588\u2588 ");
Console.WriteLine("\u2588\u2588 LAUNCHDARKLY \u2588");
Console.WriteLine(" \u2588\u2588\u2588\u2588\u2588\u2588\u2588 ");
Console.WriteLine(" \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ");
Console.WriteLine(" \u2588\u2588 ");
Console.WriteLine(" \u2588\u2588 ");
Console.WriteLine();
}
}
}