Skip to content

Commit b50a56f

Browse files
committed
refactor: move statics related to config into the appropriate module
1 parent 17c6c00 commit b50a56f

File tree

5 files changed

+51
-47
lines changed

5 files changed

+51
-47
lines changed

src/commands/gen_patch.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ use std::path::PathBuf;
55

66
use anyhow::bail;
77

8-
use crate::config::{CommitId, PatchName};
8+
use crate::config::{self, CommitId, PatchName};
9+
use crate::git;
910
use crate::utils::normalize_commit_msg;
10-
use crate::{CONFIG_PATH, git};
1111

1212
/// Generate patch `filename` at the given `Commit`
1313
pub fn gen_patch(commit: CommitId, filename: Option<PatchName>) -> anyhow::Result<()> {
14-
if !CONFIG_PATH.exists() {
14+
if !config::PATH.exists() {
1515
log::info!(
1616
"Config directory {} does not exist, creating it...",
17-
CONFIG_PATH.to_string_lossy()
17+
config::PATH.to_string_lossy()
1818
);
19-
fs::create_dir_all(&*CONFIG_PATH)?;
19+
fs::create_dir_all(&*config::PATH)?;
2020
}
2121

2222
// 1. if the user provides a custom filename for the patch file, use that
@@ -34,7 +34,7 @@ pub fn gen_patch(commit: CommitId, filename: Option<PatchName>) -> anyhow::Resul
3434
)
3535
});
3636

37-
let patch_file_path = CONFIG_PATH.join(format!("{patch_filename}.patch"));
37+
let patch_file_path = config::PATH.join(format!("{patch_filename}.patch"));
3838

3939
// Paths are UTF-8 encoded. If we cannot convert to UTF-8 that means it is not a
4040
// valid path

src/commands/init.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ use std::io::Write as _;
66
use anyhow::bail;
77
use colored::Colorize as _;
88

9-
use crate::{CONFIG_FILE_PATH, CONFIG_PATH, confirm_prompt};
9+
use crate::{config, confirm_prompt};
1010

1111
/// Initialize the Patchy config file
1212
pub fn init() -> anyhow::Result<()> {
13-
if CONFIG_FILE_PATH.exists()
13+
if config::FILE_PATH.exists()
1414
&& !confirm_prompt!(
1515
"File {} already exists. Overwrite it?",
16-
CONFIG_FILE_PATH.to_string_lossy().bright_blue(),
16+
config::FILE_PATH.to_string_lossy().bright_blue(),
1717
)
1818
{
19-
bail!("Did not overwrite {}", CONFIG_FILE_PATH.display());
19+
bail!("Did not overwrite {}", config::FILE_PATH.display());
2020
}
2121

22-
fs::create_dir_all(&*CONFIG_PATH)?;
22+
fs::create_dir_all(&*config::PATH)?;
2323

24-
let mut file = File::create(&*CONFIG_FILE_PATH)?;
24+
let mut file = File::create(&*config::FILE_PATH)?;
2525

2626
file.write_all(include_bytes!("../../example-config.toml"))?;
2727

28-
log::info!("Created config file {}", CONFIG_FILE_PATH.display());
28+
log::info!("Created config file {}", config::FILE_PATH.display());
2929

3030
Ok(())
3131
}

src/commands/run.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! `run` subcommand
22
3-
use crate::config::{BranchName, Config, PullRequest};
3+
use crate::config::{self, BranchName, Config, PullRequest};
44
use std::ffi::OsString;
55
use std::fs::{self, File};
66
use std::io::Write as _;
@@ -12,9 +12,7 @@ use colored::Colorize as _;
1212
use crate::git_high_level;
1313
use crate::github_api::{self, Branch, Remote, RemoteBranch};
1414
use crate::utils::{display_link, with_uuid};
15-
use crate::{
16-
CONFIG_FILE, CONFIG_FILE_PATH, CONFIG_PATH, CONFIG_ROOT, commands, confirm_prompt, git,
17-
};
15+
use crate::{commands, confirm_prompt, git};
1816

