diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-11-18 07:29:12 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-11-18 07:29:12 -0600 |
| commit | 3e74ff541f60d34845b33d424aec86132ac78bb4 (patch) | |
| tree | ceac968c6ee9a7563048166b05254e2c29933ee6 /src | |
| parent | c4180ef5dce447499e76a1604d2229adbe6a47b4 (diff) | |
| download | slskd-3e74ff541f60d34845b33d424aec86132ac78bb4.tar.gz | |
add endpoint for yaml validation
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/API/Controllers/ConfigurationController.cs | 37 |
1 files changed, 31 insertions, 6 deletions
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<Options>(); + 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<Options>(); + } + catch (Exception ex) + { + error = $"{ex.Message}: {ex.InnerException.Message}"; + return false; + } + + return true; + } } } |
