Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b25e50d

Browse files
update start-fastcopy structure
1 parent 39a66b2 commit b25e50d

File tree

5 files changed

+119
-58
lines changed

5 files changed

+119
-58
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
function Confirm-SFCArgs {
2+
3+
[CmdletBinding()]
4+
5+
param(
6+
[Parameter(Mandatory = $true)]
7+
[Alias("SourceFolder")]
8+
[string]$sourceFolderPath,
9+
10+
[Parameter(Mandatory = $true)]
11+
[Alias("TargetFolder")]
12+
[string]$targetFolderPath,
13+
14+
[Parameter(Mandatory = $false)]
15+
[string]$fastCopyPath,
16+
17+
[Parameter(Mandatory = $false)]
18+
[Alias("Mode")]
19+
[string]$strMode = "full",
20+
21+
[Parameter(Mandatory = $false)]
22+
[Alias("Speed")]
23+
[int]$intSpeed,
24+
25+
[Parameter(Mandatory = $false)]
26+
[Alias("Delay")]
27+
[int]$delaySeconds = 0,
28+
29+
[Parameter(Mandatory = $false)]
30+
[Alias("Verify")]
31+
[int]$verifyDigit = 1,
32+
33+
[Parameter(Mandatory = $false)]
34+
[Alias("Exec")]
35+
[int]$execDigit = 1
36+
)
37+
38+
39+
# Normalize user input for consistent validation (case-insensitive)
40+
$strMode = $strMode.ToLower()
41+
42+
# Validate selected mode against supported options
43+
$validModes = @("full", "autoslow", "suspend", "custom")
44+
$isModeValid = $validModes -contains $strMode
45+
46+
if (-not $isModeValid) {
47+
Write-Error "Your mode selection '$strMode' is invalid."
48+
Write-Error "Please select from: $($validModes -join ', ')."
49+
return
50+
}
51+
52+
# Prevent usage of -Speed unless mode is 'custom'
53+
if ($strMode -ne "custom" -and $PSBoundParameters.ContainsKey("intSpeed")) {
54+
Write-Error "The -Speed parameter is only allowed when -Mode is 'custom'."
55+
return
56+
}
57+
58+
# Enforce -Speed validation if custom mode is selected
59+
if ($strMode -eq "custom") {
60+
if (-not $PSBoundParameters.ContainsKey("intSpeed")) {
61+
Write-Error "When -Mode is 'custom', the -Speed parameter is required."
62+
return
63+
}
64+
if ($intSpeed -lt 1 -or $intSpeed -gt 9) {
65+
Write-Error "Speed value '$intSpeed' is invalid. Use an integer between 1 and 9."
66+
return
67+
}
68+
}
69+
70+
# Validate verification flag
71+
if ($verifyDigit -ne 0 -and $verifyDigit -ne 1) {
72+
Write-Error "Invalid verification option: use 1 (enable) or 0 (disable)."
73+
return
74+
}
75+
76+
# Validate execution flag
77+
if ($execDigit -ne 0 -and $execDigit -ne 1) {
78+
Write-Error "Invalid execution option: use 1 (run) or 0 (simulate)."
79+
return
80+
}
81+
82+
83+
}

