| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.IO; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | using WireMock.Validation; |
| | | 5 | | |
| | | 6 | | namespace WireMock.Handlers |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Default implementation for a handler to interact with the local file system to read and write static mapping fil |
| | | 10 | | /// </summary> |
| | | 11 | | public class LocalFileSystemHandler : IFileSystemHandler |
| | | 12 | | { |
| | 1 | 13 | | private static readonly string AdminMappingsFolder = Path.Combine("__admin", "mappings"); |
| | | 14 | | |
| | | 15 | | /// <inheritdoc cref="IFileSystemHandler.FolderExists"/> |
| | | 16 | | public bool FolderExists([NotNull] string path) |
| | 3 | 17 | | { |
| | 3 | 18 | | Check.NotNullOrEmpty(path, nameof(path)); |
| | | 19 | | |
| | 3 | 20 | | return Directory.Exists(path); |
| | 3 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc cref="IFileSystemHandler.CreateFolder"/> |
| | | 24 | | public void CreateFolder([NotNull] string path) |
| | 1 | 25 | | { |
| | 1 | 26 | | Check.NotNullOrEmpty(path, nameof(path)); |
| | | 27 | | |
| | 0 | 28 | | Directory.CreateDirectory(path); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc cref="IFileSystemHandler.EnumerateFiles"/> |
| | | 32 | | public IEnumerable<string> EnumerateFiles([NotNull] string path) |
| | 2 | 33 | | { |
| | 2 | 34 | | Check.NotNullOrEmpty(path, nameof(path)); |
| | | 35 | | |
| | 2 | 36 | | return Directory.EnumerateFiles(path); |
| | 2 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc cref="IFileSystemHandler.GetMappingFolder"/> |
| | | 40 | | public string GetMappingFolder() |
| | 1 | 41 | | { |
| | 1 | 42 | | return Path.Combine(Directory.GetCurrentDirectory(), AdminMappingsFolder); |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc cref="IFileSystemHandler.ReadMappingFile"/> |
| | | 46 | | public string ReadMappingFile([NotNull] string path) |
| | 9 | 47 | | { |
| | 9 | 48 | | Check.NotNullOrEmpty(path, nameof(path)); |
| | | 49 | | |
| | 9 | 50 | | return File.ReadAllText(path); |
| | 9 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc cref="IFileSystemHandler.WriteMappingFile"/> |
| | | 54 | | public void WriteMappingFile([NotNull] string path, [NotNull] string text) |
| | 1 | 55 | | { |
| | 1 | 56 | | Check.NotNullOrEmpty(path, nameof(path)); |
| | 0 | 57 | | Check.NotNull(text, nameof(text)); |
| | | 58 | | |
| | 0 | 59 | | File.WriteAllText(path, text); |
| | 0 | 60 | | } |
| | | 61 | | } |
| | | 62 | | } |