-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
108 lines (78 loc) · 3.01 KB
/
Program.cs
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
using DynClient.Plugins;
using DynClient.Retriever;
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Linq;
using System.Text.Json;
namespace DynClient
{
using ConfigType = IDictionary<String, Object>;
class Program
{
static IDictionary<string,object> GetConfig(String path)
{
if(File.Exists(path))
{
String rawConfig = File.ReadAllText(path);
return JsonSerializer.Deserialize<Dictionary<String, object>>(rawConfig);
}
return default;
}
static private void UpdateDns(IProviderPlugin plugin, ConfigType config) => DynClient.UpdateDns(plugin, config);
static public void Execute(FileInfo config, Boolean verbose)
{
PluginLoader loader = new PluginLoader();
var plugins = loader.GetPlugins();
if (verbose)
{
plugins.ForEach(plugin =>
{
Console.WriteLine($"Loaded {plugin.Name} plugin!");
});
Console.WriteLine($"Loaded {plugins.Count} plugins!");
}
if(config != null)
{
if(!File.Exists(config.FullName)) {
Console.WriteLine("The configuration file does not exist!");
return;
}
var configOptions = GetConfig(config.FullName);
if(configOptions.ContainsKey("plugin"))
{
string pluginName = configOptions["plugin"].ToString();
var plugin = plugins
.Where(plugin => plugin.Name.Equals(pluginName))
.FirstOrDefault();
if(plugin == null)
{
Console.WriteLine("The specified plugin was not loaded!");
return;
}
UpdateDns(plugin, configOptions);
} else
{
Console.WriteLine("The configuration file does not specify any plugin!");
}
}
Console.WriteLine($"File: {config?.FullName}, Verbose: {verbose}");
}
static int Main(string[] args)
{
var configOption = new Option<FileInfo>("--config", "Config File Location");
configOption.AddAlias("-c");
var verboseOption = new Option<Boolean>("--verbose", "Verbose");
var rootCommand = new RootCommand
{
configOption,
verboseOption
};
rootCommand.Description = "Yet Another Extensible Dynamic Dns Updater";
rootCommand.Handler = CommandHandler.Create<FileInfo, Boolean>(Execute);
return rootCommand.InvokeAsync(args).Result;
}
}
}