-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationManager.cs
More file actions
151 lines (132 loc) · 4.35 KB
/
ConfigurationManager.cs
File metadata and controls
151 lines (132 loc) · 4.35 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace NodaStack
{
public class ConfigurationManager
{
private const string CONFIG_FILE = "nodastack-config.json";
private string configPath;
public NodaStackConfiguration Configuration { get; private set; } = new NodaStackConfiguration();
public ConfigurationManager()
{
// Use AppData/Roaming to ensure write permissions even when installed in Program Files
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var configDir = Path.Combine(appData, "NodaStack");
if (!Directory.Exists(configDir))
{
Directory.CreateDirectory(configDir);
}
configPath = Path.Combine(configDir, CONFIG_FILE);
LoadConfiguration();
}
public void LoadConfiguration()
{
try
{
if (File.Exists(configPath))
{
var jsonContent = File.ReadAllText(configPath);
Configuration = JsonSerializer.Deserialize<NodaStackConfiguration>(jsonContent) ?? new NodaStackConfiguration();
}
else
{
Configuration = new NodaStackConfiguration();
SaveConfiguration();
}
}
catch (Exception)
{
Configuration = new NodaStackConfiguration();
SaveConfiguration();
}
}
public void SaveConfiguration()
{
try
{
var options = new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var jsonContent = JsonSerializer.Serialize(Configuration, options);
File.WriteAllText(configPath, jsonContent);
}
catch (Exception)
{
Console.WriteLine("Error saving configuration. Please check file permissions or disk space.");
}
}
public NodaStackConfiguration GetConfiguration()
{
return Configuration;
}
public void UpdatePorts(Dictionary<string, int> newPorts)
{
foreach (var kvp in newPorts)
{
if (Configuration.Ports.ContainsKey(kvp.Key))
{
Configuration.Ports[kvp.Key] = kvp.Value;
}
}
SaveConfiguration();
}
public void UpdateVersions(Dictionary<string, string> newVersions)
{
foreach (var kvp in newVersions)
{
if (Configuration.Versions.ContainsKey(kvp.Key))
{
Configuration.Versions[kvp.Key] = kvp.Value;
}
}
SaveConfiguration();
}
public void UpdateSettings(NodaStackSettings newSettings)
{
Configuration.Settings = newSettings;
SaveConfiguration();
}
public void UpdateNgrokToken(string token)
{
Configuration.NgrokAuthToken = token;
SaveConfiguration();
}
public void ResetToDefaults()
{
Configuration = new NodaStackConfiguration();
SaveConfiguration();
}
public bool IsPortAvailable(int port)
{
try
{
using (var tcpClient = new System.Net.Sockets.TcpClient())
{
tcpClient.Connect("127.0.0.1", port);
return false;
}
}
catch (System.Net.Sockets.SocketException)
{
return true;
}
}
public List<int> GetAvailablePorts(int startPort = 8000, int endPort = 9000)
{
var availablePorts = new List<int>();
for (int port = startPort; port <= endPort; port++)
{
if (IsPortAvailable(port))
{
availablePorts.Add(port);
}
}
return availablePorts;
}
}
}