-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmod.rs
More file actions
162 lines (145 loc) · 5.14 KB
/
mod.rs
File metadata and controls
162 lines (145 loc) · 5.14 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
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
mod auth;
pub(crate) mod exec;
pub(crate) mod run;
mod setup;
mod shared;
mod show;
mod use_mode;
pub(crate) use shared::*;
use std::path::PathBuf;
use crate::{
api_client::CodSpeedAPIClient,
config::CodSpeedConfig,
local_logger::{CODSPEED_U8_COLOR_CODE, init_local_logger},
prelude::*,
project_config::DiscoveredProjectConfig,
};
use clap::{
Parser, Subcommand,
builder::{Styles, styling},
};
fn create_styles() -> Styles {
styling::Styles::styled()
.header(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
.usage(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
.literal(
styling::Ansi256Color(CODSPEED_U8_COLOR_CODE).on_default() | styling::Effects::BOLD,
)
.placeholder(styling::AnsiColor::Cyan.on_default())
}
#[derive(Parser, Debug)]
#[command(version, about = "The CodSpeed CLI tool", styles = create_styles())]
pub struct Cli {
/// The URL of the CodSpeed GraphQL API
#[arg(
long,
env = "CODSPEED_API_URL",
global = true,
hide = true,
default_value = "https://gql.codspeed.io/"
)]
pub api_url: String,
/// The OAuth token to use for all requests
#[arg(long, env = "CODSPEED_OAUTH_TOKEN", global = true, hide = true)]
pub oauth_token: Option<String>,
/// The configuration name to use
/// If provided, the configuration will be loaded from ~/.config/codspeed/{config-name}.yaml
/// Otherwise, loads from ~/.config/codspeed/config.yaml
#[arg(long, env = "CODSPEED_CONFIG_NAME", global = true)]
pub config_name: Option<String>,
/// Path to project configuration file (codspeed.yaml)
/// If provided, loads config from this path. Otherwise, searches for config files
/// in the current directory and upward to the git root.
#[arg(long, global = true)]
pub config: Option<PathBuf>,
/// The directory to use for caching installed tools
/// The runner will restore cached tools from this directory before installing them.
/// After successful installation, the runner will cache the installed tools to this directory.
/// Only supported on ubuntu and debian systems.
#[arg(long, env = "CODSPEED_SETUP_CACHE_DIR", global = true)]
pub setup_cache_dir: Option<String>,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
enum Commands {
/// Run a benchmark program that already contains the CodSpeed instrumentation and upload the results to CodSpeed
#[command(alias = "r")]
Run(Box<run::RunArgs>),
/// Run a command after adding CodSpeed instrumentation to it and upload the results to
/// CodSpeed
#[command(alias = "x")]
Exec(Box<exec::ExecArgs>),
/// Manage the CLI authentication state
Auth(auth::AuthArgs),
/// Pre-install the codspeed executors
Setup,
/// Set the codspeed mode for the rest of the shell session
Use(use_mode::UseArgs),
/// Show the codspeed mode previously set in this shell session with `codspeed use`
Show,
}
pub async fn run() -> Result<()> {
let cli = Cli::parse();
let codspeed_config =
CodSpeedConfig::load_with_override(cli.config_name.as_deref(), cli.oauth_token.as_deref())?;
let api_client = CodSpeedAPIClient::try_from((&cli, &codspeed_config))?;
// Discover project configuration file
let discovered_config = DiscoveredProjectConfig::discover_and_load(
cli.config.as_deref(),
&std::env::current_dir()?,
)?;
// In the context of the CI, it is likely that a ~ made its way here without being expanded by the shell
let setup_cache_dir = cli
.setup_cache_dir
.as_ref()
.map(|d| PathBuf::from(shellexpand::tilde(d).as_ref()));
let setup_cache_dir = setup_cache_dir.as_deref();
match cli.command {
Commands::Run(_) | Commands::Exec(_) => {} // Run and Exec are responsible for their own logger initialization
_ => {
init_local_logger()?;
}
}
match cli.command {
Commands::Run(args) => {
run::run(
*args,
&api_client,
&codspeed_config,
discovered_config.as_ref(),
setup_cache_dir,
)
.await?
}
Commands::Exec(args) => {
exec::run(
*args,
&api_client,
&codspeed_config,
discovered_config.as_ref().map(|d| &d.config),
setup_cache_dir,
)
.await?
}
Commands::Auth(args) => auth::run(args, &api_client, cli.config_name.as_deref()).await?,
Commands::Setup => setup::setup(setup_cache_dir).await?,
Commands::Use(args) => use_mode::run(args)?,
Commands::Show => show::run()?,
}
Ok(())
}
impl Cli {
/// Create a test CLI instance with a custom API URL for use in tests
#[cfg(test)]
pub fn test_with_url(api_url: String) -> Self {
Self {
api_url,
oauth_token: None,
config_name: None,
config: None,
setup_cache_dir: None,
command: Commands::Setup,
}
}
}