Skip to content

Commit

Permalink
macossettings: implement default settings for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mjcheetham committed Jan 24, 2025
1 parent 6fddffa commit 0ee735d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
33 changes: 32 additions & 1 deletion docs/enterprise-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,38 @@ those of the [Git configuration][config] settings.
The type of each registry key can be either `REG_SZ` (string) or `REG_DWORD`
(integer).

## macOS/Linux
## macOS

Default settings values come from macOS's preferences system. Configuration
profiles can be deployed to devices using a compatible Mobile Device Management
(MDM) solution.

Configuration for Git Credential Manager must take the form of a dictionary, set
for the domain `git-credential-manager` under the key `configuration`. For
example:

```shell
defaults write git-credential-manager configuration -dict-add <key> <value>
```

..where `<key>` is the name of the settings from the [Git configuration][config]
reference, and `<value>` is the desired value.

All values in the `configuration` dictionary must be strings. For boolean values
use `true` or `false`, and for integer values use the number in string form.

To read the current configuration:

```console
$ defaults read git-credential-manager configuration
{
<key1> = <value1>;
...
<keyN> = <valueN>;
}
```

## Linux

Default configuration setting stores has not been implemented.

Expand Down
2 changes: 1 addition & 1 deletion src/shared/Core/CommandContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public CommandContext()
gitPath,
FileSystem.GetCurrentDirectory()
);
Settings = new Settings(Environment, Git);
Settings = new MacOSSettings(Environment, Git, Trace);
}
else if (PlatformUtils.IsLinux())
{
Expand Down
67 changes: 67 additions & 0 deletions src/shared/Core/Interop/MacOS/MacOSSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 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("git-credential-manager"); // TODO: move to a constant
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;
}
}
}
}

0 comments on commit 0ee735d

Please # to comment.