Files
Libation/Source/LibationFileManager/EphemeralDictionary.cs
MBucari ce2b81036f Add license and settings overrides to LibationCli
- Add `LIBATION_FILES_DIR` environment variable to specify LibationFiles directory instead of appsettings.json
- OptionsBase supports overriding setting
  - Added `EphemeralSettings` which are loaded from Settings.json once and can be modified with the `--override` command parameter
- Added `get-setting` command
  - Prints (editable) settings and their values. Prints specified settings, or all settings if none specified
  - `--listEnumValues` option will list all names for a speficied enum-type settings. If no setting names are specified, prints all enum values for all enum settings.
  - Prints in a text-based table or bare with `-b` switch
- Added `get-license` command which requests a content license and prints it as a json to stdout
- Improved `liberate` command
  - Added `-force` option to force liberation without validation.
  - Added support to download with a license file supplied to stdin
  - Improve startup performance when downloading explicit ASIN(s)
  - Fix long-standing bug where cover art was not being downloading
2025-11-19 23:47:41 -07:00

45 lines
1.5 KiB
C#

using FileManager;
using Newtonsoft.Json.Linq;
#nullable enable
namespace LibationFileManager;
internal class EphemeralDictionary : IJsonBackedDictionary
{
private JObject JsonObject { get; }
public EphemeralDictionary()
{
JsonObject = new();
}
public EphemeralDictionary(JObject dataStore)
{
JsonObject = dataStore;
}
public bool Exists(string propertyName)
=> JsonObject.ContainsKey(propertyName);
public string? GetString(string propertyName, string? defaultValue = null)
=> JsonObject[propertyName]?.Value<string>() ?? defaultValue;
public T? GetNonString<T>(string propertyName, T? defaultValue = default)
=> GetObject(propertyName) is object obj ? IJsonBackedDictionary.UpCast<T>(obj) : defaultValue;
public object? GetObject(string propertyName)
=> JsonObject[propertyName]?.Value<object>();
public void SetString(string propertyName, string? newValue)
=> JsonObject[propertyName] = newValue;
public void SetNonString(string propertyName, object? newValue)
=> JsonObject[propertyName] = newValue is null ? null : JToken.FromObject(newValue);
public bool RemoveProperty(string propertyName)
=> JsonObject.Remove(propertyName);
public string? GetStringFromJsonPath(string jsonPath)
=> JsonObject.SelectToken(jsonPath)?.Value<string>();
public bool SetWithJsonPath(string jsonPath, string propertyName, string? newValue, bool suppressLogging = false)
{
if (JsonObject.SelectToken(jsonPath) is JToken token && token?[propertyName] is not null)
{
token[propertyName] = newValue;
return true;
}
return false;
}
}