/ src / modules / cmdpal / ext / Microsoft.CmdPal.Ext.Bookmark / Persistence / FileBookmarkDataSource.cs
FileBookmarkDataSource.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System.IO;
 6  
 7  namespace Microsoft.CmdPal.Ext.Bookmarks.Persistence;
 8  
 9  public sealed partial class FileBookmarkDataSource : IBookmarkDataSource
10  {
11      private readonly string _filePath;
12  
13      public FileBookmarkDataSource(string filePath)
14      {
15          _filePath = filePath;
16      }
17  
18      public string GetBookmarkData()
19      {
20          if (!File.Exists(_filePath))
21          {
22              return string.Empty;
23          }
24  
25          try
26          {
27              return File.ReadAllText(_filePath);
28          }
29          catch (Exception ex)
30          {
31              ExtensionHost.LogMessage($"Read bookmark data failed. ex: {ex.Message}");
32              return string.Empty;
33          }
34      }
35  
36      public void SaveBookmarkData(string jsonData)
37      {
38          try
39          {
40              File.WriteAllText(_filePath, jsonData);
41          }
42          catch (Exception ex)
43          {
44              ExtensionHost.LogMessage($"Failed to save bookmark data: {ex}");
45          }
46      }
47  }