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

Favor stellar config dir. #1681

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions cmd/crates/soroban-test/tests/it/integration/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async fn invoke() {
.assert()
.stdout_as_str();
let dir = sandbox.dir();
let seed_phrase = std::fs::read_to_string(dir.join(".soroban/identity/test.toml")).unwrap();
let seed_phrase = std::fs::read_to_string(dir.join(".stellar/identity/test.toml")).unwrap();
let s = toml::from_str::<secret::Secret>(&seed_phrase).unwrap();
let secret::Secret::SeedPhrase { seed_phrase } = s else {
panic!("Expected seed phrase")
Expand Down Expand Up @@ -113,7 +113,7 @@ async fn invoke() {
},
)
.unwrap();
let sk_from_file = std::fs::read_to_string(dir.join(".soroban/identity/testone.toml")).unwrap();
let sk_from_file = std::fs::read_to_string(dir.join(".stellar/identity/testone.toml")).unwrap();

assert_eq!(sk_from_file, format!("secret_key = \"{secret_key_1}\"\n"));
let secret_key_1_readin = sandbox
Expand Down
14 changes: 12 additions & 2 deletions cmd/soroban-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use clap::CommandFactory;
use dotenvy::dotenv;
use tracing_subscriber::{fmt, EnvFilter};

use crate::config_dir_check::config_dir_check;
use crate::print::Print;
use crate::upgrade_check::upgrade_check;
use crate::{commands, print, Root};
use crate::{commands, Root};

#[tokio::main]
pub async fn main() {
Expand Down Expand Up @@ -42,6 +44,7 @@ pub async fn main() {
std::process::exit(1);
}
});

// Now use root to setup the logger
if let Some(level) = root.global_args.log_level() {
let mut e_filter = EnvFilter::from_default_env()
Expand Down Expand Up @@ -78,7 +81,14 @@ pub async fn main() {
upgrade_check(root.global_args.quiet).await;
});

let printer = print::Print::new(root.global_args.quiet);
let locator = root.global_args.locator.clone();

// Spawn a thread to check for .soroban config directories
tokio::spawn(async move {
config_dir_check(locator, Print::new(root.global_args.quiet)).await;
});

let printer = Print::new(root.global_args.quiet);
if let Err(e) = root.run().await {
printer.errorln(format!("error: {e}"));
std::process::exit(1);
Expand Down
14 changes: 11 additions & 3 deletions cmd/soroban-cli/src/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Args {

pub fn local_config(&self) -> Result<PathBuf, Error> {
let pwd = self.current_dir()?;
Ok(find_config_dir(pwd.clone()).unwrap_or_else(|_| pwd.join(".soroban")))
Ok(find_config_dir(pwd.clone()).unwrap_or_else(|_| pwd.join(".stellar")))
}

pub fn current_dir(&self) -> Result<PathBuf, Error> {
Expand Down Expand Up @@ -468,14 +468,22 @@ impl KeyType {
}

pub fn global_config_path() -> Result<PathBuf, Error> {
Ok(if let Ok(config_home) = std::env::var("XDG_CONFIG_HOME") {
let config_dir = if let Ok(config_home) = std::env::var("XDG_CONFIG_HOME") {
PathBuf::from_str(&config_home).map_err(|_| Error::XdgConfigHome(config_home))?
} else {
dirs::home_dir()
.ok_or(Error::HomeDirNotFound)?
.join(".config")
};

let soroban_dir = config_dir.join("soroban");
let stellar_dir = config_dir.join("stellar");

if soroban_dir.exists() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both exists, IMO we should either use stellar_dir first or return an error

Ok(soroban_dir)
} else {
Ok(stellar_dir)
}
.join("soroban"))
}

impl Pwd for Args {
Expand Down
35 changes: 35 additions & 0 deletions cmd/soroban-cli/src/config_dir_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::{config::locator::Args as Locator, print::Print};

pub async fn config_dir_check(locator: Locator, printer: Print) {
let Ok(config_dir) = locator.config_dir() else {
return;
};

// Do not bother warning users when the config directory doesn't exist.
if !config_dir.exists() {
return;
};

let Some(dirname) = config_dir.file_name() else {
return;
};

let Some(parent_dir) = config_dir.parent() else {
return;
};

let new_config_dir = if locator.global {
parent_dir.join("stellar")
} else {
parent_dir.join(".stellar")
};

let message = format!(
"The config directory {config_dir:?} is deprecated and should be \
renamed to {new_config_dir:?}.",
);

if (locator.global && dirname == "soroban") || (!locator.global && dirname == ".soroban") {
printer.warnln(message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do it automatically? This config directory is created and managed by the cli

}
}
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use cli::main;
pub mod assembled;
pub mod commands;
pub mod config;
pub mod config_dir_check;
pub mod fee;
pub mod get_spec;
pub mod key;
Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{

const TERMS: &[&str] = &["Apple_Terminal", "vscode"];

#[derive(Clone)]
pub struct Print {
pub quiet: bool,
}
Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub fn find_config_dir(mut pwd: std::path::PathBuf) -> std::io::Result<std::path
if soroban_exists {
return Ok(soroban_dir);
}

if !pwd.pop() {
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Rust's output directory
target

# Local Soroban settings
# Local settings
.soroban
.stellar
Loading