Skip to content

Commit df1ad30

Browse files
committed
imp(cli): initial CLI implementation
1 parent 4e24830 commit df1ad30

File tree

8 files changed

+107
-83
lines changed

8 files changed

+107
-83
lines changed

pyroscope_cli/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ clap_complete = "3.1"
1212
names= "0.13.0"
1313

1414
[dependencies.clap]
15-
version = "3.1"
15+
version = "=3.1.6"
1616
features = ["cargo", "derive"]
1717

1818
[dev-dependencies]

pyroscope_cli/cli/src/lib.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use utils::types::{LogLevel, Spy};
1818
long_about = "Pyroscope CLI",
1919
version
2020
)]
21-
#[clap(setting = AppSettings::SubcommandRequired)]
2221
#[clap(global_setting(AppSettings::DeriveDisplayOrder))]
2322
pub struct Cli {
2423
/// Set a custom config file
@@ -67,7 +66,8 @@ enum Commands {
6766
name = "detect_subprocesses",
6867
long = "detect-subprocesses",
6968
value_name = "DECTECT_SUBPROCESSES",
70-
help = "keep track of and profile subprocesses of the main process"
69+
help = "keep track of and profile subprocesses of the main process",
70+
takes_value = false
7171
)]
7272
detect_subprocesses: bool,
7373
#[clap(
@@ -83,7 +83,8 @@ enum Commands {
8383
name = "no_logging",
8484
long = "no-logging",
8585
value_name = "NO_LOGGING",
86-
help = "disable logging from pyroscope"
86+
help = "disable logging from pyroscope",
87+
takes_value = false
8788
)]
8889
no_logging: bool,
8990
#[clap(
@@ -98,35 +99,40 @@ enum Commands {
9899
name = "rbspy_blocking",
99100
long = "rbspy-blocking",
100101
value_name = "RBSPY_BLOCKING",
101-
help = "enable blocking mode for rbspy"
102+
help = "enable blocking mode for rbspy",
103+
takes_value = false
102104
)]
103105
rbspy_blocking: bool,
104106
#[clap(
105107
name = "pyspy_blocking",
106108
long = "pyspy-blocking",
107109
value_name = "PYSPY_BLOCKING",
108-
help = "enable blocking mode for pyspy"
110+
help = "enable blocking mode for pyspy",
111+
takes_value = false
109112
)]
110113
pyspy_blocking: bool,
111114
#[clap(
112115
name = "pyspy_idle",
113116
long = "pyspy-idle",
114117
value_name = "PYSPY_IDLE",
115-
help = "include idle threads for pyspy"
118+
help = "include idle threads for pyspy",
119+
takes_value = false
116120
)]
117121
pyspy_idle: bool,
118122
#[clap(
119123
name = "pyspy_gil",
120124
long = "pyspy-gil",
121125
value_name = "PYSPY_GIL",
122-
help = "enable GIL mode for pyspy"
126+
help = "enable GIL mode for pyspy",
127+
takes_value = false
123128
)]
124129
pyspy_gil: bool,
125130
#[clap(
126131
name = "pyspy_native",
127132
long = "pyspy-native",
128133
value_name = "PYSPY_NATIVE",
129-
help = "enable native extensions profiling for pyspy"
134+
help = "enable native extensions profiling for pyspy",
135+
takes_value = false
130136
)]
131137
pyspy_native: bool,
132138
#[clap(
@@ -152,7 +158,7 @@ enum Commands {
152158
)]
153159
spy_name: Spy,
154160
#[clap(
155-
multiple = true,
161+
multiple_occurrences = true,
156162
name = "tag",
157163
long = "tag",
158164
value_name = "TAG",
@@ -206,7 +212,8 @@ enum Commands {
206212
name = "detect_subprocesses",
207213
long = "detect-subprocesses",
208214
value_name = "DECTECT_SUBPROCESSES",
209-
help = "keep track of and profile subprocesses of the main process"
215+
help = "keep track of and profile subprocesses of the main process",
216+
takes_value = false
210217
)]
211218
detect_subprocesses: bool,
212219
#[clap(
@@ -222,7 +229,8 @@ enum Commands {
222229
name = "no_logging",
223230
long = "no-logging",
224231
value_name = "NO_LOGGING",
225-
help = "disable logging from pyroscope"
232+
help = "disable logging from pyroscope",
233+
takes_value = false
226234
)]
227235
no_logging: bool,
228236
//#[clap(
@@ -236,35 +244,40 @@ enum Commands {
236244
name = "rbspy_blocking",
237245
long = "rbspy-blocking",
238246
value_name = "RBSPY_BLOCKING",
239-
help = "enable blocking mode for rbspy"
247+
help = "enable blocking mode for rbspy",
248+
takes_value = false
240249
)]
241250
rbspy_blocking: bool,
242251
#[clap(
243252
name = "pyspy_blocking",
244253
long = "pyspy-blocking",
245254
value_name = "PYSPY_BLOCKING",
246-
help = "enable blocking mode for pyspy"
255+
help = "enable blocking mode for pyspy",
256+
takes_value = false
247257
)]
248258
pyspy_blocking: bool,
249259
#[clap(
250260
name = "pyspy_idle",
251261
long = "pyspy-idle",
252262
value_name = "PYSPY_IDLE",
253-
help = "include idle threads for pyspy"
263+
help = "include idle threads for pyspy",
264+
takes_value = false
254265
)]
255266
pyspy_idle: bool,
256267
#[clap(
257268
name = "pyspy_gil",
258269
long = "pyspy-gil",
259270
value_name = "PYSPY_GIL",
260-
help = "enable GIL mode for pyspy"
271+
help = "enable GIL mode for pyspy",
272+
takes_value = false
261273
)]
262274
pyspy_gil: bool,
263275
#[clap(
264276
name = "pyspy_native",
265277
long = "pyspy-native",
266278
value_name = "PYSPY_NATIVE",
267-
help = "enable native extensions profiling for pyspy"
279+
help = "enable native extensions profiling for pyspy",
280+
takes_value = false
268281
)]
269282
pyspy_native: bool,
270283
#[clap(
@@ -329,12 +342,6 @@ enum Commands {
329342
)]
330343
group_name: Option<String>,
331344
},
332-
#[clap(
333-
name = "config",
334-
about = "Show Configuration",
335-
long_about = None,
336-
)]
337-
Config,
338345
}
339346

