This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathJsonConnectionCache.cs
More file actions
113 lines (100 loc) · 3.34 KB
/
Copy pathJsonConnectionCache.cs
File metadata and controls
113 lines (100 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GitHub.Logging;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using Rothko;
using Serilog;
namespace GitHub.VisualStudio
{
/// <summary>
/// Loads and saves the configured connections from/to a .json file.
/// </summary>
[Export(typeof(IConnectionCache))]
public class JsonConnectionCache : IConnectionCache
{
static readonly ILogger log = LogManager.ForContext<JsonConnectionCache>();
const string DefaultCacheFile = "ghfvs.connections";
readonly string cachePath;
readonly IOperatingSystem os;
[ImportingConstructor]
public JsonConnectionCache(IProgram program, IOperatingSystem os)
: this(program, os, DefaultCacheFile)
{
}
public JsonConnectionCache(IProgram program, IOperatingSystem os, string cacheFile)
{
this.os = os;
cachePath = Path.Combine(
os.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData),
program.ApplicationName,
cacheFile);
}
public Task<IEnumerable<ConnectionDetails>> Load()
{
if (os.File.Exists(cachePath))
{
try
{
// TODO: Need a ReadAllTextAsync method here.
var data = os.File.ReadAllText(cachePath, Encoding.UTF8);
var result = SimpleJson.DeserializeObject<CacheData>(data);
return Task.FromResult(result.connections.Select(FromCache));
}
catch (Exception e)
{
try
{
os.File.Delete(cachePath);
}
catch { }
log.Error(e, "Failed to read connection cache from {Path}", cachePath);
}
}
return Task.FromResult(Enumerable.Empty<ConnectionDetails>());
}
public Task Save(IEnumerable<ConnectionDetails> connections)
{
var data = SimpleJson.SerializeObject(new CacheData
{
connections = connections.Select(ToCache).ToList(),
});
try
{
os.File.WriteAllText(cachePath, data);
}
catch (Exception e)
{
log.Error(e, "Failed to write connection cache to {Path}", cachePath);
}
return Task.CompletedTask;
}
static ConnectionDetails FromCache(ConnectionCacheItem c)
{
return new ConnectionDetails(HostAddress.Create(c.HostUrl), c.UserName);
}
static ConnectionCacheItem ToCache(ConnectionDetails c)
{
return new ConnectionCacheItem
{
HostUrl = c.HostAddress.WebUri,
UserName = c.UserName,
};
}
class CacheData
{
public IEnumerable<ConnectionCacheItem> connections { get; set; }
}
class ConnectionCacheItem
{
public Uri HostUrl { get; set; }
public string UserName { get; set; }
}
}
}