Repository navigation
Add global auto-update cooldown config #2041
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| use std::io::ErrorKind; | ||
| use std::ops::Deref; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| use anyhow::{Context, Result}; | ||
| use etcetera::BaseStrategy; | ||
| use prek_consts::env_vars::EnvVars; | ||
| use serde::Deserialize; | ||
|
|
||
| use crate::cli::AutoUpdateArgs; | ||
|
|
||
| fn user_config_path() -> Option<PathBuf> { | ||
| if let Some(path) = EnvVars::var_os(EnvVars::PREK_INTERNAL__USER_CONFIG_PATH) { | ||
| return Some(PathBuf::from(path)); | ||
| } | ||
|
|
||
| etcetera::choose_base_strategy() | ||
| .ok() | ||
| .map(|strategy| strategy.config_dir().join("prek").join("prek.toml")) | ||
| } | ||
|
|
||
| /// Options loaded from a user-level `prek.toml` file. | ||
| #[derive(Debug, Clone)] | ||
| pub(crate) struct FilesystemOptions(Options); | ||
|
|
||
| impl FilesystemOptions { | ||
| /// Load user-level options from the platform config directory. | ||
| pub(crate) fn user() -> Result<Option<Self>> { | ||
| let Some(path) = user_config_path() else { | ||
| tracing::trace!( | ||
| "Skipping global config lookup because no platform config directory was found" | ||
| ); | ||
| return Ok(None); | ||
| }; | ||
|
|
||
| tracing::trace!(path = %path.display(), "Searching for global config"); | ||
| Self::from_file(&path) | ||
| } | ||
|
|
||
| fn from_file(path: &Path) -> Result<Option<Self>> { | ||
| let content = match fs_err::read_to_string(path) { | ||
| Ok(content) => { | ||
| tracing::debug!(path = %path.display(), "Read global config"); | ||
| content | ||
| } | ||
| Err(err) | ||
| if matches!( | ||
| err.kind(), | ||
| ErrorKind::NotFound | ErrorKind::NotADirectory | ErrorKind::PermissionDenied | ||
| ) => | ||
| { | ||
| tracing::trace!( | ||
| path = %path.display(), | ||
| "Global config not found or inaccessible, skipping" | ||
| ); | ||
| return Ok(None); | ||
| } | ||
| Err(err) => { | ||
| return Err(err) | ||
| .with_context(|| format!("Failed to read global config `{}`", path.display())); | ||
| } | ||
| }; | ||
|
|
||
| toml::from_str(&content) | ||
| .map(Self) | ||
| .map(Some) | ||
| .with_context(|| format!("Failed to parse global config `{}`", path.display())) | ||
| } | ||
| } | ||
|
|
||
| impl Deref for FilesystemOptions { | ||
| type Target = Options; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| /// Options as represented in the global `prek.toml` file. | ||
| #[derive(Debug, Clone, Default, Deserialize)] | ||
| #[serde(default, rename_all = "snake_case")] | ||
| pub(crate) struct Options { | ||
| auto_update: Option<AutoUpdateOptions>, | ||
| } | ||
|
|
||
| /// Options for the `auto-update` command. | ||
| #[derive(Debug, Clone, Default, Deserialize)] | ||
| #[serde(default, rename_all = "snake_case")] | ||
| struct AutoUpdateOptions { | ||
| cooldown_days: Option<u8>, | ||
| } | ||
|
|
||
| /// Resolved settings for the `auto-update` command. | ||
| #[derive(Debug, Clone, Copy)] | ||
| pub(crate) struct AutoUpdateSettings { | ||
| pub(crate) cooldown_days: u8, | ||
| } | ||
|
|
||
| impl AutoUpdateSettings { | ||
| pub(crate) fn resolve(args: &AutoUpdateArgs, filesystem: Option<&FilesystemOptions>) -> Self { | ||
| Self { | ||
| cooldown_days: args | ||
| .cooldown_days | ||
| .or_else(|| { | ||
| filesystem | ||
| .and_then(|fs| fs.auto_update.as_ref()) | ||
| .and_then(|au| au.cooldown_days) | ||
| }) | ||
| .unwrap_or_default(), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| use crate::common::{TestContext, cmd_snapshot}; | ||
|
|
||
| mod common; | ||
|
|
||
| #[test] | ||
| fn global_config_missing_file_uses_defaults() { | ||
| let context = TestContext::new(); | ||
|
|
||
| cmd_snapshot!(context.filters(), context.auto_update().arg("--show-settings"), @" | ||
| success: true | ||
| exit_code: 0 | ||
| ----- stdout ----- | ||
| GlobalArgs { | ||
| config: None, | ||
| cd: None, | ||
| color: Auto, | ||
| refresh: false, | ||
| help: (), | ||
| no_progress: false, | ||
| quiet: 0, | ||
| verbose: 0, | ||
| log_file: None, | ||
| no_log_file: false, | ||
| version: (), | ||
| show_settings: true, | ||
| } | ||
| AutoUpdateSettings { | ||
| cooldown_days: 0, | ||
| } | ||
|
|
||
| ----- stderr ----- | ||
| "); | ||
| } | ||
|
|
||
| #[test] | ||
| fn global_config_applies_cooldown_days() { | ||
| let context = TestContext::new(); | ||
| context.write_user_config(indoc::indoc! {r" | ||
| [auto_update] | ||
| cooldown_days = 3 | ||
| "}); | ||
|
|
||
| cmd_snapshot!(context.filters(), context.auto_update().arg("--show-settings"), @" | ||
| success: true | ||
| exit_code: 0 | ||
| ----- stdout ----- | ||
| GlobalArgs { | ||
| config: None, | ||
| cd: None, | ||
| color: Auto, | ||
| refresh: false, | ||
| help: (), | ||
| no_progress: false, | ||
| quiet: 0, | ||
| verbose: 0, | ||
| log_file: None, | ||
| no_log_file: false, | ||
| version: (), | ||
| show_settings: true, | ||
| } | ||
| AutoUpdateSettings { | ||
| cooldown_days: 3, | ||
| } | ||
|
|
||
| ----- stderr ----- | ||
| "); | ||
| } | ||
|
|
||
| #[test] | ||
| fn global_config_cli_args_override_file() { | ||
| let context = TestContext::new(); | ||
| context.write_user_config(indoc::indoc! {r" | ||
| [auto_update] | ||
| cooldown_days = 3 | ||
| "}); | ||
|
|
||
| cmd_snapshot!( | ||
| context.filters(), | ||
| context | ||
| .auto_update() | ||
| .arg("--show-settings") | ||
| .arg("--cooldown-days") | ||
| .arg("0"), | ||
| @" | ||
| success: true | ||
| exit_code: 0 | ||
| ----- stdout ----- | ||
| GlobalArgs { | ||
| config: None, | ||
| cd: None, | ||
| color: Auto, | ||
| refresh: false, | ||
| help: (), | ||
| no_progress: false, | ||
| quiet: 0, | ||
| verbose: 0, | ||
| log_file: None, | ||
| no_log_file: false, | ||
| version: (), | ||
| show_settings: true, | ||
| } | ||
| AutoUpdateSettings { | ||
| cooldown_days: 0, | ||
| } | ||
|
|
||
| ----- stderr ----- | ||
| "); | ||
| } | ||
|
|
||
| #[test] | ||
| fn global_config_ignores_unknown_options() { | ||
| let context = TestContext::new(); | ||
| context.write_user_config(indoc::indoc! {r#" | ||
| future_option = true | ||
|
|
||
| [auto_update] | ||
| cooldown_days = 3 | ||
| future_option = "ignored" | ||
| "#}); | ||
|
|
||
| cmd_snapshot!(context.filters(), context.auto_update().arg("--show-settings"), @" | ||
| success: true | ||
| exit_code: 0 | ||
| ----- stdout ----- | ||
| GlobalArgs { | ||
| config: None, | ||
| cd: None, | ||
| color: Auto, | ||
| refresh: false, | ||
| help: (), | ||
| no_progress: false, | ||
| quiet: 0, | ||
| verbose: 0, | ||
| log_file: None, | ||
| no_log_file: false, | ||
| version: (), | ||
| show_settings: true, | ||
| } | ||
| AutoUpdateSettings { | ||
| cooldown_days: 3, | ||
| } | ||
|
|
||
| ----- stderr ----- | ||
| "); | ||
| } | ||
|
|
||
| #[test] | ||
| fn global_config_invalid_file_reports_parse_error() { | ||
| let context = TestContext::new(); | ||
| context.write_user_config(indoc::indoc! {r#" | ||
| [auto_update] | ||
| cooldown_days = "soon" | ||
| "#}); | ||
|
|
||
| cmd_snapshot!(context.filters(), context.auto_update().arg("--show-settings"), @r#" | ||
| success: false | ||
| exit_code: 2 | ||
| ----- stdout ----- | ||
| GlobalArgs { | ||
| config: None, | ||
| cd: None, | ||
| color: Auto, | ||
| refresh: false, | ||
| help: (), | ||
| no_progress: false, | ||
| quiet: 0, | ||
| verbose: 0, | ||
| log_file: None, | ||
| no_log_file: false, | ||
| version: (), | ||
| show_settings: true, | ||
| } | ||
|
|
||
| ----- stderr ----- | ||
| error: Failed to parse global config `[HOME]/config/prek/prek.toml` | ||
| caused by: TOML parse error at line 2, column 17 | ||
| | | ||
| 2 | cooldown_days = "soon" | ||
| | ^^^^^^ | ||
| invalid type: string "soon", expected u8 | ||
| "#); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.