-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.fs
79 lines (65 loc) · 2.96 KB
/
Configuration.fs
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
77
78
79
/// Module containing all the configuration options of the program.
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module NikonTheThird.Krystallizer.Configuration
open Serilog
open System.IO
open System.Text.Json
/// Exception raised when the configuration file could not be
/// found. Contains the path where it was expected to be.
exception MissingConfigurationFileException of path : string
/// Exception raised when a profile name is passed as a
/// command line argument that does not exist.
exception UnknownProfileException of profile : string
/// Specifies an execution profile of the program.
type [<Struct>] Profile = {
/// Names all the root directories that should be handled as
/// part of this profile.
RootDirectories : string list
/// Specifies whether this profile performs trash file removal.
RemoveTrash : bool
/// Specifies whether this profile removes completely empty files.
RemoveEmptyFiles : bool
/// Specifies whether a file sizes are checked to see if a hash
/// needs to be recomputed. The file size lookup can be quite
/// an expensive operation.
CheckFileSizes : bool
}
/// Specifies the global configuration of the program.
type [<Struct>] Configuration = {
/// The connection string used to connect to the database.
ConnectionString : string
/// The path relative to the executing assembly where the
/// root directories reside.
RootDirectoryParentPath : string
/// The path relative to the executing assembly where the
/// file containing the duplicates should be generated.
DuplicatesFilePath : string
/// The regular expression used to detect trash files.
TrashRegex : string
/// The number of parallel file hash operations.
DegreeOfParallelism : int32
/// The execution profiles of the program that determine
/// which root directories to handle.
Profiles : Map<string, Profile>
}
/// Reads and parses the configuration file at the given path.
/// If it does not exist, a MissingConfigurationFileException is raised.
let readConfigurationFile path = async {
let! token = Async.CancellationToken
try use fileStream = File.OpenRead path
return! JsonSerializer.DeserializeAsync<Configuration> (fileStream, jsonSerializerOptions, token)
|> Async.AwaitValueTask
with :? FileNotFoundException ->
do Log.Error ("Could not find configuration file at {path}", path)
return raise (MissingConfigurationFileException path)
}
/// Attempts to read an execution profile from the given command line
/// arguments. The default execution profile is used when none is specified.
/// If the profile does not exist, a UnknownProfileException is raised.
let readProfileFromArguments configuration =
Array.tryHead
>> Option.defaultValue "default"
>> fun profileName ->
configuration.Profiles
|> Map.tryFind profileName
|> Option.defaultWith (fun () -> raise (UnknownProfileException profileName))