-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ConfigParser.cs
133 lines (115 loc) · 5.07 KB
/
ConfigParser.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Licensed to Roland Pihlakas under one or more agreements.
// Roland Pihlakas licenses this file to you under the GNU Lesser General Public License, ver 2.1.
// See the LICENSE and copyrights.txt files for more information.
//
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Configuration;
namespace FolderSync
{
internal static class ConfigParser
{
public static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
public static readonly bool IsMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
public static bool IgnoreCase
{
get
{
//Under Windows assume NTFS or FAT filesystem which are usually case-insensitive. Under Mac filesystems are also case-insensitive by default.
return Global.CaseSensitiveFilenames == false
|| (Global.CaseSensitiveFilenames == null && !ConfigParser.IsLinux);
}
}
public static string ToUpperInvariantOnWindows(this string text)
{
if (ConfigParser.IgnoreCase)
return text?.ToUpperInvariant();
else
return text;
}
public static long? GetLong(this IConfiguration config, params string[] sectionKeyAlternateNames)
{
foreach (var sectionKeyAlternateName in sectionKeyAlternateNames)
{
var text = config[sectionKeyAlternateName];
if (text != null)
{
//long result;
//if (long.TryParse(text, out result))
// return result;
return long.Parse(text); //NB! if the parameter exists then it must be in a proper numeric format
}
}
return null;
}
public static string GetTextUpperOnWindows(this IConfiguration config, bool? caseSensitiveFilenames, params string[] sectionKeyAlternateNames)
{
if (caseSensitiveFilenames == false || (caseSensitiveFilenames == null && !IsLinux))
return config.GetTextUpper(sectionKeyAlternateNames);
else
return config.GetText(sectionKeyAlternateNames);
}
public static string GetTextUpper(this IConfiguration config, params string[] sectionKeyAlternateNames)
{
return config.GetText(sectionKeyAlternateNames)?.ToUpperInvariant();
}
public static string GetText(this IConfiguration config, params string[] sectionKeyAlternateNames)
{
foreach (var sectionKeyAlternateName in sectionKeyAlternateNames)
{
var text = config[sectionKeyAlternateName];
if (text != null)
return text;
}
return null;
}
public static List<long> GetLongList(this IConfiguration config, params string[] sectionKeyAlternateNames)
{
foreach (var sectionKeyAlternateName in sectionKeyAlternateNames)
{
var list = config.GetSection(sectionKeyAlternateName).GetList();
if (list.Count > 0)
{
return list
.Select(text => long.Parse(text)) //NB! if the parameter exists then it must be in a proper numeric format
.ToList();
}
}
return new List<long>();
}
public static List<string> GetListUpperOnWindows(this IConfiguration config, bool? caseSensitiveFilenames, params string[] sectionKeyAlternateNames)
{
if (caseSensitiveFilenames == false || (caseSensitiveFilenames == null && !IsLinux))
return config.GetListUpper(sectionKeyAlternateNames);
else
return config.GetList(sectionKeyAlternateNames);
}
public static List<string> GetListUpper(this IConfiguration config, params string[] sectionKeyAlternateNames)
{
return config.GetList(sectionKeyAlternateNames)
.Select(x => x?.ToUpperInvariant())
.ToList();
}
public static List<string> GetList(this IConfiguration config, params string[] sectionKeyAlternateNames)
{
foreach (var sectionKeyAlternateName in sectionKeyAlternateNames)
{
var list = config.GetSection(sectionKeyAlternateName).GetList();
if (list.Count > 0)
return list;
}
return new List<string>();
}
public static List<string> GetList(this IConfigurationSection config)
{
//TODO: detect also missing key and use a default value in such case
if (config.Value != null)
return new List<string>() { config.Value };
else
return config.GetChildren().Select(c => c.Value).ToList();
}
}
}