From 3e74ff541f60d34845b33d424aec86132ac78bb4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 18 Nov 2021 07:29:12 -0600 Subject: add endpoint for yaml validation --- .../API/Controllers/ConfigurationController.cs | 37 ++++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'src/slskd/Core/API') diff --git a/src/slskd/Core/API/Controllers/ConfigurationController.cs b/src/slskd/Core/API/Controllers/ConfigurationController.cs index 7fd02fca..08d84c0a 100644 --- a/src/slskd/Core/API/Controllers/ConfigurationController.cs +++ b/src/slskd/Core/API/Controllers/ConfigurationController.cs @@ -80,17 +80,25 @@ namespace slskd.Core.API return Forbid(); } - try + if (!TryValidateYaml(yaml, out var error)) { - _ = yaml.FromYaml(); + Logger.Error(error, "Failed to validate YAML configuration"); + return BadRequest(error); } - catch (Exception ex) + + IOFile.WriteAllText(Program.ConfigurationFile, yaml); + return Ok(); + } + + [HttpPost] + [Route("yaml/validate")] + public IActionResult ValidateYamlFile([FromBody] string yaml) + { + if (!TryValidateYaml(yaml, out var error)) { - Logger.Error(ex, "Failed to validate YAML configuration"); - return BadRequest($"{ex.Message}: {ex.InnerException.Message}"); + return BadRequest(error); } - IOFile.WriteAllText(Program.ConfigurationFile, yaml); return Ok(); } @@ -108,5 +116,22 @@ namespace slskd.Core.API return Ok(); } + + private bool TryValidateYaml(string yaml, out string error) + { + error = null; + + try + { + _ = yaml.FromYaml(); + } + catch (Exception ex) + { + error = $"{ex.Message}: {ex.InnerException.Message}"; + return false; + } + + return true; + } } } -- cgit v1.2.3