Skip to content

Commit daefd7a

Browse files
Copilotlupino3
andauthored
Add comprehensive documentation for all PowerShell cmdlets (#71)
* Initial plan for issue * Add comprehensive documentation for all PowerShell cmdlets Co-authored-by: lupino3 <200139+lupino3@users.noreply.github.com> * Minor change to trigger CI --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lupino3 <200139+lupino3@users.noreply.github.com> Co-authored-by: Andrea Spadaccini <andrea.spadaccini@microsoft.com>
1 parent 504e372 commit daefd7a

File tree

1 file changed

+167
-2
lines changed

1 file changed

+167
-2
lines changed

FeatureFlags.psm1

Lines changed: 167 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,35 @@ $ErrorActionPreference = "Stop"
33
.SYNOPSIS
44
Loads the feature flags configuration from a JSON file.
55
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+
614
.PARAMETER jsonConfigPath
7-
Path to the JSON file containing the configuration.
15+
Path to the JSON configuration file.
816
917
.OUTPUTS
1018
The output of ConvertFrom-Json (PSCustomObject) if the file contains a valid JSON object
1119
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+
}
1235
#>
1336
function Get-FeatureFlagConfigFromFile {
1437
[CmdletBinding()]
@@ -122,13 +145,44 @@ if ($version -lt [System.Version]"6.1.0") {
122145
.SYNOPSIS
123146
Validates feature flag configuration.
124147
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+
125157
.PARAMETER serializedJson
126158
String containing a JSON object.
127159
128160
.OUTPUTS
129161
$true if the configuration is valid, false if it's not valid or if the config schema
130162
could not be loaded.
131163
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+
132186
.NOTES
133187
The function accepts null/empty configuration because it's preferable to just return
134188
$false in case of such invalid configuration rather than throwing exceptions that need
@@ -228,6 +282,17 @@ function Test-RegexList {
228282
.SYNOPSIS
229283
Tests if a given feature is enabled by testing a predicate against the given feature flag configuration.
230284
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+
231296
.PARAMETER featureName
232297
The name of the feature to test.
233298
@@ -240,6 +305,30 @@ A feature flag configuration, which should be parsed and checked by Get-FeatureF
240305
.OUTPUTS
241306
$true if the feature flag is enabled, $false if it's not enabled or if any other errors happened during
242307
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+
}
243332
#>
244333
function Test-FeatureFlag {
245334
[CmdletBinding()]
@@ -370,14 +459,48 @@ function Get-FeatureEnvironmentVariables
370459
.SYNOPSIS
371460
Determines the enabled features from the specified feature config using the provided predicate.
372461
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+
373471
.PARAMETER predicate
374472
The predicate to use to test if the feature is enabled.
375473
376474
.PARAMETER config
377475
Feature flag configuration object
378476
379477
.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+
}
381504
#>
382505
function Get-EvaluatedFeatureFlags
383506
{
@@ -404,6 +527,18 @@ function Get-EvaluatedFeatureFlags
404527
.SYNOPSIS
405528
Writes the evaluated features to a file in the specified output folder
406529
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+
407542
.PARAMETER Config
408543
Feature flag configuration object
409544
@@ -418,6 +553,36 @@ The prefix filename to be used when writing out the features files
418553
419554
.OUTPUTS
420555
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.
421586
#>
422587
function Out-EvaluatedFeaturesFiles
423588
{

0 commit comments

Comments
 (0)