Skip to content

Commit 4c9ed7b

Browse files
feat: add --config-name argument to allow multiple configs
1 parent 73dd4b3 commit 4c9ed7b

3 files changed

Lines changed: 39 additions & 22 deletions

File tree

src/app.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ pub struct Cli {
4040
#[arg(long, env = "CODSPEED_OAUTH_TOKEN", global = true, hide = true)]
4141
pub oauth_token: Option<String>,
4242

43+
/// The configuration name to use
44+
/// If provided, the configuration will be loaded from ~/.config/codspeed/{config-name}.yaml
45+
/// Otherwise, loads from ~/.config/codspeed/config.yaml
46+
#[arg(long, env = "CODSPEED_CONFIG_NAME", global = true)]
47+
pub config_name: Option<String>,
48+
4349
/// The directory to use for caching installed tools
4450
/// The runner will restore cached tools from this directory before installing them.
4551
/// After successful installation, the runner will cache the installed tools to this directory.
@@ -63,7 +69,8 @@ enum Commands {
6369

6470
pub async fn run() -> Result<()> {
6571
let cli = Cli::parse();
66-
let codspeed_config = CodSpeedConfig::load_with_override(cli.oauth_token.as_deref())?;
72+
let codspeed_config =
73+
CodSpeedConfig::load_with_override(cli.config_name.as_deref(), cli.oauth_token.as_deref())?;
6774
let api_client = CodSpeedAPIClient::try_from((&cli, &codspeed_config))?;
6875
// In the context of the CI, it is likely that a ~ made its way here without being expanded by the shell
6976
let setup_cache_dir = cli
@@ -83,7 +90,7 @@ pub async fn run() -> Result<()> {
8390
Commands::Run(args) => {
8491
run::run(args, &api_client, &codspeed_config, setup_cache_dir).await?
8592
}
86-
Commands::Auth(args) => auth::run(args, &api_client).await?,
93+
Commands::Auth(args) => auth::run(args, &api_client, cli.config_name.as_deref()).await?,
8794
Commands::Setup => setup::setup(setup_cache_dir).await?,
8895
}
8996
Ok(())

src/auth.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@ enum AuthCommands {
1717
Login,
1818
}
1919

20-
pub async fn run(args: AuthArgs, api_client: &CodSpeedAPIClient) -> Result<()> {
20+
pub async fn run(
21+
args: AuthArgs,
22+
api_client: &CodSpeedAPIClient,
23+
config_name: Option<&str>,
24+
) -> Result<()> {
2125
match args.command {
22-
AuthCommands::Login => login(api_client).await?,
26+
AuthCommands::Login => login(api_client, config_name).await?,
2327
}
2428
Ok(())
2529
}
2630

2731
const LOGIN_SESSION_MAX_DURATION: Duration = Duration::from_secs(60 * 5); // 5 minutes
2832

29-
async fn login(api_client: &CodSpeedAPIClient) -> Result<()> {
33+
async fn login(api_client: &CodSpeedAPIClient, config_name: Option<&str>) -> Result<()> {
3034
debug!("Login to CodSpeed");
3135
start_group!("Creating login session");
3236
let login_session_payload = api_client.create_login_session().await?;
@@ -67,9 +71,9 @@ async fn login(api_client: &CodSpeedAPIClient) -> Result<()> {
6771
}
6872
end_group!();
6973

70-
let mut config = CodSpeedConfig::load()?;
74+
let mut config = CodSpeedConfig::load_with_override(config_name, None)?;
7175
config.auth.token = Some(token);
72-
config.persist()?;
76+
config.persist(config_name)?;
7377
debug!("Token saved to configuration file");
7478

7579
info!("Login successful, your are now authenticated on CodSpeed");

src/config.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@ nest! {
1616

1717
/// Get the path to the configuration file, following the XDG Base Directory Specification
1818
/// at https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
19-
fn get_configuration_file_path() -> PathBuf {
19+
///
20+
/// If config_name is None, returns ~/.config/codspeed/config.yaml (default)
21+
/// If config_name is Some, returns ~/.config/codspeed/{config_name}.yaml
22+
fn get_configuration_file_path(config_name: Option<&str>) -> PathBuf {
2023
let config_dir = env::var("XDG_CONFIG_HOME")
2124
.map(PathBuf::from)
2225
.unwrap_or_else(|_| {
2326
let home = env::var("HOME").expect("HOME env variable not set");
2427
PathBuf::from(home).join(".config")
2528
});
2629
let config_dir = config_dir.join("codspeed");
27-
config_dir.join("config.yaml")
30+
31+
match config_name {
32+
Some(name) => config_dir.join(format!("{name}.yaml")),
33+
None => config_dir.join("config.yaml"),
34+
}
2835
}
2936

3037
impl Default for CodSpeedConfig {
@@ -40,15 +47,19 @@ impl CodSpeedConfig {
4047
///
4148
/// If oauth_token_override is provided, the token from the loaded configuration will be
4249
/// ignored, and the override will be used instead
43-
pub fn load_with_override(oauth_token_override: Option<&str>) -> Result<Self> {
44-
let config_path = get_configuration_file_path();
50+
pub fn load_with_override(
51+
config_name: Option<&str>,
52+
oauth_token_override: Option<&str>,
53+
) -> Result<Self> {
54+
let config_path = get_configuration_file_path(config_name);
4555

4656
let mut config = match fs::read(&config_path) {
4757
Ok(config_str) => {
48-
let config = serde_yaml::from_slice(&config_str).context(format!(
49-
"Failed to parse CodSpeed config at {}",
50-
config_path.display()
51-
))?;
58+
let config: CodSpeedConfig =
59+
serde_yaml::from_slice(&config_str).context(format!(
60+
"Failed to parse CodSpeed config at {}",
61+
config_path.display()
62+
))?;
5263
debug!("Config loaded from {}", config_path.display());
5364
config
5465
}
@@ -66,14 +77,9 @@ impl CodSpeedConfig {
6677
Ok(config)
6778
}
6879

69-
/// Load the configuration. If it does not exist, return a default configuration.
70-
pub fn load() -> Result<Self> {
71-
Self::load_with_override(None)
72-
}
73-
7480
/// Persist changes to the configuration
75-
pub fn persist(&self) -> Result<()> {
76-
let config_path = get_configuration_file_path();
81+
pub fn persist(&self, config_name: Option<&str>) -> Result<()> {
82+
let config_path = get_configuration_file_path(config_name);
7783
fs::create_dir_all(config_path.parent().unwrap())?;
7884

7985
let config_str = serde_yaml::to_string(self)?;

0 commit comments

Comments
 (0)