@@ -271,6 +271,146 @@ function Get-RcloneOpsConfigValue {
271271 return $null
272272}
273273
274+ function Get-RcloneOpsOptionalConfigValues {
275+ <#
276+ . SYNOPSIS
277+ 读取可选的 rclone JSON 主配置。
278+
279+ . PARAMETER ConfigPath
280+ JSON 配置文件路径;文件不存在时返回空表。
281+
282+ . OUTPUTS
283+ hashtable。存在配置时返回顶层配置键值,否则返回空 hashtable。
284+ #>
285+ [CmdletBinding ()]
286+ param (
287+ [Parameter (Mandatory = $true )]
288+ [string ]$ConfigPath
289+ )
290+
291+ $resolvedPath = if ([System.IO.Path ]::IsPathRooted($ConfigPath )) {
292+ $ConfigPath
293+ }
294+ else {
295+ Join-Path (Get-Location ).Path $ConfigPath
296+ }
297+
298+ if (-not (Test-Path - LiteralPath $resolvedPath - PathType Leaf)) {
299+ return @ {}
300+ }
301+
302+ return Read-RcloneOpsConfigValues - ConfigPath $ConfigPath
303+ }
304+
305+ function Get-RcloneOpsNestedConfigValue {
306+ <#
307+ . SYNOPSIS
308+ 读取嵌套 JSON 配置值。
309+
310+ . PARAMETER ConfigValues
311+ 顶层 JSON 配置键值。
312+
313+ . PARAMETER Section
314+ 顶层 section 名称,例如 `webui`。
315+
316+ . PARAMETER Name
317+ section 内部键名。
318+
319+ . OUTPUTS
320+ object。命中的嵌套配置值;未命中时返回 $null。
321+ #>
322+ [CmdletBinding ()]
323+ param (
324+ [Parameter (Mandatory = $true )]
325+ [hashtable ]$ConfigValues ,
326+
327+ [Parameter (Mandatory = $true )]
328+ [string ]$Section ,
329+
330+ [Parameter (Mandatory = $true )]
331+ [string ]$Name
332+ )
333+
334+ $sectionValue = Get-RcloneOpsConfigValue - Values $ConfigValues - Name $Section
335+ if ($null -eq $sectionValue ) {
336+ return $null
337+ }
338+
339+ $sectionTable = ConvertTo-RcloneOpsHashtable - InputObject $sectionValue
340+ return Get-RcloneOpsConfigValue - Values $sectionTable - Name $Name
341+ }
342+
343+ function Get-RcloneOpsOptionWithConfig {
344+ <#
345+ . SYNOPSIS
346+ 按 flag、环境变量、JSON 配置、默认值的优先级解析选项。
347+
348+ . PARAMETER Flags
349+ Split-RcloneOpsArguments 返回的 Flags 哈希表。
350+
351+ . PARAMETER Name
352+ 命令行 flag 名称,不包含前缀 `--`。
353+
354+ . PARAMETER EnvName
355+ 环境变量名称。
356+
357+ . PARAMETER ConfigValues
358+ 顶层 JSON 配置键值。
359+
360+ . PARAMETER Section
361+ JSON section 名称。
362+
363+ . PARAMETER ConfigName
364+ JSON section 内部键名。
365+
366+ . PARAMETER DefaultValue
367+ 未提供 flag、环境变量与 JSON 配置时使用的默认值。
368+
369+ . OUTPUTS
370+ System.String。解析后的选项值。
371+ #>
372+ [CmdletBinding ()]
373+ param (
374+ [Parameter (Mandatory = $true )]
375+ [hashtable ]$Flags ,
376+
377+ [Parameter (Mandatory = $true )]
378+ [string ]$Name ,
379+
380+ [Parameter (Mandatory = $true )]
381+ [string ]$EnvName ,
382+
383+ [Parameter (Mandatory = $true )]
384+ [hashtable ]$ConfigValues ,
385+
386+ [Parameter (Mandatory = $true )]
387+ [string ]$Section ,
388+
389+ [Parameter (Mandatory = $true )]
390+ [string ]$ConfigName ,
391+
392+ [Parameter (Mandatory = $true )]
393+ [AllowEmptyString ()]
394+ [string ]$DefaultValue
395+ )
396+
397+ if ($Flags.ContainsKey ($Name ) -and $Flags [$Name ] -is [string ] -and -not [string ]::IsNullOrWhiteSpace($Flags [$Name ])) {
398+ return [string ]$Flags [$Name ]
399+ }
400+
401+ $envValue = [Environment ]::GetEnvironmentVariable($EnvName , ' Process' )
402+ if (-not [string ]::IsNullOrWhiteSpace($envValue )) {
403+ return $envValue
404+ }
405+
406+ $configValue = Get-RcloneOpsNestedConfigValue - ConfigValues $ConfigValues - Section $Section - Name $ConfigName
407+ if ($null -ne $configValue -and -not [string ]::IsNullOrWhiteSpace([string ]$configValue )) {
408+ return [string ](Resolve-RcloneOpsEnvPlaceholder - Value $configValue - Context " $Section .$ConfigName " )
409+ }
410+
411+ return $DefaultValue
412+ }
413+
274414function Resolve-RcloneOpsEnvPlaceholder {
275415 <#
276416 . SYNOPSIS
@@ -619,11 +759,13 @@ function Start-RcloneOpsWebUi {
619759 [string []]$Passthrough
620760 )
621761
762+ $sourcePath = Get-RcloneOpsOption - Flags $Flags - Name ' source' - EnvName ' RCLONE_SOURCE_CONFIG_PATH' - DefaultValue $script :DefaultSourcePath
763+ $sourceValues = Get-RcloneOpsOptionalConfigValues - ConfigPath $sourcePath
622764 $rclone = Get-RcloneOpsOption - Flags $Flags - Name ' rclone' - EnvName ' RCLONE_BIN' - DefaultValue ' rclone'
623765 $configPath = Get-RcloneOpsOption - Flags $Flags - Name ' config' - EnvName ' RCLONE_CONFIG_PATH' - DefaultValue $script :DefaultConfigPath
624- $rcAddr = Get-RcloneOpsOption - Flags $Flags - Name ' addr' - EnvName ' RCLONE_RC_ADDR' - DefaultValue $script :DefaultRcAddr
625- $rcPass = Get-RcloneOpsOption - Flags $Flags - Name ' pass' - EnvName ' RCLONE_RC_PASS' - DefaultValue ' '
626- $rcUser = if ($rcPass ) { Get-RcloneOpsOption - Flags $Flags - Name ' user' - EnvName ' RCLONE_RC_USER' - DefaultValue $script :DefaultRcUser } else { ' ' }
766+ $rcAddr = Get-RcloneOpsOptionWithConfig - Flags $Flags - Name ' addr' - EnvName ' RCLONE_RC_ADDR' - ConfigValues $sourceValues - Section ' webui ' - ConfigName ' addr ' - DefaultValue $script :DefaultRcAddr
767+ $rcPass = Get-RcloneOpsOptionWithConfig - Flags $Flags - Name ' pass' - EnvName ' RCLONE_RC_PASS' - ConfigValues $sourceValues - Section ' webui ' - ConfigName ' pass ' - DefaultValue ' '
768+ $rcUser = if ($rcPass ) { Get-RcloneOpsOptionWithConfig - Flags $Flags - Name ' user' - EnvName ' RCLONE_RC_USER' - ConfigValues $sourceValues - Section ' webui ' - ConfigName ' user ' - DefaultValue $script :DefaultRcUser } else { ' ' }
627769 $isBackground = $Flags.ContainsKey (' background' )
628770 $logFile = Get-RcloneOpsOption - Flags $Flags - Name ' log-file' - EnvName ' RCLONE_LOG_FILE' - DefaultValue (Join-Path $script :DefaultLogDir ' webui.log' )
629771 if ($isBackground ) {
0 commit comments