forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.rs
247 lines (212 loc) · 7.77 KB
/
cli.rs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use std::path::PathBuf;
use clap::{ArgAction, CommandFactory, FromArgMatches, Parser};
#[cfg(windows)]
use crate::service;
#[cfg(feature = "api-client")]
use crate::tap;
#[cfg(feature = "api-client")]
use crate::top;
use crate::{config, generate, get_version, graph, list, unit_test, validate};
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
pub struct Opts {
#[command(flatten)]
pub root: RootOpts,
#[command(subcommand)]
pub sub_command: Option<SubCommand>,
}
impl Opts {
pub fn get_matches() -> Result<Self, clap::Error> {
let version = get_version();
let app = Opts::command().version(version);
Opts::from_arg_matches(&app.get_matches())
}
pub const fn log_level(&self) -> &'static str {
let (quiet_level, verbose_level) = match self.sub_command {
Some(SubCommand::Validate(_))
| Some(SubCommand::Graph(_))
| Some(SubCommand::Generate(_))
| Some(SubCommand::List(_))
| Some(SubCommand::Test(_)) => {
if self.root.verbose == 0 {
(self.root.quiet + 1, self.root.verbose)
} else {
(self.root.quiet, self.root.verbose - 1)
}
}
_ => (self.root.quiet, self.root.verbose),
};
match quiet_level {
0 => match verbose_level {
0 => "info",
1 => "debug",
2..=255 => "trace",
},
1 => "warn",
2 => "error",
3..=255 => "off",
}
}
}
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
pub struct RootOpts {
/// Read configuration from one or more files. Wildcard paths are supported.
/// File format is detected from the file name.
/// If zero files are specified the default config path
/// `/etc/vector/vector.toml` will be targeted.
#[arg(
id = "config",
short,
long,
env = "VECTOR_CONFIG",
value_delimiter(',')
)]
pub config_paths: Vec<PathBuf>,
/// Read configuration from files in one or more directories.
/// File format is detected from the file name.
///
/// Files not ending in .toml, .json, .yaml, or .yml will be ignored.
#[arg(
id = "config-dir",
short = 'C',
long,
env = "VECTOR_CONFIG_DIR",
value_delimiter(',')
)]
pub config_dirs: Vec<PathBuf>,
/// Read configuration from one or more files. Wildcard paths are supported.
/// TOML file format is expected.
#[arg(
id = "config-toml",
long,
env = "VECTOR_CONFIG_TOML",
value_delimiter(',')
)]
pub config_paths_toml: Vec<PathBuf>,
/// Read configuration from one or more files. Wildcard paths are supported.
/// JSON file format is expected.
#[arg(
id = "config-json",
long,
env = "VECTOR_CONFIG_JSON",
value_delimiter(',')
)]
pub config_paths_json: Vec<PathBuf>,
/// Read configuration from one or more files. Wildcard paths are supported.
/// YAML file format is expected.
#[arg(
id = "config-yaml",
long,
env = "VECTOR_CONFIG_YAML",
value_delimiter(',')
)]
pub config_paths_yaml: Vec<PathBuf>,
/// Exit on startup if any sinks fail healthchecks
#[arg(short, long, env = "VECTOR_REQUIRE_HEALTHY")]
pub require_healthy: Option<bool>,
/// Number of threads to use for processing (default is number of available cores)
#[arg(short, long, env = "VECTOR_THREADS")]
pub threads: Option<usize>,
/// Enable more detailed internal logging. Repeat to increase level. Overridden by `--quiet`.
#[arg(short, long, action = ArgAction::Count)]
pub verbose: u8,
/// Reduce detail of internal logging. Repeat to reduce further. Overrides `--verbose`.
#[arg(short, long, action = ArgAction::Count)]
pub quiet: u8,
/// Set the logging format
#[arg(long, default_value = "text", env = "VECTOR_LOG_FORMAT")]
pub log_format: LogFormat,
/// Control when ANSI terminal formatting is used.
///
/// By default `vector` will try and detect if `stdout` is a terminal, if it is
/// ANSI will be enabled. Otherwise it will be disabled. By providing this flag with
/// the `--color always` option will always enable ANSI terminal formatting. `--color never`
/// will disable all ANSI terminal formatting. `--color auto` will attempt
/// to detect it automatically.
#[arg(long, default_value = "auto", env = "VECTOR_COLOR")]
pub color: Color,
/// Watch for changes in configuration file, and reload accordingly.
#[arg(short, long, env = "VECTOR_WATCH_CONFIG")]
pub watch_config: bool,
/// Set the internal log rate limit
#[arg(
short,
long,
env = "VECTOR_INTERNAL_LOG_RATE_LIMIT",
default_value = "10"
)]
pub internal_log_rate_limit: u64,
}
impl RootOpts {
/// Return a list of config paths with the associated formats.
pub fn config_paths_with_formats(&self) -> Vec<config::ConfigPath> {
config::merge_path_lists(vec![
(&self.config_paths, None),
(&self.config_paths_toml, Some(config::Format::Toml)),
(&self.config_paths_json, Some(config::Format::Json)),
(&self.config_paths_yaml, Some(config::Format::Yaml)),
])
.map(|(path, hint)| config::ConfigPath::File(path, hint))
.chain(
self.config_dirs
.iter()
.map(|dir| config::ConfigPath::Dir(dir.to_path_buf())),
)
.collect()
}
}
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
pub enum SubCommand {
/// Validate the target config, then exit.
Validate(validate::Opts),
/// Generate a Vector configuration containing a list of components.
Generate(generate::Opts),
/// Generate the configuration schema for this version of Vector. (experimental)
///
/// A JSON Schema document will be written to stdout that represents the valid schema for a
/// Vector configuration. This schema is based on the "full" configuration, such that for usages
/// where a configuration is split into multiple files, the schema would apply to those files
/// only when concatentated together.
GenerateSchema,
/// Output a provided Vector configuration file/dir as a single JSON object, useful for checking in to version control.
#[command(hide = true)]
Config(config::Opts),
/// List available components, then exit.
List(list::Opts),
/// Run Vector config unit tests, then exit. This command is experimental and therefore subject to change.
/// For guidance on how to write unit tests check out <https://vector.dev/guides/level-up/unit-testing/>.
Test(unit_test::Opts),
/// Output the topology as visual representation using the DOT language which can be rendered by GraphViz
Graph(graph::Opts),
/// Display topology and metrics in the console, for a local or remote Vector instance
#[cfg(feature = "api-client")]
Top(top::Opts),
/// Observe output log events from source or transform components. Logs are sampled at a specified interval.
#[cfg(feature = "api-client")]
Tap(tap::Opts),
/// Manage the vector service.
#[cfg(windows)]
Service(service::Opts),
/// Vector Remap Language CLI
#[cfg(feature = "vrl-cli")]
Vrl(vrl_cli::Opts),
}
#[derive(clap::ValueEnum, Debug, Clone, PartialEq, Eq)]
pub enum Color {
Auto,
Always,
Never,
}
#[derive(clap::ValueEnum, Debug, Clone, PartialEq, Eq)]
pub enum LogFormat {
Text,
Json,
}
pub fn handle_config_errors(errors: Vec<String>) -> exitcode::ExitCode {
for error in errors {
error!(message = "Configuration error.", %error);
}
exitcode::CONFIG
}