1917
/// Backup for a file
2018
struct FileBackup {
@@ -26,10 +24,13 @@ struct FileBackup {
2624

2725
/// Run patchy, if `yes` then there will be no prompt
2826
pub async fn run(yes: bool) -> anyhow::Result<()> {
29-
let root = CONFIG_ROOT.as_str();
27+
let root = config::ROOT.as_str();
3028

31-
let Ok(config_string) = fs::read_to_string(&*CONFIG_FILE_PATH) else {
32-
log::error!("Could not find configuration file at {root}/{CONFIG_FILE}");
29+
let Ok(config_string) = fs::read_to_string(&*config::FILE_PATH) else {
30+
log::error!(
31+
"Could not find configuration file at {root}/{}",
32+
config::FILE
33+
);
3334

3435
// We don't want to have *any* sort of prompt when using the -y flag since that
3536
// would be problematic in scripts
@@ -47,13 +48,16 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
4748
return Ok(());
4849
};
4950

50-
log::trace!("Using configuration file {}", CONFIG_FILE_PATH.display());
51+
log::trace!("Using configuration file {}", config::FILE_PATH.display());
5152

5253
let config = toml::from_str::<Config>(&config_string).map_err(|err| {
53-
anyhow!("Could not parse `{root}/{CONFIG_FILE}` configuration file:\n{err}",)
54+
anyhow!(
55+
"Could not parse `{root}/{}` configuration file:\n{err}",
56+
config::FILE
57+
)
5458
})?;
5559

56-
let crate::config::Branch {
60+
let config::Branch {
5761
name: remote_branch,
5862
commit,
5963
} = config.remote_branch;
@@ -70,10 +74,10 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
7074

7175
// --- Backup all files in the `.patchy` config directory
7276

73-
let config_files = fs::read_dir(&*CONFIG_PATH).map_err(|err| {
77+
let config_files = fs::read_dir(&*config::PATH).map_err(|err| {
7478
anyhow!(
7579
"Failed to read files in directory `{}`:\n{err}",
76-
&CONFIG_PATH.display()
80+
&config::PATH.display()
7781
)
7882
})?;
7983

@@ -211,15 +215,18 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
211215
}
212216
}
213217

214-
if let Err(err) = fs::create_dir_all(git::ROOT.join(CONFIG_ROOT.as_str())) {
218+
if let Err(err) = fs::create_dir_all(git::ROOT.join(config::ROOT.as_str())) {
215219
git::checkout(&previous_branch)?;
216220

217221
git::delete_remote_and_branch(
218222
&info.remote.local_remote_alias,
219223
&info.branch.local_branch_name,
220224
)?;
221225

222-
bail!("Could not create directory {}\n{err}", CONFIG_ROOT.as_str());
226+
bail!(
227+
"Could not create directory {}\n{err}",
228+
config::ROOT.as_str()
229+
);
223230
}
224231

225232
// Restore all the backup files
@@ -228,7 +235,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
228235
filename, contents, ..
229236
} in &backed_up_files
230237
{
231-
let path = git::ROOT.join(PathBuf::from(CONFIG_ROOT.as_str()).join(filename));
238+
let path = git::ROOT.join(PathBuf::from(config::ROOT.as_str()).join(filename));
232239
let mut file =
233240
File::create(&path).map_err(|err| anyhow!("failed to restore backup: {err}"))?;
234241

@@ -239,7 +246,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
239246

240247
for patch in config.patches {
241248
let file_name = git::ROOT
242-
.join(CONFIG_ROOT.as_str())
249+
.join(config::ROOT.as_str())
243250
.join(format!("{patch}.patch"));
244251

245252
if !file_name.exists() {
@@ -265,7 +272,7 @@ pub async fn run(yes: bool) -> anyhow::Result<()> {
265272
);
266273
}
267274

268-
git::add(CONFIG_ROOT.as_str())?;
275+
git::add(config::ROOT.as_str())?;
269276
git::commit("restore configuration files")?;
270277

271278
let temporary_branch = with_uuid("temp-branch");

src/config.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@
33
use anyhow::{anyhow, bail};
44
use itertools::Itertools;
55
use nutype::nutype;
6-
use std::{convert::Infallible, fmt::Display, path::PathBuf, str::FromStr};
6+
use std::{convert::Infallible, env, fmt::Display, path::PathBuf, str::FromStr, sync::LazyLock};
77
use tap::Pipe as _;
88

99
use indexmap::IndexSet;
1010
use serde::Deserialize;
1111

12+
/// Relative path to root of patchy's configuration
13+
pub static ROOT: LazyLock<String> =
14+
LazyLock::new(|| env::var("PATCHY_CONFIG_ROOT").unwrap_or_else(|_| ".patchy".into()));
15+
16+
/// Absolute path to root of patchy's configuration
17+
pub static PATH: LazyLock<PathBuf> = LazyLock::new(|| crate::git::ROOT.join(&*ROOT));
18+
19+
/// Absolute path to patchy's config file
20+
pub static FILE_PATH: LazyLock<PathBuf> = LazyLock::new(|| PATH.join(FILE));
21+
22+
/// Patchy's config file name
23+
pub const FILE: &str = "config.toml";
24+
1225
/// Represents the TOML config
1326
#[derive(Deserialize, Debug, Eq, PartialEq)]
1427
#[serde(rename_all = "kebab-case")]

src/lib.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Patchy
22
33
#![cfg_attr(doc, doc = include_str!("../README.md"))]
4-
use std::env;
5-
use std::path::PathBuf;
6-
use std::sync::LazyLock;
74

85
mod cli;
96
mod commands;
@@ -13,17 +10,4 @@ mod git_high_level;
1310
mod github_api;
1411
mod utils;
1512

16-
/// Relative path to root of patchy's configuration
17-
static CONFIG_ROOT: LazyLock<String> =
18-
LazyLock::new(|| env::var("PATCHY_CONFIG_ROOT").unwrap_or_else(|_| ".patchy".into()));
19-
20-
/// Absolute path to root of patchy's configuration
21-
static CONFIG_PATH: LazyLock<PathBuf> = LazyLock::new(|| git::ROOT.join(&*CONFIG_ROOT));
22-
23-
/// Absolute path to patchy's config file
24-
static CONFIG_FILE_PATH: LazyLock<PathBuf> = LazyLock::new(|| CONFIG_PATH.join(CONFIG_FILE));
25-
26-
/// Patchy's config file name
27-
const CONFIG_FILE: &str = "config.toml";
28-
2913
pub use cli::Cli;

0 commit comments

Comments
 (0)