-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJohnnyDecimalPathResolver.cs
64 lines (61 loc) · 2.36 KB
/
JohnnyDecimalPathResolver.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
using System.IO;
namespace Community.PowerToys.Run.Plugin.JohnnyDecimal
{
public record SearchResult(DirectoryInfo[]? Directories = null, string ErrorMessage = "")
{
public readonly DirectoryInfo[] Directories = Directories ?? [];
}
public class JohnnyDecimalPathResolver(string? rootFolderPath)
{
private readonly string _rootFolderPath = string.IsNullOrEmpty(rootFolderPath) ? "" : rootFolderPath;
public SearchResult FindPaths(JohnnyDecimalId id)
{
// ROOT_DIR_NOT_SET
if (string.IsNullOrEmpty(_rootFolderPath))
{
return new SearchResult(ErrorMessage: "Please set the root folder in the plugin settings");
}
// ROOT_DIR_NOT_FOUND
var rootDirectory = new DirectoryInfo(_rootFolderPath);
if (!rootDirectory.Exists)
{
return new SearchResult(ErrorMessage: "Root folder does not exist");
}
// AREA_NOT_FOUND
if (!id.HasArea)
{
return new SearchResult(ErrorMessage: $"Please provide a valid Area");
}
// AREA_DIR_EMPTY
var areas = rootDirectory.GetDirectories(id.AreaGlob, SearchOption.TopDirectoryOnly);
if (areas.Length == 0)
{
return new SearchResult(ErrorMessage: $"Area '{id.AreaGlob[..^1]}' is empty");
}
// CAT_NOT_FOUND
if (!id.HasCategory)
{
return new SearchResult(Directories: areas);
}
// CAT_DIR_EMPTY
var categories = areas[0].GetDirectories(id.CategoryGlob, SearchOption.TopDirectoryOnly);
if (categories.Length == 0)
{
return new SearchResult(ErrorMessage: $"Category '{id.CategoryGlob[..^1]}' is empty");
}
// ID_NOT_FOUND
if (!id.HasId)
{
return new SearchResult(Directories: categories);
}
// ID_DIR_EMPTY
var ids = categories[0].GetDirectories(id.IdGlob, SearchOption.TopDirectoryOnly);
if (ids.Length == 0)
{
return new SearchResult(ErrorMessage: $"ID '{id.IdGlob[..^1]}' is empty");
}
// SUCCESS
return new SearchResult(Directories: ids);
}
}
}