-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathConfigCommand.cs
More file actions
101 lines (89 loc) · 3.27 KB
/
ConfigCommand.cs
File metadata and controls
101 lines (89 loc) · 3.27 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
95
96
97
98
99
100
101
// Copyright 2018-2021 Datalust Pty Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using SeqCli.Config;
using SeqCli.Util;
using Serilog;
namespace SeqCli.Cli.Commands;
[Command("config", "View and set fields in the default `SeqCli.json` or environment-specified `SEQCLI_CONFIG_FILE` file; run with no arguments to list all fields")]
class ConfigCommand : Command
{
string? _key, _value;
bool _clear;
public ConfigCommand()
{
Options.Add("k|key=", "The field, for example `connection.serverUrl`", k => _key = k);
Options.Add("v|value=", "The field value; if not specified, the command will print the current value", v => _value = v);
Options.Add("c|clear", "Clear the field", _ => _clear = true);
}
protected override Task<int> Run()
{
var verb = "read";
try
{
var config = SeqCliConfig.ReadFromFile(RuntimeConfigurationLoader.SeqCliConfigFilename());
if (_key != null)
{
if (_clear)
{
verb = "clear";
KeyValueSettings.Clear(config, _key);
SeqCliConfig.WriteToFile(config, RuntimeConfigurationLoader.SeqCliConfigFilename());
}
else if (_value != null)
{
verb = "update";
KeyValueSettings.Set(config, _key, _value);
SeqCliConfig.WriteToFile(config, RuntimeConfigurationLoader.SeqCliConfigFilename());
}
else
{
verb = "print";
Print(config, _key);
}
}
else
{
List(config);
}
return Task.FromResult(0);
}
catch (Exception ex)
{
Log.Error(ex, "Could not {Verb} config: {ErrorMessage}", verb, Presentation.FormattedMessage(ex));
return Task.FromResult(1);
}
}
static void Print(SeqCliConfig config, string key)
{
if (config == null) throw new ArgumentNullException(nameof(config));
if (key == null) throw new ArgumentNullException(nameof(key));
if (!KeyValueSettings.TryGetValue(config, key, out var value, out _))
throw new ArgumentException($"Option {key} not found");
Console.WriteLine(value);
}
static void List(SeqCliConfig config)
{
foreach (var (key, value, _) in KeyValueSettings.Inspect(config))
{
Console.WriteLine($"{key}={value}");
}
}
}