Skip to content

Commit f2c2f2c

Browse files
committed
Fix $null checks
1 parent 07d72ab commit f2c2f2c

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

General/Test-NonInteractive.ps1

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
Function Test-InteractiveSession {
2+
[CmdletBinding()]
3+
[OutputType([bool])]
4+
Param()
5+
6+
Process {
7+
# Check if we're running in a non-interactive environment
8+
if ([Environment]::UserInteractive -and -not ([Environment]::GetCommandLineArgs() -like '-NonInteractive*')) {
9+
# Check for various CI/CD environments
10+
$ciEnvironments = @(
11+
# GitHub Actions
12+
$env:GITHUB_ACTIONS -eq 'true',
13+
14+
# GitLab CI
15+
$env:GITLAB_CI -eq 'true',
16+
17+
# Azure DevOps
18+
$env:TF_BUILD -eq 'true',
19+
20+
# Bitbucket Pipelines
21+
$null -ne $env:BITBUCKET_BUILD_NUMBER,
22+
23+
# Jenkins
24+
$null -ne $env:JENKINS_URL,
25+
26+
# CircleCI
27+
$env:CIRCLECI -eq 'true',
28+
29+
# Travis CI
30+
$env:TRAVIS -eq 'true',
31+
32+
# TeamCity
33+
$null -ne $env:TEAMCITY_VERSION
34+
)
35+
36+
# Check for container environment
37+
$containerEnvironments = @(
38+
# Check for Docker
39+
(Test-Path -Path '/.dockerenv'),
40+
# Check for common container environment variables
41+
$env:KUBERNETES_SERVICE_HOST -ne $null,
42+
# Check for Windows container
43+
$env:CONTAINER -eq 'true'
44+
)
45+
46+
# Return false if any CI/CD or container condition is true
47+
if ($ciEnvironments -contains $true -or $containerEnvironments -contains $true) {
48+
return $false
49+
}
50+
51+
return $true
52+
}
53+
54+
return $false
55+
}
56+
}
57+
58+
Function Invoke-InteractivePrompt {
59+
[CmdletBinding()]
60+
Param(
61+
[Parameter(Mandatory = $true)]
62+
[string]$Message,
63+
64+
[Parameter(Mandatory = $false)]
65+
[string]$DefaultResponse = '',
66+
67+
[Parameter(Mandatory = $false)]
68+
[switch]$ShowEnvironmentInfo
69+
)
70+
71+
Process {
72+
if (Test-InteractiveSession) {
73+
$response = Read-Host -Prompt $Message
74+
if ([string]::IsNullOrEmpty($response)) {
75+
return $DefaultResponse
76+
}
77+
return $response
78+
} else {
79+
$environmentInfo = Get-ExecutionEnvironment
80+
if ($ShowEnvironmentInfo) {
81+
Write-Verbose "Running in $($environmentInfo.Type) environment: $($environmentInfo.Name)"
82+
}
83+
Write-Verbose "Using default response: $DefaultResponse"
84+
return $DefaultResponse
85+
}
86+
}
87+
}
88+
89+
Function Get-ExecutionEnvironment {
90+
[CmdletBinding()]
91+
[OutputType([PSCustomObject])]
92+
Param()
93+
94+
Process {
95+
$environmentInfo = [PSCustomObject]@{
96+
Type = 'Unknown'
97+
Name = 'Unknown'
98+
}
99+
100+
# Check for CI/CD environments
101+
if ($env:GITHUB_ACTIONS -eq 'true') {
102+
$environmentInfo.Type = 'CI/CD'
103+
$environmentInfo.Name = 'GitHub Actions'
104+
} elseif ($env:GITLAB_CI -eq 'true') {
105+
$environmentInfo.Type = 'CI/CD'
106+
$environmentInfo.Name = 'GitLab CI'
107+
} elseif ($env:TF_BUILD -eq 'true') {
108+
$environmentInfo.Type = 'CI/CD'
109+
$environmentInfo.Name = 'Azure DevOps'
110+
} elseif ($null -ne $env:BITBUCKET_BUILD_NUMBER) {
111+
$environmentInfo.Type = 'CI/CD'
112+
$environmentInfo.Name = 'Bitbucket Pipelines'
113+
} elseif ($null -ne $env:JENKINS_URL) {
114+
$environmentInfo.Type = 'CI/CD'
115+
$environmentInfo.Name = 'Jenkins'
116+
} elseif ($env:CIRCLECI -eq 'true') {
117+
$environmentInfo.Type = 'CI/CD'
118+
$environmentInfo.Name = 'CircleCI'
119+
} elseif ($env:TRAVIS -eq 'true') {
120+
$environmentInfo.Type = 'CI/CD'
121+
$environmentInfo.Name = 'Travis CI'
122+
} elseif ($null -ne $env:TEAMCITY_VERSION) {
123+
$environmentInfo.Type = 'CI/CD'
124+
$environmentInfo.Name = 'TeamCity'
125+
}
126+
# Check for container environments
127+
elseif (Test-Path -Path '/.dockerenv') {
128+
$environmentInfo.Type = 'Container'
129+
$environmentInfo.Name = 'Docker'
130+
} elseif ($null -ne $env:KUBERNETES_SERVICE_HOST) {
131+
$environmentInfo.Type = 'Container'
132+
$environmentInfo.Name = 'Kubernetes'
133+
} elseif ($env:CONTAINER -eq 'true') {
134+
$environmentInfo.Type = 'Container'
135+
$environmentInfo.Name = 'Windows Container'
136+
} elseif (-not [Environment]::UserInteractive -or [Environment]::GetCommandLineArgs() -like '-NonInteractive*') {
137+
$environmentInfo.Type = 'Non-interactive'
138+
$environmentInfo.Name = 'PowerShell'
139+
} else {
140+
$environmentInfo.Type = 'Interactive'
141+
$environmentInfo.Name = 'PowerShell'
142+
}
143+
144+
return $environmentInfo
145+
}
146+
}
147+
148+
# Example usage
149+
$answer = Invoke-InteractivePrompt -Message 'Do you want to continue? (Y/N)' -DefaultResponse 'Y' -ShowEnvironmentInfo -Verbose
150+
Write-Host "You chose: $answer"

0 commit comments

Comments
 (0)