diff --git a/Cargo.lock b/Cargo.lock index 451235fa7c..1bc9777350 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -755,13 +755,22 @@ dependencies = [ "subtle", ] +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys 0.4.1", +] + [[package]] name = "dirs" version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" dependencies = [ - "dirs-sys", + "dirs-sys 0.3.7", ] [[package]] @@ -775,6 +784,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "doc-comment" version = "0.3.3" @@ -1741,6 +1762,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "overload" version = "0.1.1" @@ -2502,6 +2529,7 @@ dependencies = [ "clap_complete", "crate-git-revision 0.0.4", "csv", + "directories", "dirs", "dotenvy", "ed25519-dalek 2.0.0", diff --git a/Cargo.toml b/Cargo.toml index c88cd4abc7..625e34f95b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -89,6 +89,7 @@ tracing-subscriber = "0.3.16" tracing-appender = "0.2.2" which = "4.4.0" wasmparser = "0.90.0" +directories = "5.0.1" # [patch."https://github.com/stellar/rs-soroban-env"] diff --git a/cmd/soroban-cli/Cargo.toml b/cmd/soroban-cli/Cargo.toml index ad60c827ac..a48b7dc344 100644 --- a/cmd/soroban-cli/Cargo.toml +++ b/cmd/soroban-cli/Cargo.toml @@ -93,6 +93,7 @@ tracing-subscriber = { workspace = true, features = ["env-filter"] } cargo_metadata = "0.15.4" pathdiff = "0.2.1" dotenvy = "0.15.7" +directories = { workspace = true } # For hyper-tls [target.'cfg(unix)'.dependencies] openssl = { version = "0.10.55", features = ["vendored"] } diff --git a/cmd/soroban-cli/src/commands/config/data.rs b/cmd/soroban-cli/src/commands/config/data.rs new file mode 100644 index 0000000000..1882505b6e --- /dev/null +++ b/cmd/soroban-cli/src/commands/config/data.rs @@ -0,0 +1,21 @@ +use chrono::prelude::*; +use directories::ProjectDirs; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Failed to find project directories")] + FiledToFindProjectDirs, + #[error(transparent)] + Io(#[from] std::io::Error), +} + +pub fn project_dir() -> Result { + ProjectDirs::from("com", "stellar", "soroban-cli").ok_or(Error::FiledToFindProjectDirs) +} + +pub fn write(prefix: &str, suffix: &str, data: impl AsRef<[u8]>) -> Result<(), Error> { + let timestamp = Utc::now().format("%Y%m%d_%H%M%S"); + let name = format!("{prefix}_{timestamp}{suffix}"); + let file = project_dir()?.data_local_dir().join(name); + Ok(std::fs::write(file, data)?) +}