YamlFileComponent.razor 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. @using System.Collections.Specialized
  2. @using System.Text
  3. @using YamlDotNet.Core
  4. @using RackPeek.Domain.Persistence
  5. @using RackPeek.Domain.Persistence.Yaml
  6. @using RackPeek.Domain.Resources
  7. @using RackPeek.Domain.Resources.AccessPoints
  8. @using RackPeek.Domain.Resources.Desktops
  9. @using RackPeek.Domain.Resources.Firewalls
  10. @using RackPeek.Domain.Resources.Laptops
  11. @using RackPeek.Domain.Resources.Servers
  12. @using RackPeek.Domain.Resources.Switches
  13. @using RackPeek.Domain.Resources.SystemResources
  14. @using YamlDotNet.Serialization
  15. @using YamlDotNet.Serialization.NamingConventions
  16. @using Router = RackPeek.Domain.Resources.Routers.Router
  17. @inject ITextFileStore FileStore
  18. @inject IResourceCollection Resources
  19. <div class="border border-zinc-800 rounded p-4 bg-zinc-900">
  20. <div class="flex justify-between items-center mb-3">
  21. <div class="text-zinc-100">
  22. @Title
  23. </div>
  24. <div class="flex gap-3 text-xs">
  25. @if (!_isEditing)
  26. {
  27. <button class="text-zinc-400 hover:text-zinc-200"
  28. @onclick="BeginEdit">
  29. Edit
  30. </button>
  31. }
  32. else
  33. {
  34. <button class="text-emerald-400 hover:text-emerald-300"
  35. @onclick="Save">
  36. Save
  37. </button>
  38. <button class="text-zinc-500 hover:text-zinc-300"
  39. @onclick="Cancel">
  40. Cancel
  41. </button>
  42. }
  43. </div>
  44. </div>
  45. @if (!_exists)
  46. {
  47. <div class="text-red-400 text-sm">
  48. File does not exist.
  49. </div>
  50. }
  51. else if (_isEditing)
  52. {
  53. <textarea class="w-full input font-mono text-xs"
  54. style="min-height: 40rem"
  55. @bind="_editText">
  56. </textarea>
  57. @if (_error is not null)
  58. {
  59. <div class="mt-3 border border-red-500/40 bg-red-500/10 rounded p-3"
  60. data-testid="yaml-file-error"
  61. role="alert">
  62. <div class="flex items-start gap-2">
  63. <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 flex-shrink-0 text-red-400 mt-0.5"
  64. fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
  65. <path stroke-linecap="round" stroke-linejoin="round"
  66. d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"/>
  67. </svg>
  68. <div class="min-w-0 flex-1">
  69. <div class="text-red-400 text-sm font-semibold">
  70. @_error.Headline
  71. </div>
  72. @if (_error.Line is long line)
  73. {
  74. <div class="text-zinc-400 text-xs mt-1">
  75. Line @line@(_error.Column is long col ? $", column {col}" : "")
  76. </div>
  77. }
  78. @if (!string.IsNullOrEmpty(_error.Snippet))
  79. {
  80. <pre class="mt-2 p-2 rounded bg-zinc-950 border border-zinc-800 text-xs overflow-x-auto text-zinc-300"
  81. data-testid="yaml-file-error-snippet">@_error.Snippet</pre>
  82. }
  83. </div>
  84. </div>
  85. </div>
  86. }
  87. }
  88. else
  89. {
  90. <pre class="text-zinc-300 text-xs whitespace-pre-wrap">@_currentText</pre>
  91. }
  92. </div>
  93. <ConfirmModal
  94. IsOpen="_confirmDeleteOpen"
  95. IsOpenChanged="v => _confirmDeleteOpen = v"
  96. Title="Delete File"
  97. ConfirmText="Delete"
  98. ConfirmClass="bg-red-600 hover:bg-red-500"
  99. OnConfirm="DeleteFile"
  100. TestIdPrefix="File">>
  101. Are you sure you want to delete <strong>@Path</strong>?
  102. </ConfirmModal>
  103. @code {
  104. [Parameter] [EditorRequired] public string Path { get; set; } = default!;
  105. [Parameter] public string Title { get; set; } = "Edit YAML";
  106. [Parameter] public EventCallback<string> OnDeleted { get; set; }
  107. bool _isEditing;
  108. bool _exists;
  109. bool _confirmDeleteOpen;
  110. string _currentText = "";
  111. string _editText = "";
  112. YamlEditError? _error;
  113. protected override async Task OnParametersSetAsync()
  114. {
  115. await Load();
  116. }
  117. async Task Load()
  118. {
  119. _exists = await FileStore.ExistsAsync(Path);
  120. if (!_exists)
  121. return;
  122. _currentText = await FileStore.ReadAllTextAsync(Path);
  123. }
  124. void BeginEdit()
  125. {
  126. _editText = _currentText;
  127. _error = null;
  128. _isEditing = true;
  129. }
  130. void Cancel()
  131. {
  132. _isEditing = false;
  133. _error = null;
  134. }
  135. async Task Save()
  136. {
  137. if (!ValidateYamlRoundTrip(_editText, out var err))
  138. {
  139. _error = err;
  140. return;
  141. }
  142. await FileStore.WriteAllTextAsync(Path, _editText);
  143. await Resources.LoadAsync();
  144. _currentText = _editText;
  145. _isEditing = false;
  146. }
  147. void ConfirmDelete()
  148. {
  149. _confirmDeleteOpen = true;
  150. }
  151. async Task DeleteFile()
  152. {
  153. _confirmDeleteOpen = false;
  154. // if your store supports delete, call it here
  155. await FileStore.WriteAllTextAsync(Path, "");
  156. if (OnDeleted.HasDelegate)
  157. await OnDeleted.InvokeAsync(Path);
  158. }
  159. private bool ValidateYamlRoundTrip(string yaml, out YamlEditError? error)
  160. {
  161. try
  162. {
  163. if (string.IsNullOrWhiteSpace(yaml))
  164. {
  165. error = new YamlEditError("YAML is empty.", null, null, null);
  166. return false;
  167. }
  168. // ---------- DESERIALIZER (same as resource loader) ----------
  169. var deserializer = new DeserializerBuilder()
  170. .WithNamingConvention(CamelCaseNamingConvention.Instance)
  171. .WithCaseInsensitivePropertyMatching()
  172. .WithTypeConverter(new StorageSizeYamlConverter())
  173. .WithTypeDiscriminatingNodeDeserializer(options =>
  174. {
  175. options.AddKeyValueTypeDiscriminator<Resource>("kind", new Dictionary<string, Type>
  176. {
  177. { Server.KindLabel, typeof(Server) },
  178. { Switch.KindLabel, typeof(Switch) },
  179. { Firewall.KindLabel, typeof(Firewall) },
  180. { Router.KindLabel, typeof(Router) },
  181. { Desktop.KindLabel, typeof(Desktop) },
  182. { Laptop.KindLabel, typeof(Laptop) },
  183. { AccessPoint.KindLabel, typeof(AccessPoint) },
  184. { RackPeek.Domain.Resources.UpsUnits.Ups.KindLabel, typeof(RackPeek.Domain.Resources.UpsUnits.Ups) },
  185. { SystemResource.KindLabel, typeof(SystemResource) },
  186. { Service.KindLabel, typeof(Service) }
  187. });
  188. })
  189. .Build();
  190. var root = deserializer.Deserialize<YamlRoot>(yaml);
  191. if (root?.Resources == null)
  192. {
  193. error = new YamlEditError("No resources section found.", null, null, null);
  194. return false;
  195. }
  196. // ---------- SERIALIZE AGAIN ----------
  197. var serializer = new SerializerBuilder()
  198. .WithNamingConvention(CamelCaseNamingConvention.Instance)
  199. .Build();
  200. var payload = new OrderedDictionary
  201. {
  202. ["resources"] = root.Resources
  203. };
  204. var roundTripYaml = serializer.Serialize(payload);
  205. // ---------- DESERIALIZE AGAIN ----------
  206. var root2 = deserializer.Deserialize<YamlRoot>(roundTripYaml);
  207. if (root2?.Resources == null)
  208. {
  209. error = new YamlEditError("Round-trip serialization failed.", null, null, null);
  210. return false;
  211. }
  212. // ---------- DUPLICATE NAME CHECK ----------
  213. var dup = root2.Resources
  214. .GroupBy(r => r.Name, StringComparer.OrdinalIgnoreCase)
  215. .FirstOrDefault(g => g.Count() > 1);
  216. if (dup != null)
  217. {
  218. error = new YamlEditError($"Duplicate resource name: '{dup.Key}'", null, null, null);
  219. return false;
  220. }
  221. error = null;
  222. return true;
  223. }
  224. catch (Exception ex)
  225. {
  226. error = BuildEditError(ex, yaml);
  227. return false;
  228. }
  229. }
  230. private static YamlEditError BuildEditError(Exception ex, string yaml)
  231. {
  232. YamlException? ye = FindYamlException(ex);
  233. if (ye is not null)
  234. {
  235. long? line = ye.Start.Line > 0 ? ye.Start.Line : null;
  236. long? col = ye.Start.Column > 0 ? ye.Start.Column : null;
  237. return new YamlEditError(
  238. $"YAML invalid: {FirstLine(ye.Message)}",
  239. line,
  240. col,
  241. line is long l ? ExtractSnippet(yaml, (int)l) : null);
  242. }
  243. return new YamlEditError($"YAML validation failed: {FirstLine(ex.Message)}", null, null, null);
  244. }
  245. private static YamlException? FindYamlException(Exception? ex)
  246. {
  247. while (ex is not null)
  248. {
  249. if (ex is YamlException ye) return ye;
  250. ex = ex.InnerException;
  251. }
  252. return null;
  253. }
  254. private static string FirstLine(string message)
  255. {
  256. if (string.IsNullOrEmpty(message)) return string.Empty;
  257. var nl = message.IndexOf('\n');
  258. return nl < 0 ? message.Trim() : message[..nl].Trim();
  259. }
  260. private static string ExtractSnippet(string yaml, int lineNumber, int context = 2)
  261. {
  262. var lines = yaml.Replace("\r\n", "\n").Split('\n');
  263. if (lineNumber < 1 || lineNumber > lines.Length) return string.Empty;
  264. var start = Math.Max(1, lineNumber - context);
  265. var end = Math.Min(lines.Length, lineNumber + context);
  266. var sb = new StringBuilder();
  267. for (int i = start; i <= end; i++)
  268. {
  269. sb.Append(i == lineNumber ? "→ " : " ")
  270. .Append(i.ToString().PadLeft(4))
  271. .Append(" ")
  272. .Append(lines[i - 1]);
  273. if (i < end) sb.Append('\n');
  274. }
  275. return sb.ToString();
  276. }
  277. private sealed record YamlEditError(string Headline, long? Line, long? Column, string? Snippet);
  278. }