forked from jkroepke/helm-secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsh.ps1
136 lines (112 loc) · 3.86 KB
/
sh.ps1
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
trap { "[helm-secrets] powershell errored in line $($_.InvocationInfo.ScriptLineNumber): $($_.InvocationInfo.Line)"; "[helm-secrets] Error: $_"; exit 1 }
function which([string] $cmd) {
(Get-Command -ErrorAction "SilentlyContinue" $cmd).Path
}
function runShell(
[string][Parameter(Mandatory, Position=0)] $shell,
[System.Object[]][Parameter(Mandatory, Position=1)] $args
) {
$quotedArgs = foreach ($arg in $args) {
if ($arg -notmatch '[ "]') { $arg }
else { # must double-quote
'"{0}"' -f ($arg -replace '"', '\"' -replace '\\$', '\\')
}
}
$quotedArg = $quotedArgs -join " "
echo $args
echo $quotedArgs
echo $quotedArg
& $shell $quotedArg
exit $LASTEXITCODE
}
function prepareWsl(
[System.Object[]][Parameter(Mandatory, Position=0)] $args
) {
if ($null -eq $env:HELM_BIN -and $null -eq $env:HELM_SECRETS_HELM_PATH) {
if ((which helm.exe) -ne $null) {
$env:HELM_SECRETS_HELM_PATH = "helm.exe"
}
}
if ($null -eq $env:HELM_SECRETS_SOPS_PATH) {
if ((which sops.exe) -ne $null) {
$env:HELM_SECRETS_SOPS_PATH = "sops.exe"
}
}
if ($null -eq $env:HELM_SECRETS_VALS_PATH) {
if ((which vals.exe) -ne $null) {
$env:HELM_SECRETS_VALS_PATH = "vals.exe"
}
}
if ($null -eq $env:HELM_SECRETS_CURL_PATH) {
if ((which curl.exe) -ne $null) {
$env:HELM_SECRETS_CURL_PATH = "curl.exe"
}
}
$env:WSLENV += ':SOPS_AGE_KEY:SOPS_AGE_KEY_FILE'
$env:WSLENV += ':HELM_SECRETS_DEC_SUFFIX'
$env:WSLENV += ':HELM_SECRETS_QUIET'
$env:WSLENV += ':HELM_SECRETS_BACKEND'
$env:WSLENV += ':HELM_SECRETS_BACKEND_ARGS'
$env:WSLENV += ':HELM_SECRETS_ALLOWED_BACKENDS'
$env:WSLENV += ':HELM_SECRETS_DEC_DIR'
$env:WSLENV += ':HELM_SECRETS_URL_VARIABLE_EXPANSION'
$env:WSLENV += ':HELM_DEBUG'
$env:WSLENV += ':HELM_SECRETS_EVALUATE_TEMPLATES'
$env:WSLENV += ':HELM_SECRETS_EVALUATE_TEMPLATES_DECODE_SECRETS'
$env:WSLENV += ':HELM_BIN'
$env:WSLENV += if ($env:HELM_BIN -match "\\") {"/p"}
$env:WSLENV += ':HELM_PLUGIN_DIR'
$env:WSLENV += if ($env:HELM_PLUGIN_DIR -match "\\") {"/p"}
$env:WSLENV += ':HELM_SECRETS_HELM_PATH'
$env:WSLENV += if ($env:HELM_SECRETS_HELM_PATH -match "\\") {"/p"}
$env:WSLENV += ':HELM_SECRETS_SOPS_PATH'
$env:WSLENV += if ($env:HELM_SECRETS_SOPS_PATH -match "\\") {"/p"}
$env:WSLENV += ':HELM_SECRETS_VALS_PATH'
$env:WSLENV += if ($env:HELM_SECRETS_VALS_PATH -match "\\") {"/p"}
$env:WSLENV += ':HELM_SECRETS_CURL_PATH'
$env:WSLENV += if ($env:HELM_SECRETS_CURL_PATH -match "\\") {"/p"}
if ($args[0] -match "\\") {
$args[0] = $args[0] -replace "\\","/"
$args[0] = wsl wslpath "$($args[0])"
}
runShell "wsl.exe" $args
}
echo $args
if ('1', 'true' -Contains $env:HELM_DEBUG) {
Set-PSDebug -Trace 1
}
if ($null -eq $env:SOPS_GPG_EXEC) {
$env:SOPS_GPG_EXEC = (which gpg.exe)
}
if ($env:HELM_SECRETS_WINDOWS_SHELL -eq 'wsl' ) {
prepareWsl $args
}
$knownShellPaths = @(
("$($env:HELM_SECRETS_WINDOWS_SHELL)"),
("$($env:ProgramFiles)\Git\bin\bash.exe"),
("${$env:ProgramFiles(x86)}\Git\bin\bash.exe"),
("$($env:UserProfile)\scoop\shims\bash.exe"),
("$($env:UserProfile)\scoop\shims\sh.exe")
)
foreach($knownShellPath in $knownShellPaths) {
if (Test-Path -Path "$knownShellPath") {
runShell "$knownShellPath" $args
}
}
$gitPath = (which git.exe)
if (Test-Path -Path "$gitPath\..\bin\bash.exe") {
runShell "$gitPath\..\bin\bash.exe" $args
}
if ((which wsl.exe) -ne $null) {
prepareWsl $args
}
$shPath = (which sh.exe)
if ($shPath -ne $null) {
runShell $shPath $args
}
$bashPath = (which bash.exe)
if ($bashPath -ne $null) {
runShell $bashPath $args
}
echo "helm-secrets needs a unix shell. Please install WSL, cygwin or Git for Windows."
exit 1