|
| 1 | +// Copyright (c) KeelMatrix |
| 2 | + |
| 3 | +using System.Text.Json; |
| 4 | +using System.Text.Json.Nodes; |
| 5 | + |
| 6 | +namespace KeelMatrix.QueryWatch.Cli.Telemetry { |
| 7 | + internal static class RepoLocalTelemetryConfigInspector { |
| 8 | + internal const string RepositoryConfigFileName = "keelmatrix.telemetry.json"; |
| 9 | + internal const string ManagedByPropertyName = "managedBy"; |
| 10 | + internal const string ManagedByQwatchValue = "qwatch"; |
| 11 | + internal const int MaxRepositoryConfigBytes = 16 * 1024; |
| 12 | + |
| 13 | + private static readonly IRepoLocalTelemetryConfigFileAccess DefaultFileAccess = new RepoLocalTelemetryConfigFileAccess(); |
| 14 | + |
| 15 | + internal static RepoLocalTelemetryConfigInspection Inspect(string repoRoot) { |
| 16 | + return InspectPath(Path.Combine(repoRoot, RepositoryConfigFileName)); |
| 17 | + } |
| 18 | + |
| 19 | + internal static RepoLocalTelemetryConfigInspection InspectPath(string path) { |
| 20 | + return InspectPath(path, DefaultFileAccess); |
| 21 | + } |
| 22 | + |
| 23 | + internal static RepoLocalTelemetryConfigInspection InspectPath(string path, IRepoLocalTelemetryConfigFileAccess fileAccess) { |
| 24 | + try { |
| 25 | + RepoLocalTelemetryConfigFileMetadata fileMetadata = fileAccess.GetMetadata(path); |
| 26 | + if (!fileMetadata.Exists) |
| 27 | + return RepoLocalTelemetryConfigInspection.Missing(path); |
| 28 | + |
| 29 | + if (fileMetadata.Length > MaxRepositoryConfigBytes) |
| 30 | + return RepoLocalTelemetryConfigInspection.TooLarge(path); |
| 31 | + |
| 32 | + string text = fileAccess.ReadAllText(path); |
| 33 | + if (text.Length == 0) |
| 34 | + return RepoLocalTelemetryConfigInspection.InvalidJson(path); |
| 35 | + |
| 36 | + JsonNode? node = JsonNode.Parse(text); |
| 37 | + if (node is not JsonObject config) |
| 38 | + return RepoLocalTelemetryConfigInspection.InvalidJson(path); |
| 39 | + |
| 40 | + return IsManagedByQueryWatch(config) |
| 41 | + ? RepoLocalTelemetryConfigInspection.QueryWatchManaged(path, config) |
| 42 | + : RepoLocalTelemetryConfigInspection.NotQueryWatchManaged(path, config); |
| 43 | + } |
| 44 | + catch (JsonException) { |
| 45 | + return RepoLocalTelemetryConfigInspection.InvalidJson(path); |
| 46 | + } |
| 47 | + catch { |
| 48 | + return RepoLocalTelemetryConfigInspection.Unreadable(path); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + internal static JsonObject CreateNewManagedOptOutConfig() { |
| 53 | + return new JsonObject { |
| 54 | + ["disabled"] = true, |
| 55 | + [ManagedByPropertyName] = ManagedByQwatchValue |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + internal static void ApplyManagedOptOut(JsonObject config) { |
| 60 | + RemovePropertyCaseInsensitive(config, "disabled"); |
| 61 | + RemovePropertyCaseInsensitive(config, ManagedByPropertyName); |
| 62 | + config["disabled"] = true; |
| 63 | + config[ManagedByPropertyName] = ManagedByQwatchValue; |
| 64 | + } |
| 65 | + |
| 66 | + internal static void NormalizeManagedMarker(JsonObject config) { |
| 67 | + if (!IsManagedByQueryWatch(config)) |
| 68 | + return; |
| 69 | + |
| 70 | + RemovePropertyCaseInsensitive(config, ManagedByPropertyName); |
| 71 | + config[ManagedByPropertyName] = ManagedByQwatchValue; |
| 72 | + } |
| 73 | + |
| 74 | + internal static bool IsManagedByQueryWatch(JsonObject config) { |
| 75 | + if (!TryGetPropertyNameCaseInsensitive(config, ManagedByPropertyName, out string? propertyName)) |
| 76 | + return false; |
| 77 | + |
| 78 | + return config[propertyName!] is JsonValue value |
| 79 | + && value.TryGetValue(out string? managedBy) |
| 80 | + && string.Equals(managedBy, ManagedByQwatchValue, StringComparison.Ordinal); |
| 81 | + } |
| 82 | + |
| 83 | + internal static bool TryGetPropertyNameCaseInsensitive(JsonObject obj, string name, out string? propertyName) { |
| 84 | + KeyValuePair<string, JsonNode?> property = obj |
| 85 | + .FirstOrDefault(property => property.Key.Equals(name, StringComparison.OrdinalIgnoreCase)); |
| 86 | + if (property.Key is not null) { |
| 87 | + propertyName = property.Key; |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + propertyName = null; |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + internal static void RemovePropertyCaseInsensitive(JsonObject obj, string name) { |
| 96 | + if (TryGetPropertyNameCaseInsensitive(obj, name, out string? propertyName)) |
| 97 | + obj.Remove(propertyName!); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + internal interface IRepoLocalTelemetryConfigFileAccess { |
| 102 | + RepoLocalTelemetryConfigFileMetadata GetMetadata(string path); |
| 103 | + string ReadAllText(string path); |
| 104 | + } |
| 105 | + |
| 106 | + internal readonly record struct RepoLocalTelemetryConfigFileMetadata(bool Exists, long Length); |
| 107 | + |
| 108 | + internal sealed class RepoLocalTelemetryConfigFileAccess : IRepoLocalTelemetryConfigFileAccess { |
| 109 | + public RepoLocalTelemetryConfigFileMetadata GetMetadata(string path) { |
| 110 | + FileInfo fileInfo = new(path); |
| 111 | + return new RepoLocalTelemetryConfigFileMetadata(fileInfo.Exists, fileInfo.Exists ? fileInfo.Length : 0); |
| 112 | + } |
| 113 | + |
| 114 | + public string ReadAllText(string path) { |
| 115 | + return File.ReadAllText(path); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + internal enum RepoLocalTelemetryConfigStateKind { |
| 120 | + Missing = 0, |
| 121 | + QueryWatchManaged = 1, |
| 122 | + NotQueryWatchManaged = 2, |
| 123 | + Unreadable = 3, |
| 124 | + InvalidJson = 4, |
| 125 | + TooLarge = 5 |
| 126 | + } |
| 127 | + |
| 128 | + internal sealed class RepoLocalTelemetryConfigInspection { |
| 129 | + private RepoLocalTelemetryConfigInspection(string path, RepoLocalTelemetryConfigStateKind stateKind, JsonObject? config) { |
| 130 | + Path = path; |
| 131 | + StateKind = stateKind; |
| 132 | + Config = config; |
| 133 | + } |
| 134 | + |
| 135 | + internal string Path { get; } |
| 136 | + internal RepoLocalTelemetryConfigStateKind StateKind { get; } |
| 137 | + internal JsonObject? Config { get; } |
| 138 | + |
| 139 | + internal static RepoLocalTelemetryConfigInspection Missing(string path) { |
| 140 | + return new RepoLocalTelemetryConfigInspection(path, RepoLocalTelemetryConfigStateKind.Missing, config: null); |
| 141 | + } |
| 142 | + |
| 143 | + internal static RepoLocalTelemetryConfigInspection QueryWatchManaged(string path, JsonObject config) { |
| 144 | + return new RepoLocalTelemetryConfigInspection(path, RepoLocalTelemetryConfigStateKind.QueryWatchManaged, config); |
| 145 | + } |
| 146 | + |
| 147 | + internal static RepoLocalTelemetryConfigInspection NotQueryWatchManaged(string path, JsonObject config) { |
| 148 | + return new RepoLocalTelemetryConfigInspection(path, RepoLocalTelemetryConfigStateKind.NotQueryWatchManaged, config); |
| 149 | + } |
| 150 | + |
| 151 | + internal static RepoLocalTelemetryConfigInspection Unreadable(string path) { |
| 152 | + return new RepoLocalTelemetryConfigInspection(path, RepoLocalTelemetryConfigStateKind.Unreadable, config: null); |
| 153 | + } |
| 154 | + |
| 155 | + internal static RepoLocalTelemetryConfigInspection InvalidJson(string path) { |
| 156 | + return new RepoLocalTelemetryConfigInspection(path, RepoLocalTelemetryConfigStateKind.InvalidJson, config: null); |
| 157 | + } |
| 158 | + |
| 159 | + internal static RepoLocalTelemetryConfigInspection TooLarge(string path) { |
| 160 | + return new RepoLocalTelemetryConfigInspection(path, RepoLocalTelemetryConfigStateKind.TooLarge, config: null); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments