-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghe_config.ps1
96 lines (81 loc) · 2.37 KB
/
ghe_config.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
#
# ghe_config.ps1 : GheConfig Implementation Classes
#
class GheConfig : System.Collections.Specialized.OrderedDictionary
{
# Intance (GheConfig) Singleton pattern
static [GheConfig] Get([GheClient] $GheClient)
{ return [GheConfig]::Get($GheClient, $null) }
static [GheConfig] Get([GheClient] $GheClient, [String] $RegEx)
{
# Create the configuration cache if it does not exists
if($GheClient._Config -eq $null)
{
$config = [GheConfig]::new($GheClient, $RegEx)
# Attach the configuration to the client for subsequent call cache
$GheClient | Add-Member NoteProperty -Name _Config -Value $config -Force
}
return $GheClient._Config
}
Hidden [GheCommand] $_Command
GheConfig([GheClient] $GheClient) : base()
{ $this._create($GheClient, $null) }
GheConfig([GheClient] $GheClient, [String] $RegEx) : base()
{ $this._create($GheClient, $RegEx) }
hidden [void] _create(
[GheClient] $GheClient,
[String] $RegEx)
{
# Create the linux command text
if(!$RegEx)
{ $CommandText = "ghe-config -l" }
else
{ $CommandText = "ghe-config --get-regexp '{0}'" -f $RegEx }
# The tricky way to set the class property without adding a key / value
# pair to the [hashtable].
$CommandObj = [GheCommand]::new($CommandText)
[GheConfig].GetProperty("_Command").SetValue(
$this, $CommandObj)
# Run ssh command to get the result
$GheClient.SendCommand($CommandObj)
# Convert Response parameters list into parameters dictionary
$conf_list = $CommandObj.Response.Output
$last_key = $null
ForEach($conf_obj in $conf_list)
{
switch -regex ($conf_obj)
{
"^-----END .*-----$"
{
$this[$last_key] += "`r`n" + $conf_obj
$last_key = $null
break
}
# With "ghe-config -l" value is separated from the parameter by the first equal
# With "ghe-config --get-regexp -l" value is separated from the parameter by the first space
"^([^= ]*)[= ](-----BEGIN .*-----)$"
{
$last_key = $matches[1]
$this[$last_key] = $matches[2]
break
}
"^([^= ]*)[= ](.*)$" # Parameter / Value
{
if($last_key -eq $null)
{
$this[$matches[1]] = $matches[2]
break
}
}
"^.*$"
{
if($last_key -ne $null)
{ $this[$last_key] += "`r`n" + $conf_obj }
else
{ Write-Output("[ERROR] : Unable to parse {0}" -f $conf_obj) }
break
}
}
}
}
}