340347
/// Supported Completion Shells
@@ -358,12 +365,12 @@ pub fn cli_match() -> Result<()> {
358365
// Merge clap config file if the value is set
359366
AppConfig::merge_config(cli.config.as_deref())?;
360367

361-
let app = Cli::into_app();
368+
let app = Cli::command();
362369

363370
// Merge clap args into config
364371
AppConfig::merge_args(app)?;
365372

366-
let mut app = Cli::into_app();
373+
let mut app = Cli::command();
367374
// Execute the subcommand
368375
match &cli.command {
369376
Commands::Exec { .. } => {
@@ -391,9 +398,6 @@ pub fn cli_match() -> Result<()> {
391398
generate(Zsh, &mut app, "pyroscope-cli", &mut std::io::stdout());
392399
}
393400
},
394-
Commands::Config => {
395-
commands::config()?;
396-
}
397401
}
398402

399403
Ok(())

pyroscope_cli/core/src/commands.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::profiler::Profiler;
1010

1111
/// exec command
1212
pub fn exec() -> Result<()> {
13-
// TODO: this is hacky
13+
// TODO: this processing should probably be done along with the config parsing
1414
set_application_name()?;
1515
set_tags()?;
1616

@@ -31,22 +31,25 @@ pub fn exec() -> Result<()> {
3131

3232
// Initialize profiler
3333
let mut profiler = Profiler::default();
34-
3534
profiler.init()?;
3635

36+
// Create a channel for ctrlc
3737
let (tx, rx) = channel();
3838

39+
// Set ctrcl handler
3940
ctrlc::set_handler(move || {
4041
tx.send(()).unwrap();
4142
})
4243
.expect("Error setting Ctrl-C handler");
4344

4445
println!("Press Ctrl-C to exit.");
4546

47+
// Wait for Ctrl+C signal
4648
rx.recv().unwrap();
4749

4850
println!("Exiting.");
4951

52+
// Stop exector and profiler
5053
executor.stop()?;
5154
profiler.stop()?;
5255

@@ -55,52 +58,50 @@ pub fn exec() -> Result<()> {
5558

5659
/// connect command
5760
pub fn connect() -> Result<()> {
58-
// TODO: this is hacky
61+
// TODO: this processing should probably be done along with the config parsing
5962
set_application_name()?;
6063
set_tags()?;
6164

65+
// Initialize profiler
6266
let mut profiler = Profiler::default();
63-
6467
profiler.init()?;
6568

69+
// Create a channel for ctrlc
6670
let (tx, rx) = channel();
6771

72+
// Set ctrcl handler
6873
ctrlc::set_handler(move || {
6974
tx.send(()).unwrap();
7075
})
7176
.expect("Error setting Ctrl-C handler");
7277

7378
println!("Press Ctrl-C to exit.");
7479

80+
// Wait for Ctrl+C signal
7581
rx.recv().unwrap();
7682

7783
println!("Exiting.");
7884

85+
// Stop exector and profiler
7986
profiler.stop()?;
8087

8188
Ok(())
8289
}
8390

84-
/// Show the configuration file
85-
pub fn config() -> Result<()> {
86-
let config = AppConfig::fetch()?;
87-
println!("{:#?}", config);
88-
89-
Ok(())
90-
}
91-
91+
//
92+
// TODO: These functions should be placed somewhere else
93+
//
9294
fn set_application_name() -> Result<()> {
9395
let pre_app_name: String = AppConfig::get::<String>("application_name").unwrap_or_else(|_| {
9496
names::Generator::default()
9597
.next()
96-
.unwrap_or("unassigned.app".to_string())
97-
.replace("-", ".")
98+
.unwrap_or_else(|| "unassigned.app".to_string())
99+
.replace('-', ".")
98100
});
99101

100102
let pre = match AppConfig::get::<Spy>("spy_name")? {
101103
Spy::Pyspy => "pyspy",
102104
Spy::Rbspy => "rbspy",
103-
_ => "none",
104105
};
105106

106107
// add pre to pre_app_name
@@ -112,7 +113,7 @@ fn set_application_name() -> Result<()> {
112113
}
113114

114115
fn set_tags() -> Result<()> {
115-
let tag: String = AppConfig::get::<String>("tag").unwrap_or("".to_string());
116+
let tag: String = AppConfig::get::<String>("tag").unwrap_or_else(|_| "".to_string());
116117

117118
AppConfig::set("tag", tag.as_str())?;
118119

pyroscope_cli/core/src/profiler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ fn tags_to_array(tags: &str) -> Result<Vec<(&str, &str)>> {
8484

8585
let mut tags_array = Vec::new();
8686

87-
for tag in tags.split(";") {
88-
let mut tag_array = tag.split("=");
87+
for tag in tags.split(';') {
88+
let mut tag_array = tag.split('=');
8989
let key = tag_array
9090
.next()
9191
.ok_or_else(|| Error::new("failed to parse tag key"))?;

pyroscope_cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn main() -> Result<()> {
2525
.install();
2626
}
2727

28+
// Setup logging
2829
let _guard = utils::logger::setup_logging()?;
2930

3031
// Initialize Configuration

pyroscope_cli/utils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ slog-term = "2.8.0"
1515
slog-scope = "4.4.0"
1616
slog-async = "2.7.0"
1717
slog-stdlog = "4.1.0"
18-
clap = "3.1"
18+
clap = "=3.1.6"
1919
log = "=0.4.11"
2020
serde = { version = "1.0", features = ["derive"] }
2121
pyroscope = { path = "../../" }

0 commit comments

Comments
 (0)