-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathMacOSSettings.cs
67 lines (57 loc) · 2.51 KB
/
MacOSSettings.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
using System;
using System.Collections.Generic;
namespace GitCredentialManager.Interop.MacOS
{
/// <summary>
/// Reads settings from Git configuration, environment variables, and defaults from the system.
/// </summary>
public class MacOSSettings : Settings
{
private readonly ITrace _trace;
public MacOSSettings(IEnvironment environment, IGit git, ITrace trace)
: base(environment, git)
{
EnsureArgument.NotNull(trace, nameof(trace));
_trace = trace;
PlatformUtils.EnsureMacOS();
}
protected internal override bool TryGetExternalDefault(string section, string scope, string property, out string value)
{
value = null;
try
{
// Check for app default preferences for our bundle ID.
// Defaults can be deployed system administrators via device management profiles.
var prefs = new MacOSPreferences(Constants.MacOSBundleId);
IDictionary<string, string> dict = prefs.GetDictionary("configuration");
if (dict is null)
{
// No configuration key exists
return false;
}
// Wrap the raw dictionary in one configured with the Git configuration key comparer.
// This means we can use the same key comparison rules as Git in our configuration plist dict,
// That is, sections and names are insensitive to case, but the scope is case-sensitive.
var config = new Dictionary<string, string>(dict, GitConfigurationKeyComparer.Instance);
string name = string.IsNullOrWhiteSpace(scope)
? $"{section}.{property}"
: $"{section}.{scope}.{property}";
if (!config.TryGetValue(name, out value))
{
// No property exists
return false;
}
_trace.WriteLine($"Default setting found in app preferences: {name}={value}");
return true;
}
catch (Exception ex)
{
// Reading defaults is not critical to the operation of the application
// so we can ignore any errors and just log the failure.
_trace.WriteLine("Failed to read default setting from app preferences.");
_trace.WriteException(ex);
return false;
}
}
}
}