db-config.ps1 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #Requires -Version 5.1
  2. # 读写 MySQL 连接配置。仅允许 mysql。
  3. function Get-MesDbConfigPath {
  4. return (Join-Path $PSScriptRoot "db-config.json")
  5. }
  6. function Get-DefaultMesDbConfig {
  7. return [ordered]@{
  8. engine = "mysql"
  9. host = "127.0.0.1"
  10. port = 3306
  11. database = "mes_cloud"
  12. user = "mes_cloud"
  13. password = "s2ENj7KaEySTC7dW"
  14. }
  15. }
  16. function ConvertTo-MesDbConfig {
  17. param($InputObject)
  18. $d = Get-DefaultMesDbConfig
  19. if ($null -eq $InputObject) { return $d }
  20. $engine = "mysql"
  21. if ($InputObject.PSObject.Properties.Name -contains "engine" -and $InputObject.engine) {
  22. $engine = ([string]$InputObject.engine).Trim().ToLower()
  23. }
  24. if ($engine -ne "mysql") {
  25. throw "仅支持 MySQL,不支持: $engine"
  26. }
  27. $hostName = [string]$InputObject.host
  28. if ([string]::IsNullOrWhiteSpace($hostName)) { $hostName = [string]$d.host }
  29. $hostName = $hostName.Trim()
  30. if ($hostName -match "jdbc:mysql://([^:/]+)") {
  31. $hostName = $Matches[1]
  32. }
  33. $hostName = $hostName -replace "^mysql://", ""
  34. if ($hostName -match "\s|/") {
  35. throw "IP/主机名不合法"
  36. }
  37. $port = [int]$d.port
  38. if ($InputObject.port) { [void][int]::TryParse([string]$InputObject.port, [ref]$port) }
  39. if ($port -lt 1 -or $port -gt 65535) { throw "端口必须在 1-65535" }
  40. $database = [string]$InputObject.database
  41. if ([string]::IsNullOrWhiteSpace($database)) { $database = [string]$d.database }
  42. $user = [string]$InputObject.user
  43. if ([string]::IsNullOrWhiteSpace($user)) { throw "请填写账号" }
  44. $password = [string]$InputObject.password
  45. if ($null -eq $password) { $password = "" }
  46. return [ordered]@{
  47. engine = "mysql"
  48. host = $hostName.Trim()
  49. port = $port
  50. database = $database.Trim()
  51. user = $user.Trim()
  52. password = $password
  53. }
  54. }
  55. function Read-MesDbConfig {
  56. $path = Get-MesDbConfigPath
  57. if (-not (Test-Path -LiteralPath $path)) {
  58. $cfg = Get-DefaultMesDbConfig
  59. Save-MesDbConfig -Config $cfg
  60. return $cfg
  61. }
  62. try {
  63. $raw = Get-Content -LiteralPath $path -Raw -Encoding UTF8
  64. if ([string]::IsNullOrWhiteSpace($raw)) { throw "配置文件为空" }
  65. # 去掉 # 注释行,避免 PowerShell ConvertFrom-Json 报「无效的 JSON 基元」
  66. $lines = $raw -split "`r?`n" | Where-Object { $_.Trim() -notmatch "^#" }
  67. $json = ($lines -join "`n").Trim()
  68. $start = $json.IndexOf("{")
  69. $end = $json.LastIndexOf("}")
  70. if ($start -ge 0 -and $end -gt $start) {
  71. $json = $json.Substring($start, $end - $start + 1)
  72. }
  73. $obj = $json | ConvertFrom-Json
  74. return (ConvertTo-MesDbConfig -InputObject $obj)
  75. } catch {
  76. $cfg = Get-DefaultMesDbConfig
  77. Save-MesDbConfig -Config $cfg
  78. return $cfg
  79. }
  80. }
  81. function Save-MesDbConfig {
  82. param($Config)
  83. $cfg = ConvertTo-MesDbConfig -InputObject $Config
  84. $path = Get-MesDbConfigPath
  85. $json = ($cfg | ConvertTo-Json -Depth 4)
  86. $utf8 = New-Object System.Text.UTF8Encoding $false
  87. [System.IO.File]::WriteAllText($path, $json, $utf8)
  88. return $cfg
  89. }
  90. function Write-MesMysqlCnf {
  91. param(
  92. $Config,
  93. [string]$CnfPath
  94. )
  95. $cfg = ConvertTo-MesDbConfig -InputObject $Config
  96. $pwd = [string]$cfg.password
  97. $pwd = $pwd.Replace("\", "\\").Replace('"', '\"')
  98. @(
  99. "[client]"
  100. "host=$($cfg.host)"
  101. "port=$($cfg.port)"
  102. "user=$($cfg.user)"
  103. "password=""$pwd"""
  104. "default-character-set=utf8mb4"
  105. ) | Set-Content -Path $CnfPath -Encoding ASCII
  106. }