Repository navigation
Expand file tree
/
Copy pathBaseConfig.ps1
More file actions
90 lines (79 loc) · 1.9 KB
/
Copy pathBaseConfig.ps1
File metadata and controls
90 lines (79 loc) · 1.9 KB
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
<#
MindMiner Copyright (C) 2017-2022 Oleg Samsonov aka Quake4
https://github.com/Quake4/MindMiner
License GPL-3.0
#>
# read/write/store base confirguration
class BaseConfig {
static [string] $Filename = ".config.txt"
# save config file
[void] Save([string] $fn) {
$this | ConvertTo-Json -Depth 10 | Out-File -FilePath $fn -Force
}
# check exists file and really parsed
static [bool] Exists([string] $fn) {
try {
$hash = [BaseConfig]::Read($fn)
if ($hash -is [hashtable] -and $hash.Count -gt 0) {
return $true
}
}
catch { }
return $false
}
static [void] Save([string] $fn, [hashtable] $hash) {
$hash | ConvertTo-Json | Out-File -FilePath $fn -Force
}
# read json config
static [hashtable] Read([string] $fn) {
$temp = Get-Content -Path $fn | ConvertFrom-Json
if ($temp) {
$hash = @{}
$temp | Get-Member -MemberType NoteProperty | ForEach-Object { $hash.Add($_.Name, $temp."$($_.Name)") }
if ($hash.Count -gt 0) {
return $hash
}
}
return $null
}
# read or create config
static [hashtable] ReadOrCreate([string] $fn, [hashtable] $hash) {
try {
$temp = Get-Content -Path $fn | ConvertFrom-Json
}
catch {
$temp = $null
# if exists rename to bak
if (Test-Path $fn) {
Remove-Item "$fn.bak" -Force | Out-Null
Rename-Item $fn "$fn.bak" -Force | Out-Null
}
}
if ($temp) {
$hash = @{}
$temp | Get-Member -MemberType NoteProperty | ForEach-Object { $hash.Add($_.Name, $temp."$($_.Name)") }
return $hash
}
else {
$hash | ConvertTo-Json -Depth 10 | Out-File -FilePath $fn -Force
return $hash
}
}
<# read or create config
static [Object[]] ReadOrCreate([string] $fn, [Object[]] $array) {
try {
$temp = Get-Content -Path $fn | ConvertFrom-Json
}
catch {
$temp = $null
}
if ($temp) {
return $temp
}
else {
$array | ConvertTo-Json -Depth 10 | Out-File -FilePath $fn -Force
return $array
}
}
#>
}