| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #Requires -Version 5.1
- # 读写 MySQL 连接配置。仅允许 mysql。
- function Get-MesDbConfigPath {
- return (Join-Path $PSScriptRoot "db-config.json")
- }
- function Get-DefaultMesDbConfig {
- return [ordered]@{
- engine = "mysql"
- host = "127.0.0.1"
- port = 3306
- database = "mes_cloud"
- user = "mes_cloud"
- password = "s2ENj7KaEySTC7dW"
- }
- }
- function ConvertTo-MesDbConfig {
- param($InputObject)
- $d = Get-DefaultMesDbConfig
- if ($null -eq $InputObject) { return $d }
- $engine = "mysql"
- if ($InputObject.PSObject.Properties.Name -contains "engine" -and $InputObject.engine) {
- $engine = ([string]$InputObject.engine).Trim().ToLower()
- }
- if ($engine -ne "mysql") {
- throw "仅支持 MySQL,不支持: $engine"
- }
- $hostName = [string]$InputObject.host
- if ([string]::IsNullOrWhiteSpace($hostName)) { $hostName = [string]$d.host }
- $hostName = $hostName.Trim()
- if ($hostName -match "jdbc:mysql://([^:/]+)") {
- $hostName = $Matches[1]
- }
- $hostName = $hostName -replace "^mysql://", ""
- if ($hostName -match "\s|/") {
- throw "IP/主机名不合法"
- }
- $port = [int]$d.port
- if ($InputObject.port) { [void][int]::TryParse([string]$InputObject.port, [ref]$port) }
- if ($port -lt 1 -or $port -gt 65535) { throw "端口必须在 1-65535" }
- $database = [string]$InputObject.database
- if ([string]::IsNullOrWhiteSpace($database)) { $database = [string]$d.database }
- $user = [string]$InputObject.user
- if ([string]::IsNullOrWhiteSpace($user)) { throw "请填写账号" }
- $password = [string]$InputObject.password
- if ($null -eq $password) { $password = "" }
- return [ordered]@{
- engine = "mysql"
- host = $hostName.Trim()
- port = $port
- database = $database.Trim()
- user = $user.Trim()
- password = $password
- }
- }
- function Read-MesDbConfig {
- $path = Get-MesDbConfigPath
- if (-not (Test-Path -LiteralPath $path)) {
- $cfg = Get-DefaultMesDbConfig
- Save-MesDbConfig -Config $cfg
- return $cfg
- }
- try {
- $raw = Get-Content -LiteralPath $path -Raw -Encoding UTF8
- if ([string]::IsNullOrWhiteSpace($raw)) { throw "配置文件为空" }
- # 去掉 # 注释行,避免 PowerShell ConvertFrom-Json 报「无效的 JSON 基元」
- $lines = $raw -split "`r?`n" | Where-Object { $_.Trim() -notmatch "^#" }
- $json = ($lines -join "`n").Trim()
- $start = $json.IndexOf("{")
- $end = $json.LastIndexOf("}")
- if ($start -ge 0 -and $end -gt $start) {
- $json = $json.Substring($start, $end - $start + 1)
- }
- $obj = $json | ConvertFrom-Json
- return (ConvertTo-MesDbConfig -InputObject $obj)
- } catch {
- $cfg = Get-DefaultMesDbConfig
- Save-MesDbConfig -Config $cfg
- return $cfg
- }
- }
- function Save-MesDbConfig {
- param($Config)
- $cfg = ConvertTo-MesDbConfig -InputObject $Config
- $path = Get-MesDbConfigPath
- $json = ($cfg | ConvertTo-Json -Depth 4)
- $utf8 = New-Object System.Text.UTF8Encoding $false
- [System.IO.File]::WriteAllText($path, $json, $utf8)
- return $cfg
- }
- function Write-MesMysqlCnf {
- param(
- $Config,
- [string]$CnfPath
- )
- $cfg = ConvertTo-MesDbConfig -InputObject $Config
- $pwd = [string]$cfg.password
- $pwd = $pwd.Replace("\", "\\").Replace('"', '\"')
- @(
- "[client]"
- "host=$($cfg.host)"
- "port=$($cfg.port)"
- "user=$($cfg.user)"
- "password=""$pwd"""
- "default-character-set=utf8mb4"
- ) | Set-Content -Path $CnfPath -Encoding ASCII
- }
|