Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(opts): remove config from opts #9562

Merged
merged 5 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions crates/turborepo-lib/src/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub async fn link(
let api_client = base.api_client()?;
let token = base
.opts()
.api_client_opts
.token
.as_deref()
.ok_or_else(|| Error::TokenNotFound {
Expand Down
8 changes: 4 additions & 4 deletions crates/turborepo-lib/src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub async fn sso_login(
telemetry.track_login_method(LoginMethod::SSO);
let api_client: APIClient = base.api_client()?;
let color_config = base.color_config;
let login_url_config = base.opts.login_url.to_string();
let login_url_config = base.opts.api_client_opts.login_url.to_string();
let options = LoginOptions {
existing_token: base.opts.token.as_deref(),
existing_token: base.opts.api_client_opts.token.as_deref(),
sso_team: Some(sso_team),
force,
..LoginOptions::new(
Expand Down Expand Up @@ -72,8 +72,8 @@ pub async fn login(

let api_client: APIClient = base.api_client()?;
let color_config = base.color_config;
let login_url_config = base.opts.login_url.to_string();
let existing_token = base.opts.token.as_deref();
let login_url_config = base.opts.api_client_opts.login_url.to_string();
let existing_token = base.opts.api_client_opts.token.as_deref();

let options = LoginOptions {
existing_token,
Expand Down
14 changes: 7 additions & 7 deletions crates/turborepo-lib/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ impl CommandBase {
}

pub fn api_auth(&self) -> Result<Option<APIAuth>, ConfigError> {
let team_id = self.opts.team_id.as_ref();
let team_slug = self.opts.team_slug.as_ref();
let team_id = self.opts.api_client_opts.team_id.as_ref();
let team_slug = self.opts.api_client_opts.team_slug.as_ref();

let Some(token) = &self.opts.token else {
let Some(token) = &self.opts.api_client_opts.token else {
return Ok(None);
};

Expand All @@ -158,11 +158,11 @@ impl CommandBase {
}

pub fn api_client(&self) -> Result<APIClient, ConfigError> {
let timeout = self.opts.timeout;
let upload_timeout = self.opts.upload_timeout;
let timeout = self.opts.api_client_opts.timeout;
let upload_timeout = self.opts.api_client_opts.upload_timeout;

APIClient::new(
&self.opts.api_url,
&self.opts.api_client_opts.api_url,
if timeout > 0 {
Some(Duration::from_secs(timeout))
} else {
Expand All @@ -174,7 +174,7 @@ impl CommandBase {
None
},
self.version,
self.opts.preflight,
self.opts.api_client_opts.preflight,
)
.map_err(ConfigError::ApiClient)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/commands/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl<'a> Prune<'a> {
output_dir: &str,
telemetry: CommandEventBuilder,
) -> Result<Self, Error> {
let allow_missing_package_manager = base.opts().allow_no_package_manager;
let allow_missing_package_manager = base.opts().config_opts.allow_no_package_manager;
telemetry.track_arg_usage(
"dangerously-allow-missing-package-manager",
allow_missing_package_manager,
Expand Down
6 changes: 4 additions & 2 deletions crates/turborepo-lib/src/commands/unlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ enum UnlinkSpacesResult {
}

fn unlink_remote_caching(base: &mut CommandBase) -> Result<(), cli::Error> {
let needs_disabling = base.opts.team_id.is_some() || base.opts.team_slug.is_some();
let needs_disabling = base.opts.api_client_opts.team_id.is_some()
|| base.opts.api_client_opts.team_slug.is_some();

let output = if needs_disabling {
let local_config_path = base.local_config_path();
Expand Down Expand Up @@ -56,7 +57,8 @@ fn unlink_remote_caching(base: &mut CommandBase) -> Result<(), cli::Error> {
}

fn unlink_spaces(base: &mut CommandBase) -> Result<(), cli::Error> {
let needs_disabling = base.opts().team_id.is_some() || base.opts().team_slug.is_some();
let needs_disabling = base.opts().api_client_opts.team_id.is_some()
|| base.opts().api_client_opts.team_slug.is_some();

if needs_disabling {
let local_config_path = base.local_config_path();
Expand Down
4 changes: 2 additions & 2 deletions crates/turborepo-lib/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ impl Diagnostic for RemoteCacheDiagnostic {
let (has_team_id, has_team_slug) = {
let base = base.lock().await;
(
base.opts().team_id.is_some(),
base.opts().team_slug.is_some(),
base.opts().api_client_opts.team_id.is_some(),
base.opts().api_client_opts.team_slug.is_some(),
)
};

Expand Down
121 changes: 78 additions & 43 deletions crates/turborepo-lib/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,36 @@ pub enum Error {
Config(#[from] crate::config::Error),
}

/// The fully resolved options for Turborepo. This is the combination of config,
/// including all the layers (env, args, etc.), and the command line arguments.
#[derive(Debug, Clone)]
pub struct Opts {
pub root_turbo_json_path: AbsoluteSystemPathBuf,
pub struct APIClientOpts {
pub api_url: String,
pub timeout: u64,
pub upload_timeout: u64,
pub token: Option<String>,
pub team_id: Option<String>,
pub team_slug: Option<String>,
pub login_url: String,
pub preflight: bool,
}

#[derive(Debug, Clone)]
pub struct ConfigOpts {
NicholasLYang marked this conversation as resolved.
Show resolved Hide resolved
pub root_turbo_json_path: AbsoluteSystemPathBuf,
pub allow_no_package_manager: bool,
pub allow_no_turbo_json: bool,
pub login_url: String,
}

/// The fully resolved options for Turborepo. This is the combination of config,
/// including all the layers (env, args, defaults, etc.), and the command line
/// arguments.
#[derive(Debug, Clone)]
pub struct Opts {
pub config_opts: ConfigOpts,
pub api_client_opts: APIClientOpts,
pub cache_opts: CacheOpts,
pub run_opts: RunOpts,
pub runcache_opts: RunCacheOpts,
pub scope_opts: ScopeOpts,
pub preflight: bool,
}

impl Opts {
Expand Down Expand Up @@ -139,6 +150,7 @@ impl Opts {
};

let inputs = OptsInputs {
repo_root,
run_args: run_args.as_ref(),
execution_args: execution_args.as_ref(),
config: &config,
Expand All @@ -148,40 +160,23 @@ impl Opts {
let cache_opts = CacheOpts::try_from(inputs)?;
let scope_opts = ScopeOpts::try_from(inputs)?;
let runcache_opts = RunCacheOpts::from(inputs);
let root_turbo_json_path = config.root_turbo_json_path(repo_root);
let api_url = config.api_url().to_string();
let timeout = config.timeout();
let upload_timeout = config.upload_timeout();
let preflight = config.preflight();
let token = config.token().map(|s| s.to_string());
let team_id = config.team_id().map(|s| s.to_string());
let team_slug = config.team_slug().map(|s| s.to_string());
let login_url = config.login_url().to_string();
let allow_no_package_manager = config.allow_no_package_manager();
let allow_no_turbo_json = config.allow_no_turbo_json();
let api_client_opts = APIClientOpts::from(inputs);
let config_opts = ConfigOpts::from(inputs);

Ok(Self {
root_turbo_json_path,
api_url,
timeout,
upload_timeout,
preflight,
token,
team_id,
team_slug,
login_url,
allow_no_package_manager,
allow_no_turbo_json,
config_opts,
run_opts,
cache_opts,
scope_opts,
runcache_opts,
api_client_opts,
})
}
}

#[derive(Debug, Clone, Copy)]
struct OptsInputs<'a> {
repo_root: &'a AbsoluteSystemPath,
run_args: &'a RunArgs,
execution_args: &'a ExecutionArgs,
config: &'a ConfigurationOptions,
Expand Down Expand Up @@ -276,6 +271,20 @@ pub enum ResolvedLogPrefix {
None,
}

impl<'a> From<OptsInputs<'a>> for ConfigOpts {
fn from(inputs: OptsInputs<'a>) -> Self {
let root_turbo_json_path = inputs.config.root_turbo_json_path(inputs.repo_root);
let allow_no_package_manager = inputs.config.allow_no_package_manager();
let allow_no_turbo_json = inputs.config.allow_no_turbo_json();

ConfigOpts {
root_turbo_json_path,
allow_no_package_manager,
allow_no_turbo_json,
}
}
}

const DEFAULT_CONCURRENCY: u32 = 10;

impl<'a> TryFrom<OptsInputs<'a>> for RunOpts {
Expand Down Expand Up @@ -415,6 +424,30 @@ impl<'a> TryFrom<OptsInputs<'a>> for ScopeOpts {
}
}

impl<'a> From<OptsInputs<'a>> for APIClientOpts {
fn from(inputs: OptsInputs<'a>) -> Self {
let api_url = inputs.config.api_url().to_string();
let timeout = inputs.config.timeout();
let upload_timeout = inputs.config.upload_timeout();
let preflight = inputs.config.preflight();
let token = inputs.config.token().map(|s| s.to_string());
let team_id = inputs.config.team_id().map(|s| s.to_string());
let team_slug = inputs.config.team_slug().map(|s| s.to_string());
let login_url = inputs.config.login_url().to_string();

APIClientOpts {
api_url,
timeout,
upload_timeout,
token,
team_id,
team_slug,
login_url,
preflight,
}
}
}

impl<'a> TryFrom<OptsInputs<'a>> for CacheOpts {
type Error = self::Error;

Expand Down Expand Up @@ -501,7 +534,7 @@ mod test {
use turborepo_cache::CacheOpts;
use turborepo_ui::ColorConfig;

use super::RunOpts;
use super::{APIClientOpts, ConfigOpts, RunOpts};
use crate::{
cli::{Command, DryRunMode, RunArgs},
commands::CommandBase,
Expand Down Expand Up @@ -642,23 +675,25 @@ mod test {
let root_turbo_json_path = config.root_turbo_json_path(&AbsoluteSystemPathBuf::default());

let opts = Opts {
config,
root_turbo_json_path,
api_url: "".to_string(),
timeout: 0,
upload_timeout: 0,
token: None,
team_id: None,
team_slug: None,
allow_no_package_manager: false,
allow_no_turbo_json: false,
config_opts: ConfigOpts {
root_turbo_json_path,
allow_no_package_manager: false,
allow_no_turbo_json: false,
},
api_client_opts: APIClientOpts {
api_url: "".to_string(),
timeout: 0,
upload_timeout: 0,
token: None,
team_id: None,
team_slug: None,
login_url: "".to_string(),
preflight: false,
},
scope_opts,
run_opts,
cache_opts,
scope_opts,
runcache_opts,

login_url: "".to_string(),
preflight: false,
};
let synthesized = opts.synthesize_command();
assert_eq!(synthesized, expected);
Expand Down
Loading
Loading