-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathCreateCommand.cs
More file actions
62 lines (53 loc) · 1.72 KB
/
CreateCommand.cs
File metadata and controls
62 lines (53 loc) · 1.72 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
using System;
using System.Threading.Tasks;
using SeqCli.Config;
using SeqCli.Util;
using Serilog;
namespace SeqCli.Cli.Commands.Profile;
[Command("profile", "create", "Create or replace a connection profile",
Example = "seqcli profile create -n Production -s https://seq.example.com -a th15ISanAPIk3y")]
class CreateCommand : Command
{
string? _url, _apiKey, _name;
public CreateCommand()
{
Options.Add("n=|name=",
"The name of the connection profile",
v => _name = ArgumentString.Normalize(v));
Options.Add("s=|server=",
"The URL of the Seq server",
v => _url = ArgumentString.Normalize(v));
Options.Add("a=|apikey=",
"The API key to use when connecting to the server, if required",
v => _apiKey = ArgumentString.Normalize(v));
}
protected override Task<int> Run()
{
return Task.FromResult(RunSync());
}
int RunSync()
{
if (_name == null)
{
Log.Error("A profile name is required");
return 1;
}
if (_url == null)
{
Log.Error("A server URL is required");
return 1;
}
try
{
var config = SeqCliConfig.ReadFromFile(RuntimeConfigurationLoader.SeqCliConfigFilename());
config.Profiles[_name] = new SeqCliConnectionConfig { ServerUrl = _url, ApiKey = _apiKey };
SeqCliConfig.WriteToFile(config, RuntimeConfigurationLoader.SeqCliConfigFilename());
return 0;
}
catch (Exception ex)
{
Log.Error("Could not create profile: {ErrorMessage}", Presentation.FormattedMessage(ex));
return 1;
}
}
}