‎Project_Start-FastCopy/FastCopyTools.psd1 renamed to ‎Project_Start-FastCopy/Modules/FastCopyTools.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@{
1010

1111
# Script module or binary module file associated with this manifest.
12-
RootModule = 'FastCopyTools.psm1'
12+
RootModule = '.\FastCopyTools.psm1'
1313

1414
# Version number of this module.
1515
ModuleVersion = '1.0.0'
@@ -24,7 +24,7 @@
2424
Author = 'Jialiang Chang'
2525

2626
# Company or vendor of this module
27-
CompanyName = 'Unknown'
27+
CompanyName = 'N/A'
2828

2929
# Copyright statement for this module
3030
Copyright = '(c) Jialiang Chang. All rights reserved.'
@@ -69,7 +69,7 @@
6969
# NestedModules = @()
7070

7171
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
72-
FunctionsToExport = @('Get-ChildFolderPath','Build-FCargs','Get-Config','Test-IsNullOrWhiteSpace','Confirm-FCpath')
72+
FunctionsToExport = '*'
7373

7474
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
7575
CmdletsToExport = '*'
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
1-
# FastCopyTools.psm1
2-
# This module dynamically loads helper functions from .ps1 files and exports only verified functions.
3-
41
# Initialize list to track successfully loaded helper function names
52
$loadedFunctions = @()
63

4+
# Initialize list to track expected functions, used for dynamic tracking
5+
$expectedFunctions = @()
6+
77
# Retrieve all .ps1 helper scripts in the Helpers subdirectory
8-
$helperFunctionFiles = Get-ChildItem -Path "$PSScriptRoot\Helpers" -Filter *.ps1 -ErrorAction Stop
8+
# Optional: load by folder, but not very necessary
9+
try {
10+
$helperFunctionFiles = Get-ChildItem -Path "$PSScriptRoot\..\Helpers" -Recurse -Filter *.ps1 -ErrorAction Stop
11+
}
12+
catch {
13+
Write-Error "Unable to find helper functions."
14+
throw
15+
}
16+
17+
# Dot source main function
18+
$expectedFunctions += 'Start-FastCopy'
19+
# Force means that if main is already sourced, it will be removed and sourced again
20+
. "$PSScriptRoot\..\Start-FastCopy.ps1" -Force
21+
$loadedFunctions += 'Start-FastCopy'
922

1023
# Dot-source each helper file and track loaded function names
1124
foreach ($file in $helperFunctionFiles) {
1225
try {
13-
. $file.FullName
26+
# Add this file to the expected list
27+
$expectedFunctions += [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
28+
29+
# Dot source
30+
. $file.FullName -Force
1431
Write-Verbose "Loaded helper: $($file.Name)"
1532

1633
# Extract base name (without extension) for function tracking
1734
$loadedFunctions += [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
1835
}
1936
catch {
20-
Write-Warning "Failed to load helper script '$($file.Name)': $_"
37+
Write-Warning "Failed to load helper function '$($file.Name)': $_"
2138
}
2239
}
2340

24-
# Define required function names that must be loaded successfully
25-
$expectedFunctions = @('Get-ChildFolderPath', 'Build-FCargs', 'Get-Config', 'Test-IsNullOrWhiteSpace', 'Confirm-FCpath')
26-
2741
# Verify that each expected function was actually loaded into the session
2842
foreach ($func in $expectedFunctions) {
2943
if (-not (Get-Command $func -ErrorAction SilentlyContinue)) {
@@ -35,4 +49,5 @@ foreach ($func in $expectedFunctions) {
3549
}
3650

3751
# Export only the validated and expected functions to consumers of this module
52+
# Optional: better validation
3853
Export-ModuleMember -Function $expectedFunctions

‎Project_Start-FastCopy/Start-FastCopy.ps1

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -111,51 +111,15 @@ function Start-FastCopy {
111111
[int]$execDigit = 1
112112
)
113113

114-
# Normalize user input for consistent validation (case-insensitive)
115-
$strMode = $strMode.ToLower()
116-
117-
# Validate selected mode against supported options
118-
$validModes = @("full", "autoslow", "suspend", "custom")
119-
$isModeValid = $validModes -contains $strMode
120-
121-
if (-not $isModeValid) {
122-
Write-Error "Your mode selection '$strMode' is invalid."
123-
Write-Error "Please select from: $($validModes -join ', ')."
124-
return
125-
}
126-
127-
# Prevent usage of -Speed unless mode is 'custom'
128-
if ($strMode -ne "custom" -and $PSBoundParameters.ContainsKey("intSpeed")) {
129-
Write-Error "The -Speed parameter is only allowed when -Mode is 'custom'."
130-
return
131-
}
132-
133-
# Enforce -Speed validation if custom mode is selected
134-
if ($strMode -eq "custom") {
135-
if (-not $PSBoundParameters.ContainsKey("intSpeed")) {
136-
Write-Error "When -Mode is 'custom', the -Speed parameter is required."
137-
return
138-
}
139-
if ($intSpeed -lt 1 -or $intSpeed -gt 9) {
140-
Write-Error "Speed value '$intSpeed' is invalid. Use an integer between 1 and 9."
141-
return
142-
}
143-
}
144-
145-
# Validate verification flag
146-
if ($verifyDigit -ne 0 -and $verifyDigit -ne 1) {
147-
Write-Error "Invalid verification option: use 1 (enable) or 0 (disable)."
148-
return
149-
}
150-
151-
# Validate execution flag
152-
if ($execDigit -ne 0 -and $execDigit -ne 1) {
153-
Write-Error "Invalid execution option: use 1 (run) or 0 (simulate)."
154-
return
155-
}
156-
157-
# Import helper module
158-
Import-Module "$PSScriptRoot\FastCopyTools.psd1" -Force
114+
Confirm-SFCArgs `
115+
-sourceFolderPath $sourceFolderPath `
116+
-targetFolderPath $targetFolderPath `
117+
-fastCopyPath $fastCopyPath `
118+
-strMode $strMode `
119+
-intSpeed $intSpeed `
120+
-delaySeconds $delaySeconds `
121+
-verifyDigit $verifyDigit `
122+
-execDigit $execDigit
159123

160124
# Define action description for WhatIf/Confirm support
161125
$action = "Copy data from $sourceFolderPath"
@@ -233,6 +197,5 @@ function Start-FastCopy {
233197
}
234198

235199
Write-Host "`nCopy task complete.`n"
236-
exit 1
237200
}
238201
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /