67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
using System.Text.Json;
|
|
|
|
namespace ScadaLink.CLI;
|
|
|
|
public class CliConfig
|
|
{
|
|
public List<string> ContactPoints { get; set; } = new();
|
|
public string? LdapServer { get; set; }
|
|
public int LdapPort { get; set; } = 636;
|
|
public bool LdapUseTls { get; set; } = true;
|
|
public string DefaultFormat { get; set; } = "json";
|
|
|
|
public static CliConfig Load()
|
|
{
|
|
var config = new CliConfig();
|
|
|
|
// Load from config file
|
|
var configPath = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
|
|
".scadalink", "config.json");
|
|
if (File.Exists(configPath))
|
|
{
|
|
var json = File.ReadAllText(configPath);
|
|
var fileConfig = JsonSerializer.Deserialize<CliConfigFile>(json,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
if (fileConfig != null)
|
|
{
|
|
if (fileConfig.ContactPoints?.Count > 0) config.ContactPoints = fileConfig.ContactPoints;
|
|
if (fileConfig.Ldap != null)
|
|
{
|
|
config.LdapServer = fileConfig.Ldap.Server;
|
|
config.LdapPort = fileConfig.Ldap.Port;
|
|
config.LdapUseTls = fileConfig.Ldap.UseTls;
|
|
}
|
|
if (!string.IsNullOrEmpty(fileConfig.DefaultFormat)) config.DefaultFormat = fileConfig.DefaultFormat;
|
|
}
|
|
}
|
|
|
|
// Override from environment variables
|
|
var envContacts = Environment.GetEnvironmentVariable("SCADALINK_CONTACT_POINTS");
|
|
if (!string.IsNullOrEmpty(envContacts))
|
|
config.ContactPoints = envContacts.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
|
|
|
|
var envLdap = Environment.GetEnvironmentVariable("SCADALINK_LDAP_SERVER");
|
|
if (!string.IsNullOrEmpty(envLdap)) config.LdapServer = envLdap;
|
|
|
|
var envFormat = Environment.GetEnvironmentVariable("SCADALINK_FORMAT");
|
|
if (!string.IsNullOrEmpty(envFormat)) config.DefaultFormat = envFormat;
|
|
|
|
return config;
|
|
}
|
|
|
|
private class CliConfigFile
|
|
{
|
|
public List<string>? ContactPoints { get; set; }
|
|
public LdapConfig? Ldap { get; set; }
|
|
public string? DefaultFormat { get; set; }
|
|
}
|
|
|
|
private class LdapConfig
|
|
{
|
|
public string? Server { get; set; }
|
|
public int Port { get; set; } = 636;
|
|
public bool UseTls { get; set; } = true;
|
|
}
|
|
}
|