-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
newQuote.ps1
76 lines (59 loc) · 2.18 KB
/
newQuote.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
param (
[string]$json = "./static/Database/Quotes.json"
)
Write-Host "
+-------------------------------------------------------------------------+
| ___ _____ _____ ____ _ _____ _ ____ _ ____ _____ |
| |_ _|_ _|_ _| | _ \ / \|_ _|/ \ | __ ) / \ / ___|| ____| |
| | | | | | | | | | |/ _ \ | | / _ \ | _ \ / _ \ \___ \| _| |
| | | | | | | | |_| / ___ \| |/ ___ \| |_) / ___ \ ___) | |___ |
| |___| |_| |_| |____/_/ \_\_/_/ \_\____/_/ \_\____/|_____| |
| Made with ♥ By Emad Adel |
+-------------------------------------------------------------------------+
"
try {
# Read existing JSON file
$jsonFilePath = $json
$existingData = Get-Content $jsonFilePath -Raw -ErrorAction Stop | ConvertFrom-Json
$QuotesList = @{
# Available options
1 = "quote"
2 = "info"
3 = "music"
}
# Prompt user to choose mothed
do {
Write-Host "Select text type"
foreach ($key in $QuotesList.Keys | Sort-Object) {
Write-Host "$key - $($QuotesList[$key])"
}
$choice = Read-Host "Enter the number corresponding to the methods"
if ([int]$choice -in $QuotesList.Keys) {
$type = $QuotesList[[int]$choice]
} else {
Write-Host "Invalid choice. Please select a valid option."
}
} until ([int]$choice -in $QuotesList.Keys)
# Prompt for input
$text = Read-Host "Enter text"
$name = Read-Host "Enter author name or source -You can skip this"
# Store input
$Quotes = @{
type = $type
text = $text
}
# Add name only if it's not empty
if (-not [string]::IsNullOrWhiteSpace($name)) {
$Quotes.name = $name
}
# Add new software object to existing array
$existingData.Quotes += $Quotes
# Write updated JSON to file
$existingData | ConvertTo-Json -Depth 4 | Out-File $jsonFilePath -ErrorAction Stop
}
catch {
Write-Host "An error occurred: $_"
}
finally {
Write-Host "Added successfully, Don't forget to build and test it before commit" -ForegroundColor Green
}