-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigLoader.cs
More file actions
76 lines (69 loc) · 2.96 KB
/
Copy pathConfigLoader.cs
File metadata and controls
76 lines (69 loc) · 2.96 KB
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
namespace SCAScanner;
/// <summary>
/// Utility class for loading and managing SCA Scanner configuration files.
/// Handles loading from YAML files, writing templates, and merging with defaults.
/// </summary>
public static class ConfigLoader
{
/// <summary>
/// Default config file name to search for in the working directory.
/// </summary>
private const string DefaultConfigFileName = "config.yml";
/// <summary>
/// Loads configuration from a file, with fallback to default location.
/// </summary>
/// <param name="configPath">Explicit config file path (overrides default search). If null, tries default location.</param>
/// <param name="reporter">Optional reporter for logging which config file was loaded.</param>
/// <returns>Loaded ScannerConfig or default if not found.</returns>
/// <exception cref="InvalidOperationException">If config file exists but is invalid YAML.</exception>
public static ScannerConfig LoadConfig(string? configPath, IReporter? reporter = null)
{
string? actualPath = null;
if (!string.IsNullOrEmpty(configPath))
{
// Use explicitly provided config path
actualPath = configPath;
}
else if (File.Exists(DefaultConfigFileName))
{
// Fall back to default config.yml if it exists in working directory
actualPath = DefaultConfigFileName;
}
if (actualPath is null)
{
// No config file found, use defaults
return ScannerConfig.CreateDefault();
}
// Load and parse the config file
try
{
ScannerConfig config = ScannerConfig.LoadFromFile(actualPath);
reporter?.PrintInfo($"Loaded configuration from: {Path.GetFullPath(actualPath)}");
return config;
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException($"Error loading config file '{actualPath}': {ex.Message}", ex);
}
}
/// <summary>
/// Writes a template config file with all available options.
/// </summary>
/// <param name="outputPath">Where to write the template. If null or empty, uses default "config.yml".</param>
/// <param name="reporter">Optional reporter for logging the output path.</param>
/// <exception cref="InvalidOperationException">If template cannot be written.</exception>
public static void WriteTemplate(string? outputPath, IReporter? reporter = null)
{
string targetPath = string.IsNullOrEmpty(outputPath) ? DefaultConfigFileName : outputPath;
try
{
ScannerConfig.WriteTemplate(targetPath);
string fullPath = Path.GetFullPath(targetPath);
reporter?.PrintInfo($"Template config file created: {fullPath}");
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException($"Failed to write config template: {ex.Message}", ex);
}
}
}