-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
94 lines (82 loc) · 3.16 KB
/
Program.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using CommandLine;
using Humanizer;
using PhotoCopy.Directory;
using PhotoCopy.Files;
using PhotoCopy.Validators;
using System;
using System.IO;
namespace PhotoCopy;
public static class ApplicationState
{
public static Options Options { get; set; }
}
class Program
{
/// <summary>
/// Validates whether input is valid.
/// </summary>
/// <param name="options">Options</param>
/// <returns>True if input is valid</returns>
private static bool ValidateInput(Options options)
{
var sourceFile = new FileInfo(options.Source);
var isValid = true;
if (sourceFile.Exists)
{
Log.Print($"Source {sourceFile.FullName} does not exist", Options.LogLevel.errorsOnly);
isValid = false;
}
if (!sourceFile.Attributes.HasFlag(FileAttributes.Directory))
{
Log.Print("Source is not a directory", Options.LogLevel.errorsOnly);
isValid = false;
}
if (!options.DuplicatesFormat.Contains("{number}"))
{
Log.Print("Duplicates format does not contain {number}", Options.LogLevel.errorsOnly);
isValid = false;
}
if (!options.Destination.Contains(Options.DestinationVariables.Name) &&
!options.Destination.Contains(Options.DestinationVariables.NameNoExtension))
{
Console.WriteLine(
"Your destination path does not contain name or name without extension. This will result in files losing their original name and is generally undesirable. Are you absolutely sure about this? Write yes to confirm, anything else to abort.");
var response = Console.ReadLine();
if (response != "yes")
{
isValid = false;
}
}
return isValid;
}
private static void PrintParsedOptions(Options options)
{
Log.Print($"Input: {options.Source}", Options.LogLevel.verbose);
Log.Print($"Output: {options.Destination}", Options.LogLevel.verbose);
Log.Print($"Dry run: {options.DryRun}", Options.LogLevel.verbose);
Log.Print($"Mode: {options.Mode}", Options.LogLevel.verbose);
if (options.MaxDate.HasValue)
{
Log.Print($"Max date: {options.MaxDate.Value.ToLongDateString()} {options.MaxDate.Value.ToLongTimeString()} ({options.MaxDate.Value.Humanize()})", Options.LogLevel.verbose);
}
if (options.MinDate.HasValue)
{
Log.Print($"Min date: {options.MinDate.Value.ToLongDateString()} {options.MinDate.Value.ToLongTimeString()} ({options.MinDate.Value.Humanize()})", Options.LogLevel.verbose);
}
Log.Print("--------------", Options.LogLevel.verbose);
}
static void Main(string[] args)
{
Parser
.Default
.ParseArguments<Options>(args)
.WithParsed(options =>
{
if (!ValidateInput(options)) return;
ApplicationState.Options = options;
PrintParsedOptions(options);
var validators = ValidatorFactory.Create(options);
DirectoryCopier.Copy(options, validators);
});
}
}