|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace GitCredentialManager.Interop.MacOS |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Reads settings from Git configuration, environment variables, and defaults from the system. |
| 8 | + /// </summary> |
| 9 | + public class MacOSSettings : Settings |
| 10 | + { |
| 11 | + private readonly ITrace _trace; |
| 12 | + |
| 13 | + public MacOSSettings(IEnvironment environment, IGit git, ITrace trace) |
| 14 | + : base(environment, git) |
| 15 | + { |
| 16 | + EnsureArgument.NotNull(trace, nameof(trace)); |
| 17 | + _trace = trace; |
| 18 | + |
| 19 | + PlatformUtils.EnsureMacOS(); |
| 20 | + } |
| 21 | + |
| 22 | + protected override bool TryGetExternalDefault(string section, string scope, string property, out string value) |
| 23 | + { |
| 24 | + value = null; |
| 25 | + |
| 26 | + try |
| 27 | + { |
| 28 | + // Check for app default preferences for our bundle ID. |
| 29 | + // Defaults can be deployed system administrators via device management profiles. |
| 30 | + var prefs = new MacOSPreferences(Constants.MacOSBundleId); |
| 31 | + IDictionary<string, string> dict = prefs.GetDictionary("configuration"); |
| 32 | + |
| 33 | + if (dict is null) |
| 34 | + { |
| 35 | + // No configuration key exists |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + // Wrap the raw dictionary in one configured with the Git configuration key comparer. |
| 40 | + // This means we can use the same key comparison rules as Git in our configuration plist dict, |
| 41 | + // That is, sections and names are insensitive to case, but the scope is case-sensitive. |
| 42 | + var config = new Dictionary<string, string>(dict, GitConfigurationKeyComparer.Instance); |
| 43 | + |
| 44 | + string name = string.IsNullOrWhiteSpace(scope) |
| 45 | + ? $"{section}.{property}" |
| 46 | + : $"{section}.{scope}.{property}"; |
| 47 | + |
| 48 | + if (!config.TryGetValue(name, out value)) |
| 49 | + { |
| 50 | + // No property exists |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + _trace.WriteLine($"Default setting found in app preferences: {name}={value}"); |
| 55 | + return true; |
| 56 | + } |
| 57 | + catch (Exception ex) |
| 58 | + { |
| 59 | + // Reading defaults is not critical to the operation of the application |
| 60 | + // so we can ignore any errors and just log the failure. |
| 61 | + _trace.WriteLine("Failed to read default setting from app preferences."); |
| 62 | + _trace.WriteException(ex); |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments