Skip to content

Commit

Permalink
Favor stellar config dir. (#1681)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Oct 24, 2024
1 parent fb6be31 commit d16fcf1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
52 changes: 52 additions & 0 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use assert_fs::TempDir;
use predicates::prelude::predicate;
use soroban_test::{AssertExt, TestEnv};
use std::{fs, path::Path};

Expand Down Expand Up @@ -288,3 +289,54 @@ fn use_env() {
.success()
.stdout("SDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2DYQCHVCQYFD\n");
}

#[test]
fn config_dirs_precedence() {
let sandbox = TestEnv::default();

sandbox
.new_assert_cmd("keys")
.env(
"SOROBAN_SECRET_KEY",
"SC4ZPYELVR7S7EE7KZDZN3ETFTNQHHLTUL34NUAAWZG5OK2RGJ4V2U3Z",
)
.arg("add")
.arg("alice")
.assert()
.success();

fs::rename(
sandbox.dir().join(".stellar"),
sandbox.dir().join("_soroban"),
)
.unwrap();

sandbox
.new_assert_cmd("keys")
.env(
"SOROBAN_SECRET_KEY",
"SAQMV6P3OWM2SKCK3OEWNXSRYWK5RNNUL5CPHQGIJF2WVT4EI2BZ63GG",
)
.arg("add")
.arg("alice")
.assert()
.success();

fs::rename(
sandbox.dir().join("_soroban"),
sandbox.dir().join(".soroban"),
)
.unwrap();

sandbox
.new_assert_cmd("keys")
.arg("show")
.arg("alice")
.arg("--verbose")
.assert()
.success()
.stderr(predicate::str::contains(
"WARN soroban_cli::utils: the .stellar and .soroban config directories exist at path",
))
.stdout("SAQMV6P3OWM2SKCK3OEWNXSRYWK5RNNUL5CPHQGIJF2WVT4EI2BZ63GG\n");
}
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
6 changes: 4 additions & 2 deletions cmd/soroban-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use clap::CommandFactory;
use dotenvy::dotenv;
use tracing_subscriber::{fmt, EnvFilter};

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 +43,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 +80,7 @@ pub async fn main() {
upgrade_check(root.global_args.quiet).await;
});

let printer = print::Print::new(root.global_args.quiet);
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
24 changes: 21 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,32 @@ 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");
let soroban_exists = soroban_dir.exists();
let stellar_exists = stellar_dir.exists();

if stellar_exists && soroban_exists {
tracing::warn!("the .stellar and .soroban config directories exist at path {config_dir:?}, using the .stellar");
}

if stellar_exists {
return Ok(stellar_dir);
}
.join("soroban"))

if soroban_exists {
return Ok(soroban_dir);
}

Ok(stellar_dir)
}

impl Pwd for Args {
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
2 changes: 2 additions & 0 deletions cmd/soroban-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ 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;
}
}

Err(std::io::Error::new(
std::io::ErrorKind::Other,
"stellar directory not found",
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

0 comments on commit d16fcf1

Please sign in to comment.