Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ pub struct CodSpeedAPIClient {
unauthenticated_gql_client: GQLClient,
}

impl TryFrom<&Cli> for CodSpeedAPIClient {
impl TryFrom<(&Cli, &CodSpeedConfig)> for CodSpeedAPIClient {
type Error = Error;
fn try_from(args: &Cli) -> Result<Self> {
let codspeed_config = CodSpeedConfig::load()?;

fn try_from((args, codspeed_config): (&Cli, &CodSpeedConfig)) -> Result<Self> {
Ok(Self {
gql_client: build_gql_api_client(&codspeed_config, args.api_url.clone(), true),
gql_client: build_gql_api_client(codspeed_config, args.api_url.clone(), true),
unauthenticated_gql_client: build_gql_api_client(
&codspeed_config,
codspeed_config,
args.api_url.clone(),
false,
),
Expand Down
10 changes: 8 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
api_client::CodSpeedAPIClient,
auth,
config::CodSpeedConfig,
local_logger::{init_local_logger, CODSPEED_U8_COLOR_CODE},
prelude::*,
run, setup,
Expand Down Expand Up @@ -33,6 +34,10 @@ pub struct Cli {
)]
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>,

#[command(subcommand)]
command: Commands,
}
Expand All @@ -49,7 +54,8 @@ enum Commands {

pub async fn run() -> Result<()> {
let cli = Cli::parse();
let api_client = CodSpeedAPIClient::try_from(&cli)?;
let codspeed_config = CodSpeedConfig::load_with_override(cli.oauth_token.as_deref())?;
let api_client = CodSpeedAPIClient::try_from((&cli, &codspeed_config))?;

match cli.command {
Commands::Run(_) => {} // Run is responsible for its own logger initialization
Expand All @@ -59,7 +65,7 @@ pub async fn run() -> Result<()> {
}

match cli.command {
Commands::Run(args) => run::run(args, &api_client).await?,
Commands::Run(args) => run::run(args, &api_client, &codspeed_config).await?,
Commands::Auth(args) => auth::run(args, &api_client).await?,
Commands::Setup => setup::setup().await?,
}
Expand Down
26 changes: 19 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,39 @@ impl Default for CodSpeedConfig {
}

impl CodSpeedConfig {
/// Load the configuration. If it does not exist, store and return a default configuration
pub fn load() -> Result<Self> {
/// Load the configuration. If it does not exist, return a default configuration.
///
/// If oauth_token_override is provided, the token from the loaded configuration will be
/// ignored, and the override will be used instead
pub fn load_with_override(oauth_token_override: Option<&str>) -> Result<Self> {
let config_path = get_configuration_file_path();

match fs::read(&config_path) {
let mut config = match fs::read(&config_path) {
Ok(config_str) => {
let config = serde_yaml::from_slice(&config_str).context(format!(
"Failed to parse CodSpeed config at {}",
config_path.display()
))?;
debug!("Config loaded from {}", config_path.display());
Ok(config)
config
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
debug!("Config file not found at {}", config_path.display());
let config = CodSpeedConfig::default();
config.persist()?;
Ok(config)
CodSpeedConfig::default()
}
Err(e) => bail!("Failed to load config: {}", e),
};

if let Some(oauth_token) = oauth_token_override {
config.auth.token = Some(oauth_token.to_owned());
}

Ok(config)
}

/// Load the configuration. If it does not exist, return a default configuration.
pub fn load() -> Result<Self> {
Self::load_with_override(None)
}

/// Persist changes to the configuration
Expand Down
7 changes: 5 additions & 2 deletions src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,14 @@ impl RunArgs {
}
}

pub async fn run(args: RunArgs, api_client: &CodSpeedAPIClient) -> Result<()> {
pub async fn run(
args: RunArgs,
api_client: &CodSpeedAPIClient,
codspeed_config: &CodSpeedConfig,
) -> Result<()> {
let output_json = args.message_format == Some(MessageFormat::Json);
let mut config = Config::try_from(args)?;
let provider = run_environment::get_provider(&config)?;
let codspeed_config = CodSpeedConfig::load()?;
let logger = Logger::new(&provider)?;

if provider.get_run_environment() != RunEnvironment::Local {
Expand Down
31 changes: 31 additions & 0 deletions src/run/run_environment/local/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use simplelog::SharedLogger;

use crate::local_logger::get_local_logger;
use crate::prelude::*;
use crate::run::check_system::SystemInfo;
use crate::run::config::RepositoryOverride;
use crate::run::helpers::{parse_git_remote, GitRemote};
use crate::run::run_environment::{RunEnvironment, RunPart};
use crate::run::runner::ExecutorName;
use crate::run::uploader::{Runner, UploadMetadata};
use crate::run::{
config::Config,
helpers::find_repository_root,
Expand Down Expand Up @@ -141,6 +144,34 @@ impl RunEnvironmentProvider for LocalProvider {
})
}

fn get_upload_metadata(
&self,
config: &Config,
system_info: &SystemInfo,
archive_hash: &str,
executor_name: ExecutorName,
) -> Result<UploadMetadata> {
let run_environment_metadata = self.get_run_environment_metadata()?;

Ok(UploadMetadata {
version: Some(6),
tokenless: config.token.is_none(),
repository_provider: self.get_repository_provider(),
commit_hash: run_environment_metadata.ref_.clone(),
run_environment_metadata,
profile_md5: archive_hash.into(),
runner: Runner {
name: "codspeed-runner".into(),
version: crate::VERSION.into(),
instruments: config.instruments.get_active_instrument_names(),
executor: executor_name,
system_info: system_info.clone(),
},
run_environment: self.get_run_environment(),
run_part: self.get_run_provider_run_part(),
})
}

/// For local runs have, we cannot really send anything here
fn get_run_provider_run_part(&self) -> Option<RunPart> {
None
Expand Down
Loading