Repository navigation
Expand file tree
/
Copy pathcli.rs
More file actions
153 lines (131 loc) · 4.71 KB
/
Copy pathcli.rs
File metadata and controls
153 lines (131 loc) · 4.71 KB
1
2
3
4
8000
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
use std::path::PathBuf;
use structopt::{clap::AppSettings, StructOpt};
use vector::{generate, list, unit_test, validate};
#[derive(StructOpt, Debug)]
#[structopt(rename_all = "kebab-case")]
pub struct Opts {
#[structopt(flatten)]
pub root: RootOpts,
#[structopt(subcommand)]
pub sub_command: Option<SubCommand>,
}
impl Opts {
pub fn get_matches() -> Self {
let version = vector::get_version();
let app = Opts::clap().version(version.as_str()).global_settings(&[
AppSettings::ColoredHelp,
AppSettings::InferSubcommands,
AppSettings::DeriveDisplayOrder,
]);
Opts::from_clap(&app.get_matches())
}
pub fn log_level(&self) -> &'static str {
let (quiet_level, verbose_level) = match self.sub_command {
Some(SubCommand::Validate(_)) | Some(SubCommand::Generate(_)) => {
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(StructOpt, Debug)]
#[structopt(rename_all = "kebab-case")]
pub struct RootOpts {
/// Read configuration from one or more files. Wildcard paths are supported.
/// If zero files are specified the default config path
/// `/etc/vector/vector.toml` will be targeted.
#[structopt(name = "config", short, long)]
pub config_paths: Vec<PathBuf>,
/// Exit on startup if any sinks fail healthchecks
#[structopt(short, long)]
pub require_healthy: bool,
/// Number of threads to use for processing (default is number of available cores)
#[structopt(short, long)]
pub threads: Option<usize>,
/// Enable more detailed internal logging. Repeat to increase level. Overridden by `--quiet`.
#[structopt(short, long, parse(from_occurrences))]
pub verbose: u8,
/// Reduce detail of internal logging. Repeat to reduce further. Overrides `--verbose`.
#[structopt(short, long, parse(from_occurrences))]
pub quiet: u8,
/// Set the logging format
#[structopt(long, default_value = "text", possible_values = &["text", "json"])]
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.
#[structopt(long, default_value = "auto", possible_values = &["auto", "always", "never"])]
pub color: Color,
/// Watch for changes in configuration file, and reload accordingly.
#[structopt(short, long)]
pub watch_config: bool,
}
#[derive(StructOpt, Debug)]
#[structopt(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),
/// 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/docs/setup/guides/unit-testing/
Test(unit_test::Opts),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Color {
Auto,
Always,
Never,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LogFormat {
Text,
Json,
}
impl std::str::FromStr for Color {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"auto" => Ok(Color::Auto),
"always" => Ok(Color::Always),
"never" => Ok(Color::Never),
s => Err(format!(
"{} is not a valid option, expected `auto`, `always` or `never`",
s
)),
}
}
}
impl std::str::FromStr for LogFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"text" => Ok(LogFormat::Text),
"json" => Ok(LogFormat::Json),
s => Err(format!(
"{} is not a valid option, expected `text` or `json`",
s
)),
}
}
}