@@ -3,12 +3,35 @@ $ErrorActionPreference = "Stop"
3
3
. SYNOPSIS
4
4
Loads the feature flags configuration from a JSON file.
5
5
6
+ . DESCRIPTION
7
+ This cmdlet reads a JSON file containing feature flag configuration and validates it against
8
+ the feature flags schema. If the configuration is valid, it returns a PowerShell object
9
+ representation of the JSON. If the file is invalid or doesn't exist, it returns $null.
10
+
11
+ The JSON file should contain two main sections: "stages" for defining rollout stages with
12
+ conditions, and "features" for associating features with stages and environment variables.
13
+
6
14
. PARAMETER jsonConfigPath
7
- Path to the JSON file containing the configuration.
15
+ Path to the JSON configuration file .
8
16
9
17
. OUTPUTS
10
18
The output of ConvertFrom-Json (PSCustomObject) if the file contains a valid JSON object
11
19
that matches the feature flags JSON schema, $null otherwise.
20
+
21
+ . EXAMPLE
22
+ $config = Get-FeatureFlagConfigFromFile -jsonConfigPath ".\features.json"
23
+ if ($config) {
24
+ Write-Host "Configuration loaded successfully"
25
+ } else {
26
+ Write-Host "Failed to load configuration"
27
+ }
28
+
29
+ . EXAMPLE
30
+ # Load configuration and check available features
31
+ $config = Get-FeatureFlagConfigFromFile "C:\config\feature-flags.json"
32
+ if ($config -and $config.features) {
33
+ $config.features | Get-Member -MemberType NoteProperty | ForEach-Object { $_.Name }
34
+ }
12
35
#>
13
36
function Get-FeatureFlagConfigFromFile {
14
37
[CmdletBinding ()]
@@ -122,13 +145,44 @@ if ($version -lt [System.Version]"6.1.0") {
122
145
. SYNOPSIS
123
146
Validates feature flag configuration.
124
147
148
+ . DESCRIPTION
149
+ This cmdlet validates a feature flag configuration JSON string against the feature flags
150
+ schema. It performs both JSON schema validation and additional business logic validation,
151
+ such as ensuring that all features reference defined stages.
152
+
153
+ The validation includes checking that the JSON structure matches the expected schema for
154
+ stages and features, and that all stage references in features actually exist in the
155
+ stages section.
156
+
125
157
. PARAMETER serializedJson
126
158
String containing a JSON object.
127
159
128
160
. OUTPUTS
129
161
$true if the configuration is valid, false if it's not valid or if the config schema
130
162
could not be loaded.
131
163
164
+ . EXAMPLE
165
+ $jsonConfig = Get-Content "features.json" -Raw
166
+ if (Confirm-FeatureFlagConfig -serializedJson $jsonConfig) {
167
+ Write-Host "Configuration is valid"
168
+ } else {
169
+ Write-Host "Configuration validation failed"
170
+ }
171
+
172
+ . EXAMPLE
173
+ # Validate a simple configuration
174
+ $simpleConfig = @"
175
+ {
176
+ "stages": {
177
+ "test": [{"allowlist": ["test.*"]}]
178
+ },
179
+ "features": {
180
+ "new-feature": {"stages": ["test"]}
181
+ }
182
+ }
183
+ "@
184
+ Confirm-FeatureFlagConfig -serializedJson $simpleConfig
185
+
132
186
. NOTES
133
187
The function accepts null/empty configuration because it's preferable to just return
134
188
$false in case of such invalid configuration rather than throwing exceptions that need
@@ -228,6 +282,17 @@ function Test-RegexList {
228
282
. SYNOPSIS
229
283
Tests if a given feature is enabled by testing a predicate against the given feature flag configuration.
230
284
285
+ . DESCRIPTION
286
+ This cmdlet evaluates whether a specific feature should be enabled for a given predicate by
287
+ checking the feature's associated stages and their conditions. The predicate is typically
288
+ an identifier (like a machine name, user ID, or environment name) that gets tested against
289
+ the stage conditions.
290
+
291
+ Each feature can be associated with multiple stages, and the feature is considered enabled
292
+ if ANY of its stages evaluate to true. Stages contain conditions (allowlist, denylist,
293
+ probability) that are evaluated in order, and ALL conditions in a stage must be satisfied
294
+ for that stage to be considered active.
295
+
231
296
. PARAMETER featureName
232
297
The name of the feature to test.
233
298
@@ -240,6 +305,30 @@ A feature flag configuration, which should be parsed and checked by Get-FeatureF
240
305
. OUTPUTS
241
306
$true if the feature flag is enabled, $false if it's not enabled or if any other errors happened during
242
307
the verification.
308
+
309
+ . EXAMPLE
310
+ $config = Get-FeatureFlagConfigFromFile -jsonConfigPath "features.json"
311
+ $isEnabled = Test-FeatureFlag -featureName "new-ui" -predicate "prod-server1" -config $config
312
+ if ($isEnabled) {
313
+ Write-Host "New UI feature is enabled for prod-server1"
314
+ }
315
+
316
+ . EXAMPLE
317
+ # Test multiple predicates for a feature
318
+ $config = Get-FeatureFlagConfigFromFile "features.json"
319
+ $predicates = @("test-env", "dev-machine", "prod-canary")
320
+ foreach ($predicate in $predicates) {
321
+ $result = Test-FeatureFlag -featureName "experimental-feature" -predicate $predicate -config $config
322
+ Write-Host "${predicate}: $result"
323
+ }
324
+
325
+ . EXAMPLE
326
+ # Test feature enablement and set environment variables accordingly
327
+ $config = Get-FeatureFlagConfigFromFile "features.json"
328
+ if (Test-FeatureFlag -featureName "new-cache" -predicate $env:COMPUTERNAME -config $config) {
329
+ $env:USE_NEW_CACHE = "1"
330
+ Write-Host "New cache feature enabled"
331
+ }
243
332
#>
244
333
function Test-FeatureFlag {
245
334
[CmdletBinding ()]
@@ -370,14 +459,48 @@ function Get-FeatureEnvironmentVariables
370
459
. SYNOPSIS
371
460
Determines the enabled features from the specified feature config using the provided predicate.
372
461
462
+ . DESCRIPTION
463
+ This cmdlet evaluates all features defined in the configuration against a given predicate
464
+ and returns a hashtable showing which features are enabled or disabled. This is useful
465
+ for getting a complete picture of feature enablement for a specific context (like a
466
+ particular server, user, or environment).
467
+
468
+ The returned hashtable contains feature names as keys and boolean values indicating
469
+ whether each feature is enabled (True) or disabled (False) for the given predicate.
470
+
373
471
. PARAMETER predicate
374
472
The predicate to use to test if the feature is enabled.
375
473
376
474
. PARAMETER config
377
475
Feature flag configuration object
378
476
379
477
. OUTPUTS
380
- Returns an array of the evaluated feature flags given the specified predicate.
478
+ Returns a hashtable of the evaluated feature flags given the specified predicate.
479
+
480
+ . EXAMPLE
481
+ $config = Get-FeatureFlagConfigFromFile -jsonConfigPath "features.json"
482
+ $results = Get-EvaluatedFeatureFlags -predicate "prod-server1" -config $config
483
+ $results.GetEnumerator() | ForEach-Object {
484
+ Write-Host "Feature '$($_.Key)': $($_.Value)"
485
+ }
486
+
487
+ . EXAMPLE
488
+ # Get enabled features for current machine
489
+ $config = Get-FeatureFlagConfigFromFile "features.json"
490
+ $enabledFeatures = Get-EvaluatedFeatureFlags -predicate $env:COMPUTERNAME -config $config
491
+ $enabledFeatures.GetEnumerator() | Where-Object { $_.Value -eq $true } | ForEach-Object {
492
+ Write-Host "Enabled: $($_.Key)"
493
+ }
494
+
495
+ . EXAMPLE
496
+ # Compare feature enablement across environments
497
+ $config = Get-FeatureFlagConfigFromFile "features.json"
498
+ $environments = @("test-env", "staging-env", "prod-env")
499
+ foreach ($env in $environments) {
500
+ Write-Host "Environment: $env"
501
+ $features = Get-EvaluatedFeatureFlags -predicate $env -config $config
502
+ $features.GetEnumerator() | ForEach-Object { Write-Host " $($_.Key): $($_.Value)" }
503
+ }
381
504
#>
382
505
function Get-EvaluatedFeatureFlags
383
506
{
@@ -404,6 +527,18 @@ function Get-EvaluatedFeatureFlags
404
527
. SYNOPSIS
405
528
Writes the evaluated features to a file in the specified output folder
406
529
530
+ . DESCRIPTION
531
+ This cmdlet takes a collection of evaluated feature flags and writes them to multiple file
532
+ formats in the specified output folder. It creates three types of files:
533
+
534
+ 1. JSON file (.json) - Contains the feature flags in JSON format
535
+ 2. INI file (.ini) - Contains the feature flags in INI/key-value format
536
+ 3. Environment config file (.env.config) - Contains environment variables for enabled features
537
+
538
+ The environment config file only includes features that are enabled and have associated
539
+ environment variables defined in the configuration. This is useful for setting up
540
+ environment-specific configurations in deployment scenarios.
541
+
407
542
. PARAMETER Config
408
543
Feature flag configuration object
409
544
@@ -418,6 +553,36 @@ The prefix filename to be used when writing out the features files
418
553
419
554
. OUTPUTS
420
555
Outputs multiple file formats expressing the evaluated feature flags
556
+
557
+ . EXAMPLE
558
+ $config = Get-FeatureFlagConfigFromFile -jsonConfigPath "features.json"
559
+ $evaluated = Get-EvaluatedFeatureFlags -predicate "prod-server1" -config $config
560
+ Out-EvaluatedFeaturesFiles -Config $config -EvaluatedFeatures $evaluated -OutputFolder "C:\output"
561
+
562
+ # This creates:
563
+ # C:\output\features.json
564
+ # C:\output\features.ini
565
+ # C:\output\features.env.config
566
+
567
+ . EXAMPLE
568
+ # Generate feature files for multiple environments
569
+ $config = Get-FeatureFlagConfigFromFile "features.json"
570
+ $environments = @("dev", "staging", "prod")
571
+ foreach ($env in $environments) {
572
+ $evaluated = Get-EvaluatedFeatureFlags -predicate $env -config $config
573
+ Out-EvaluatedFeaturesFiles -Config $config -EvaluatedFeatures $evaluated -OutputFolder ".\output\$env" -FileName "features-$env"
574
+ }
575
+
576
+ . EXAMPLE
577
+ # Custom filename for output files
578
+ $config = Get-FeatureFlagConfigFromFile "features.json"
579
+ $evaluated = Get-EvaluatedFeatureFlags -predicate $env:COMPUTERNAME -config $config
580
+ Out-EvaluatedFeaturesFiles -Config $config -EvaluatedFeatures $evaluated -OutputFolder ".\deployment" -FileName "machine-features"
581
+
582
+ . NOTES
583
+ The output directory will be created automatically if it doesn't exist. Environment
584
+ variables are only written to the .env.config file for features that are both enabled
585
+ and have environmentVariables defined in the configuration.
421
586
#>
422
587
function Out-EvaluatedFeaturesFiles
423
588
{
0 commit comments