-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-ConfigItem.ps1
132 lines (103 loc) · 3.44 KB
/
Get-ConfigItem.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
# Import the necesssary files
. "$PSScriptRoot\Get-EtcdValue.ps1"
. "$PSScriptRoot\Set-EtcdValue.ps1"
. "$PSScriptRoot\Get-InfisicalValue.ps1"
function Get-ConfigItem() {
[CmdletBinding()]
param (
[string]
# Server to use to get the information
$Server = $env:CONFIG_SERVER,
[Parameter(Mandatory = $true)]
[string]
# This is the path to the item in the configuration system
$Path,
[switch]
# State that Etcd should be used
$Etcd,
[switch]
# State that Infisical shhould be used
$Infisical,
[ValidatePattern("ado|raw|yaml|.+?(?=,|$)|json|kv")]
[string]
# Format of the output data
$Format = "raw",
[string[]]
# Otions that can be passed, such as the ability to change the case
# of the key, or turn the valus that contain a comma into an array
$Options = @(),
[string]
$Project
)
if ([string]::IsNullOrEmpty($Server)) {
throw "Server is required"
}
# Throw an error if both Etcd and Infisical are used
if ($Etcd.IsPresent -and $Infisical.IsPresent) {
throw "Both Etcd and Infisical cannot be used at the same time"
}
# If Etcd is used, get the value from Etcd
if ($Etcd.IsPresent) {
$items = Get-EtcdValue -Server $Server -Prefix $Path
}
# If Infisical is used, get the value from Infisical
if ($Infisical.IsPresent) {
# if the project has not been explicilty set, infer it from the path that has been given
if ([string]::IsNullOrEmpty($Project)) {
$Project, $remaining = $Path.Split("/")
$path = $remaining -join "/"
} else {
$path = $Path
}
$items = Get-InfisicalValue -Server $Server -Project $Project -Name $path
}
# iterate around the keys in the items
$data = @()
foreach ($key in $items.Keys) {
# set the key to the correct case based on the options
if ($options -contains "upper") {
$key = $key.ToUpper()
}
if ($options -contains "lower") {
$key = $key.ToLower()
}
# split the value based on the options
if ($options -contains "split") {
$items[$key] = $items[$key] -split ","
}
# based on the format, output the appropriate data
switch -wildcard ($Format) {
"ado" {
$data += "##vso[task.setvariable variable={0}]{1}" -f $key, $items[$key]
}
"kv" {
$data += "{0}=`"{1}`"" -f $key, $items[$key]
}
"env*" {
$prefix = ""
# Determine if a prefix has been set
$prefix = $Format.split(":")[-1]
# Set the name of the environment variable
$name = "{0}{1}" -f $prefix, $key
Write-Host "Setting Environment variable: $name"
# Set the environment variable in the shell
$path = "env:\{0}" -f $name
Set-Item -Path $path -Value $items[$key]
}
}
}
# switch on the format parameter
switch ($Format) {
"raw" {
$data = $items
}
"json" {
$data = $items | ConvertTo-Json
}
"yaml" {
$data = $items | ConvertTo-Yaml
}
}
# Output the data that has been set
Write-Output $data
}