-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathProgram.cs
More file actions
94 lines (78 loc) · 2.78 KB
/
Program.cs
File metadata and controls
94 lines (78 loc) · 2.78 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
using System;
using System.Threading.Tasks;
using DocoptNet;
using Seq.Api;
using Seq.Api.Model.Signals;
using System.Collections.Generic;
const string usage = @"signal-copy: copy Seq signals from one server to another.
Usage:
signal-copy.exe <src> <dst> [--srckey=<sk>] [--dstkey=<dk>]
signal-copy.exe (-h | --help)
Options:
-h --help Show this screen.
--srckey=<sk> Source server API key.
--dstkey=<dk> Destination server API key.
";
try
{
var arguments = new Docopt().Apply(usage, args, version: "Seq Signal Copy 0.1", exit: true)!;
var src = arguments["<src>"].ToString();
var dst = arguments["<dst>"].ToString();
var srcKey = Normalize(arguments["--srckey"]);
var dstKey = Normalize(arguments["--dstkey"]);
await Run(src, srcKey, dst, dstKey);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("signal-copy: {0}", ex);
Console.ResetColor();
Environment.Exit(-1);
}
static string? Normalize(ValueObject? v)
{
if (v == null) return null;
var s = v.ToString();
return string.IsNullOrWhiteSpace(s) ? null : s;
}
static async Task Run(string src, string? srcKey, string dst, string? dstKey)
{
var srcConnection = new SeqConnection(src, srcKey);
var dstConnection = new SeqConnection(dst, dstKey);
var dstSignals = new Dictionary<string, SignalEntity>();
foreach (var dstSignal in await dstConnection.Signals.ListAsync())
{
if (dstSignals.ContainsKey(dstSignal.Title))
{
Console.WriteLine($"The destination server has more than one signal named '{dstSignal.Title}'; only one copy of the signal will be updated.");
continue;
}
dstSignals.Add(dstSignal.Title, dstSignal);
}
var count = 0;
foreach (var signal in await srcConnection.Signals.ListAsync())
{
if (dstSignals.TryGetValue(signal.Title, out var target))
{
if (target.IsProtected)
{
Console.WriteLine($"Skipping restricted signal '{signal.Title}' ({target.Id})");
continue;
}
Console.WriteLine($"Updating existing signal '{signal.Title}' ({target.Id})");
}
else
{
Console.WriteLine($"Creating new signal '{signal.Title}'");
target = await dstConnection.Signals.TemplateAsync();
}
target.Title = signal.Title;
target.Filters = signal.Filters;
target.Columns = signal.Columns;
target.Description = signal.Description + " (copy)";
await (target.Id != null ? dstConnection.Signals.UpdateAsync(target) : dstConnection.Signals.AddAsync(target));
++count;
}
Console.WriteLine($"Done, {count} signals updated.");
}