blob: fcb507240b9c700f81e94840b74f383e253d0b97 (
plain) (
blame)
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
|
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using ArcaneLibs;
using ArcaneLibs.Extensions;
namespace ModerationClient.Services;
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public record CommandLineConfiguration {
public static CommandLineConfiguration FromProcessArgs() {
// logger.LogInformation("Command line arguments: " + string.Join(", ", Environment.GetCommandLineArgs()));
CommandLineConfiguration cfg = FromSerialised(Environment.GetCommandLineArgs());
if (string.IsNullOrWhiteSpace(cfg.ProfileDirectory))
cfg = cfg with {
ProfileDirectory = cfg.IsTemporary
? Directory.CreateTempSubdirectory("ModerationClient-tmp").FullName
: Util.ExpandPath($"$HOME/.local/share/ModerationClient/{cfg.Profile}")
};
// logger.LogInformation("Profile directory: " + cfg.ProfileDirectory);
Directory.CreateDirectory(cfg.ProfileDirectory);
if (!string.IsNullOrWhiteSpace(cfg.LoginData)) {
File.WriteAllText(Path.Combine(cfg.ProfileDirectory, "login.json"), cfg.LoginData);
}
return cfg;
}
public string[] Serialise() {
var current = FromProcessArgs();
List<string> args = new();
if (Profile != current.Profile) args.AddRange(["--profile", Profile]);
if (IsTemporary) args.Add("--temporary");
if (Math.Abs(Scale - 1f) > float.Epsilon) args.AddRange(["--scale", Scale.ToString(CultureInfo.InvariantCulture)]);
if (ProfileDirectory != current.ProfileDirectory) args.AddRange(["--profile-dir", ProfileDirectory]);
if (!string.IsNullOrWhiteSpace(_loginData) && _loginData != current.LoginData) args.AddRange(["--login-data", _loginData!]);
if (TestConfiguration is not null && TestConfiguration != current.TestConfiguration) args.AddRange(["--test-config", TestConfiguration!.ToJson()]);
Console.WriteLine("Serialised CommandLineConfiguration: " + string.Join(" ", args));
return args.ToArray();
}
public static CommandLineConfiguration FromSerialised(string[] args) {
CommandLineConfiguration cfg = new();
for (var i = 0; i < args.Length; i++) {
switch (args[i]) {
case "--profile":
case "-p":
cfg = cfg with { Profile = args[++i] };
break;
case "--temporary":
cfg = cfg with { IsTemporary = true };
break;
case "--profile-dir":
cfg = cfg with { ProfileDirectory = args[++i] };
break;
case "--scale":
cfg = cfg with { Scale = float.Parse(args[++i]) };
break;
case "--login-data":
cfg = cfg with { LoginData = args[++i] };
break;
case "--test-config":
cfg = cfg with { testConfiguration = args[++i] };
break;
}
}
return cfg;
}
private readonly string? _loginData;
public string Profile { get; init; } = "default";
public bool IsTemporary { get; init; }
public string ProfileDirectory { get; init; }
public float Scale { get; init; } = 1f;
public string? LoginData {
get => _loginData;
init {
Console.WriteLine("Setting login data: " + value);
_loginData = value;
}
}
[JsonIgnore]
private string? testConfiguration {
get => TestConfiguration?.ToJson();
init => TestConfiguration = value is null ? null : JsonSerializer.Deserialize<TestConfig>(value);
}
[JsonIgnore]
public TestConfig? TestConfiguration { get; init; }
public class TestConfig {
public List<string> Mxids { get; set; } = new();
}
}
|